-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
79 lines (68 loc) · 1.28 KB
/
Copy pathmain.cpp
File metadata and controls
79 lines (68 loc) · 1.28 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
78
79
#include <iostream>
#include <stack>
using namespace std;
typedef struct Point
{
int x;
int y;
} POINT;
stack<POINT> s;
int row = 4, col = 4;
POINT item[4] = {
{ -1, 0 },
{ 0, 1 },
{ 1, 0 },
{ 0, -1 }
};
int map[6][6] = {
{ 1, 1, 1, 1, 1, 1 },//0
{ 1, 0, 0, 1, 0, 1 },//1
{ 1, 0, 0, 1, 0, 1 },//2
{ 1, 0, 0, 0, 0, 1 },//3
{ 1, 0, 0, 1, 0, 1 },//4
{ 1, 1, 1, 1, 1, 1 },
};
void dfs(int row, int col, POINT p);
int main()
{
POINT begin;
begin.x = 1;
begin.y = 1;
dfs(row, col, begin);
return 0;
}
void printPath()
{
cout << "-------------\n";
while (!s.empty())
{
cout << "[" << s.top().x << "," << s.top().y << "]\n";
s.pop();
}
cout << "-------------\n";
}
void dfs(int row, int col, POINT p)
{
int x = p.x, y = p.y;
int i = 0;
POINT t;
map[p.x][p.y] = -1; // 走过的节点赋值为 -1
s.push(p);
if (row == p.x && col == p.y)
{
printPath();
map[p.x][p.y] = 0;
}
else
{
for (i = 0; i < 4; i++)
{
t.x = x + item[i].x;
t.y = y + item[i].y;
if (map[t.x][t.y] == 0)
{
dfs(row, col, t);
}
}
}
}