-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfs.c
54 lines (48 loc) · 972 Bytes
/
dfs.c
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
#include <stdio.h>
#define END_X 3
#define END_Y 0
#define LENGTH_A 4
#define LENGTH 3
int map[LENGTH_A][LENGTH_A] = {
{1,1,0,0},
{0,1,1,1},
{1,1,0,1},
{1,0,0,1}
};
//int mark[4][4] = map;
int move[4][2] = {
{1,0},
{-1,0},
{0,1},
{0,-1}
};
int dfs(int x, int y){
printf("x = %d y = %d\n", x, y);
if (x == END_X && y == END_Y){
return 1; //赋值给上一次的flag,并返回到flag所在层
}
else{
for (int i = 0; i < 4; i++){
if (x+move[i][0] > LENGTH || x+move[i][0] < 0 || y+move[i][1] > LENGTH || y+move[i][1] < 0 || map[x+move[i][0]][y+move[i][1]] == 0){
continue;
}
map[x+move[i][0]][y+move[i][1]] = 0;
int flag = dfs(x+move[i][0], y+move[i][1]);
map[x+move[i][0]][y+move[i][1]] = 1;
if (flag == 1){
return 1;//赋值给函数体,并返回到main层
}
}
return 0;
}
}
int main(void){
int flag = dfs(0, 0);
if (flag == 1){
printf("can get out!!\n");
}
else{
printf("no answer~");
}
return 0;
}