-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1625-grid-paths.cpp
161 lines (137 loc) · 5.86 KB
/
1625-grid-paths.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include <bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll long long
#define vi vector<int>
#define vl vector<ll>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll LLINF = 1e18;
// 2D grid to track visited cells
vector<vector<bool>> visited(9, vector<bool>(9));
// Variables for counting paths and tracking current position
int countPaths = 0, currentIndex = -1;
// Function to check if a cell is a valid one-way path based on specific conditions
bool isValid(int i, int j) {
return (i - 7 && j - 1 && !visited[i - 1][j] + !visited[i + 1][j] + !visited[i][j + 1] + !visited[i][j - 1] == 1);
}
// Depth-First Search (DFS) function to explore paths in the grid
void dfs(int row, int col, const string& path) {
// Check if the DFS traversal has reached the destination (7, 1)
if (row == 7 && col == 1) {
// Check if the current path length is 47, increment countPaths if true
countPaths += currentIndex == 47;
return;
}
// Increment the currentIndex to move to the next character in the path string
++currentIndex;
// Mark the current cell as visited
visited[row][col] = 1;
// Check if moving right is a valid option and follows the path string
if (!visited[row][col + 1] && isValid(row, col + 1)) {
if (path[currentIndex] == '?' || path[currentIndex] == 'R') {
dfs(row, col + 1, path);
}
// Backtrack by decrementing currentIndex and marking the cell as unvisited
--currentIndex;
visited[row][col] = 0;
return;
}
// Check if moving left is a valid option and follows the path string
if (!visited[row][col - 1] && isValid(row, col - 1)) {
if (path[currentIndex] == '?' || path[currentIndex] == 'L') {
dfs(row, col - 1, path);
}
// Backtrack by decrementing currentIndex and marking the cell as unvisited
--currentIndex;
visited[row][col] = 0;
return;
}
// Check if moving down is a valid option and follows the path string
if (!visited[row + 1][col] && isValid(row + 1, col)) {
if (path[currentIndex] == '?' || path[currentIndex] == 'D') {
dfs(row + 1, col, path);
}
// Backtrack by decrementing currentIndex and marking the cell as unvisited
--currentIndex;
visited[row][col] = 0;
return;
}
// Check if moving up is a valid option and follows the path string
if (!visited[row - 1][col] && isValid(row - 1, col)) {
if (path[currentIndex] == '?' || path[currentIndex] == 'U') {
dfs(row - 1, col, path);
}
// Backtrack by decrementing currentIndex and marking the cell as unvisited
--currentIndex;
visited[row][col] = 0;
return;
}
// Check if moving right (R) is a valid option and follows the path string
if (!visited[row][col + 1] && (path[currentIndex] == '?' || path[currentIndex] == 'R')) {
// Check if the cell two steps to the right is not visited
// and if the diagonal cells (top-right and bottom-right) are not blocked
if (!(visited[row][col + 2] && !visited[row + 1][col + 1] && !visited[row - 1][col + 1])) {
// If conditions are met, recursively explore the path by moving right
dfs(row, col + 1, path);
}
}
// Check if moving down (D) is a valid option and follows the path string
if (!visited[row + 1][col] && (path[currentIndex] == '?' || path[currentIndex] == 'D')) {
// Check if the cell two steps down is not visited
// and if the diagonal cells (bottom-left and bottom-right) are not blocked
if (!(visited[row + 2][col] && !visited[row + 1][col - 1] && !visited[row + 1][col + 1])) {
// If conditions are met, recursively explore the path by moving down
dfs(row + 1, col, path);
}
}
// Check if moving left (L) is a valid option and follows the path string
if (!visited[row][col - 1] && (path[currentIndex] == '?' || path[currentIndex] == 'L')) {
// Check if the cell two steps to the left is not visited
// and if the diagonal cells (top-left and bottom-left) are not blocked
if (!visited[row][col - 1] && !(visited[row][col - 2] && !visited[row - 1][col - 1] && !visited[row + 1][col - 1])) {
// If conditions are met, recursively explore the path by moving left
dfs(row, col - 1, path);
}
}
// Check if moving up (U) is a valid option and follows the path string
if (!visited[row - 1][col] && (path[currentIndex] == '?' || path[currentIndex] == 'U')) {
// Check if the cell two steps up is not visited
// and if the diagonal cells (top-left and top-right) are not blocked
if (!(visited[row - 2][col] && !visited[row - 1][col - 1] && !visited[row - 1][col + 1])) {
// If conditions are met, recursively explore the path by moving up
dfs(row - 1, col, path);
}
}
// If none of the above conditions are met, backtrack by decrementing currentIndex and marking the cell as unvisited
--currentIndex;
visited[row][col] = 0;
}
void solve() {
string path;
cin >> path;
// Initialize the visited grid based on grid edge conditions
for (int row = 0; row < 9; ++row) {
for (int col = 0; col < 9; ++col) {
visited[row][col] = row == 8 || !row || col == 8 || !col;
}
}
// Start the DFS traversal from the initial position (1, 1) with the given path string
dfs(1, 1, path);
cout << countPaths << " ";
}
int main() {
fastio
int tc = 1; // Number of test cases
// cin >> tc;
while (tc--) {
solve();
}
return 0;
}