-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmaze.cpp
More file actions
77 lines (61 loc) · 1.61 KB
/
maze.cpp
File metadata and controls
77 lines (61 loc) · 1.61 KB
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
#include <bits/stdc++.h>
using namespace std;
int jumpPath(vector<vector<char>>& grid, int n, int m) {
const int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<vector<vector<int>>> visited(n, vector<vector<int>>(m, vector<int>(5, 0)));
queue<tuple<int, int, int, int>> q; // (x, y, k, dir)
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (grid[i][j] == 'S') {
q.push(make_tuple(i, j, 1, -1));
}
}
}
while (!q.empty()) {
int x, y, k, dir;
tie(x, y, k, dir) = q.front();
q.pop();
if (grid[x][y] == 'E') {
return k - 1; // Found the destination cell
}
for (int d = 0; d < 4; d++) {
for (int j = 1; j <= k; j++) {
int nx = x + dirs[d][0] * j;
int ny = y + dirs[d][1] * j;
if (nx >= 0 && nx < n && ny >= 0 && ny < m && grid[nx][ny] != '#') {
if (k == 1 || dir == d) {
if (visited[nx][ny][d] == 0) {
visited[nx][ny][d] = 1;
q.push(make_tuple(nx, ny, k + 1, d));
}
}
} else {
break; // Stop jumping in this direction
}
}
}
}
return -1; // No path found
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<char>> grid(n, vector<char>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> grid[i][j];
}
}
int result = jumpPath(grid, n, m);
if (result != -1) {
cout << "Minimum moves required: " << result << endl;
} else {
cout << "No path found to reach the destination." << endl;
}
return 0;
}
// S****#
// **#***
// *****#
// *#*#**
// #****E