Skip to content

Commit 2eaea5a

Browse files
committed
added day3 q1 solution
1 parent c912c70 commit 2eaea5a

1 file changed

Lines changed: 86 additions & 0 deletions

File tree

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
/*
3+
TC -O(N)
4+
SC -O(1)
5+
6+
*/
7+
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
string s;
13+
bool visited[7][7];
14+
int ans;
15+
16+
bool inside(int x, int y) {
17+
return x >= 0 && x < 7 && y >= 0 && y < 7;
18+
}
19+
20+
void dfs(int x, int y, int step) {
21+
if (x == 6 && y == 0) {
22+
if (step == 48) ans++;
23+
return;
24+
}
25+
26+
if (step == 48) return;
27+
28+
if ((!inside(x-1,y) || visited[x-1][y]) &&
29+
(!inside(x+1,y) || visited[x+1][y])) {
30+
if (inside(x,y-1) && !visited[x][y-1] &&
31+
inside(x,y+1) && !visited[x][y+1])
32+
return;
33+
}
34+
35+
if ((!inside(x,y-1) || visited[x][y-1]) &&
36+
(!inside(x,y+1) || visited[x][y+1])) {
37+
if (inside(x-1,y) && !visited[x-1][y] &&
38+
inside(x+1,y) && !visited[x+1][y])
39+
return;
40+
}
41+
42+
char c = s[step];
43+
44+
if (c == '?' || c == 'U') {
45+
int nx = x - 1, ny = y;
46+
if (inside(nx, ny) && !visited[nx][ny]) {
47+
visited[nx][ny] = true;
48+
dfs(nx, ny, step + 1);
49+
visited[nx][ny] = false;
50+
}
51+
}
52+
53+
if (c == '?' || c == 'D') {
54+
int nx = x + 1, ny = y;
55+
if (inside(nx, ny) && !visited[nx][ny]) {
56+
visited[nx][ny] = true;
57+
dfs(nx, ny, step + 1);
58+
visited[nx][ny] = false;
59+
}
60+
}
61+
62+
if (c == '?' || c == 'L') {
63+
int nx = x, ny = y - 1;
64+
if (inside(nx, ny) && !visited[nx][ny]) {
65+
visited[nx][ny] = true;
66+
dfs(nx, ny, step + 1);
67+
visited[nx][ny] = false;
68+
}
69+
}
70+
71+
if (c == '?' || c == 'R') {
72+
int nx = x, ny = y + 1;
73+
if (inside(nx, ny) && !visited[nx][ny]) {
74+
visited[nx][ny] = true;
75+
dfs(nx, ny, step + 1);
76+
visited[nx][ny] = false;
77+
}
78+
}
79+
}
80+
81+
int main() {
82+
cin >> s;
83+
visited[0][0] = true;
84+
dfs(0, 0, 0);
85+
cout << ans;
86+
}

0 commit comments

Comments
 (0)