-
Notifications
You must be signed in to change notification settings - Fork 0
/
robot.c
105 lines (74 loc) · 1.52 KB
/
robot.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
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
#include "stdio.h"
#include "stdlib.h"
#define ROW_SIZE 4
#define COL_SIZE 5
int grid[ROW_SIZE][COL_SIZE] =
{
{0, 0, 0, 0, 0}
,{0, 1, 0, 1, 0}
,{0, 1, 0, 1, 0}
,{0, 0, 0, 0, 0}
};
int hasPath(int x1, int y1, int x2, int y2)
{
if ((x2 < 0) || (x2 >= ROW_SIZE))
return 0;
if ((y2 < 0) || (y2 >= COL_SIZE))
return 0;
if ((x1 == x2) && (y1 == y2))
{
// printf("%d, %d\n", x2, y2);
grid[x2][y2] = 2;
return 1;
}
if (grid[x2][y2] == 1)
return 0;
if (hasPath(x1, y1, x2 - 1, y2) || (hasPath(x1, y1, x2, y2 - 1)))
{
grid[x2][y2] = 2;
return 1;
}
return 0;
}
int dfs(int x1, int y1, int x2, int y2)
{
int ret = 0, org = 0;
if ((x1 < 0) || (x1 >= ROW_SIZE))
return 0;
if ((y1 < 0) || (y1 >= COL_SIZE))
return 0;
if ((x1 == x2) && (y1 == y2))
{
grid[x1][y1] = 3;
return 1;
}
if (grid[x1][y1] == 1)
return 0;
if (grid[x1][y1] == 3)
return 0;
org = grid[x1][y1];
grid[x1][y1] = 3;
ret = dfs(x1, y1 - 1, x2, y2);
if (! ret)
ret = dfs(x1 + 1, y1, x2, y2);
if (! ret)
ret = dfs(x1, y1 + 1, x2, y2);
if (! ret)
ret = dfs(x1 - 1, y1, x2, y2);
if (! ret)
grid[x1][y1] = org;
return ret;
}
int main(int argc, char *argv[])
{
int i = 0, j = 0;
printf("only right down %d\n", hasPath(0, 0, ROW_SIZE - 1, COL_SIZE - 1));
printf("dfs %d\n", dfs(0, 0, ROW_SIZE - 1, COL_SIZE - 1));
for (i = 0; i < ROW_SIZE; ++i)
{
for (j = 0; j < COL_SIZE; ++j)
printf(" %d", grid[i][j]);
printf("\n");
}
return 0;
}