-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1625.cc
71 lines (61 loc) · 1.43 KB
/
1625.cc
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <bits/stdc++.h>
using namespace std;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
int reqd_x = 6;
int reqd_y = 0;
string s;
map<char, int> mp;
bool vis[8][8];
bool ok(int x, int y) {
int horizontal_left = 0;
if (x == 0 || vis[x - 1][y])
horizontal_left += 1;
if (x == 6 || vis[x + 1][y])
horizontal_left += 1;
int vertical_left = 0;
if (y == 0 || vis[x][y - 1])
vertical_left += 1;
if (y == 6 || vis[x][y + 1])
vertical_left += 1;
int v1 = min(horizontal_left, vertical_left);
int v2 = max(horizontal_left, vertical_left);
return (v1 != 0 || v2 == 1);
}
int solve(int x, int y, int id) {
if (id == s.size()) return (x == reqd_x && y == reqd_y);
if (vis[reqd_x][reqd_y] || !ok(x, y)) return 0;
int ret = 0;
if (s[id] == '?') {
for (int i = 0; i < 4; i++) {
int nx = x + dx[i];
int ny = y + dy[i];
if (nx < 0 || ny < 0 || nx > 6 || ny > 6 || vis[nx][ny]) continue;
vis[nx][ny] = 1;
ret += solve(nx, ny, id + 1);
vis[nx][ny] = 0;
}
} else {
int i = mp[s[id]];
int nx = x + dx[i];
int ny = y + dy[i];
if (!(nx < 0 || ny < 0 || nx > 6 || ny > 6 || vis[nx][ny])) {
vis[nx][ny] = 1;
ret += solve(nx, ny, id + 1);
vis[nx][ny] = 0; }
}
return ret;
}
int main() {
mp['D'] = 0;
mp['R'] = 3;
mp['L'] = 1;
mp['U'] = 2;
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++)
vis[i][j] = 0;
cin >> s;
vis[0][0] = 1;
cout << solve(0, 0, 0) << "\n";
return 0;
}