-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.cpp
More file actions
76 lines (60 loc) · 1.49 KB
/
maze.cpp
File metadata and controls
76 lines (60 loc) · 1.49 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
#include <bits/stdc++.h>
using namespace std;
#define VISITED 2
int maze[10][10];
int cost[4] = {-3, -1, -1, 0};
int dir[4][2] = {{-1, 0}, {0, 1}, {0, -1}, {1, 0}};
int m, n, p;
int final_p = -1000;
struct position {
int x;
int y;
position(int vx, int vy) : x(vx), y(vy) {}
};
vector<position> pathStack;
vector<position> minCostPath;
void search(int x, int y, int cur_p)
{
maze[x][y] = VISITED;
pathStack.push_back(position(x, y));
if (x == 0 && y == m-1 && cur_p >= 0) {
if (cur_p > final_p) {
final_p = cur_p;
minCostPath = pathStack;
}
pathStack.pop_back();
maze[x][y] = 1;
return;
}
if (cur_p > 0) {
for (int i = 0; i < 4; i++) {
int nx = dir[i][0] + x;
int ny = dir[i][1] + y;
int np = cur_p + cost[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < m && maze[nx][ny] == 1)
search(nx, ny, np);
}
}
pathStack.pop_back();
maze[x][y] = 1;
}
void printPath(vector<position> &path)
{
for (int i = 0; i < path.size(); i++) {
cout << "[" << path[i].x << "," << path[i].y << "]";
if (i < path.size() - 1)
cout << ",";
}
}
int main()
{
cin >> n >> m >> p;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> maze[i][j];
search(0, 0, p);
if (final_p == -1000)
cout << "Can not escape!" ;
else
printPath(minCostPath);
}