-
Notifications
You must be signed in to change notification settings - Fork 166
/
Grid Paths I.cpp
47 lines (40 loc) · 1.24 KB
/
Grid Paths I.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
const int N = 7;
int ans;
char c[N*N+1];
bool vis[N+1][N+1];
bool inbounds(int x, int y){
return 1 <= x && x <= N && 1 <= y && y <= N;
}
void dfs(int x, int y, int i){
if(i == N*N-1 || (x == N && y == 1)){
ans += (i == N*N-1 && (x == N && y == 1));
return;
}
if((!inbounds(x+1, y) || vis[x+1][y]) && (!inbounds(x-1, y) || vis[x-1][y]))
if(inbounds(x, y-1) && !vis[x][y-1] && inbounds(x, y+1) && !vis[x][y+1])
return;
if((!inbounds(x, y+1) || vis[x][y+1]) && (!inbounds(x, y-1) || vis[x][y-1]))
if(inbounds(x+1, y) && !vis[x+1][y] && inbounds(x-1, y) && !vis[x-1][y])
return;
vis[x][y] = true;
if(c[i] == 'D' || c[i] == '?')
if(inbounds(x+1, y) && !vis[x+1][y])
dfs(x+1, y, i+1);
if(c[i] == 'U' || c[i] == '?')
if(inbounds(x-1, y) && !vis[x-1][y])
dfs(x-1, y, i+1);
if(c[i] == 'R' || c[i] == '?')
if(inbounds(x, y+1) && !vis[x][y+1])
dfs(x, y+1, i+1);
if(c[i] == 'L' || c[i] == '?')
if(inbounds(x, y-1) && !vis[x][y-1])
dfs(x, y-1, i+1);
vis[x][y] = false;
}
int main(){
scanf(" %s", c);
dfs(1, 1, 0);
printf("%d\n", ans);
}