算法分析——走迷宫问题

原文链接:http://www.cnblogs.com/liangyan19910818/archive/2011/08/19/2146140.html
 1 #include <stdio.h>
2
3 int visit(int i , int j);
4
5 int maze[7][7] = {
6 {2,2,2,2,2,2,2},
7 {2,0,0,0,0,0,2},
8 {2,0,2,0,2,0,2},
9 {2,0,0,2,0,2,2},
10 {2,2,0,2,0,2,2},
11 {2,0,0,0,0,0,2},
12 {2,2,2,2,2,2,2},
13 };
14 int si = 1 , sj = 1 ;
15 int ei = 5 , ej = 5 ;
16 int success = 0 ;
17
18 int main()
19 {
20
21 int i , j ;
22
23 printf("Display the maze :\n");
24 for(i = 0 ; i < 7 ; i++)
25 {
26 for(j = 0 ; j < 7 ; j++)
27 {
28 if(maze[i][j] == 2)
29 {
30 printf("■");
31 }
32 else
33 {
34 printf(" ");
35 }
36 }
37 printf("\n");
38 }
39
40 if(visit(si , sj) == 0)
41 {
42 printf("\nNot found exit !");
43 }
44 else
45 {
46 printf("Display the maze :\n");
47 for(i = 0 ; i < 7 ; i++)
48 {
49 for(j = 0 ; j < 7 ; j++)
50 {
51 if(maze[i][j] == 2)
52 {
53 printf("■");
54 }
55 else if(maze[i][j] == 1)
56 {
57 printf("◇");
58 }
59 else
60 {
61 printf(" ");
62 }
63 }
64 printf("\n");
65 }
66 }
67    return 0 ;
68 }
69
70 int visit(int i , int j)
71 {
72 maze[i][j] = 1 ;
73
74 if(i == ei && j == ej)
75 success = 1 ;
76
77 if(success != 1 && maze[i][j + 1] == 0)
78 visit(i , j + 1);
79 if(success != 1 && maze[i + 1][j] == 0)
80 visit(i + 1 , j);
81 if(success != 1 && maze[i - 1][j] == 0)
82 visit(i - 1 , j);
83 if(success != 1 && maze[i][j - 1] == 0)
84 visit(i , j - 1);
85
86 if(success != 1)
87 maze[i][j] == 0 ;
88
89 return success ;
90 }

算法分析——走迷宫问题

显示所有路径:

 1 #include <stdio.h>
2
3 int visit(int i , int j);
4
5 int maze[9][9] = {
6 {2, 2, 2, 2, 2, 2, 2, 2, 2},
7 {2, 0, 0, 0, 0, 0, 0, 0, 2},
8 {2, 0, 2, 2, 0, 2, 2, 0, 2},
9 {2, 0, 2, 0, 0, 2, 0, 0, 2},
10 {2, 0, 2, 0, 2, 0, 2, 0, 2},
11 {2, 0, 0, 0, 0, 0, 2, 0, 2},
12 {2, 2, 0, 2, 2, 0, 2, 2, 2},
13 {2, 0, 0, 0, 0, 0, 0, 0, 2},
14 {2, 2, 2, 2, 2, 2, 2, 2, 2},
15 };
16
17 int si = 1 , sj = 1 ;
18 int ei = 7 , ej = 7 ;
19 int success = 0 ;
20
21 int main()
22 {
23
24 int i , j ;
25
26 printf("Display the maze :\n");
27 for(i = 0 ; i < 9 ; i++)
28 {
29 for(j = 0 ; j < 9 ; j++)
30 {
31 if(maze[i][j] == 2)
32 {
33 printf("█");
34 }
35 else
36 {
37 printf(" ");
38 }
39 }
40 printf("\n");
41 }
42
43 visit(si , sj) ;
44
45 return 0 ;
46 }
47
48 int visit(int i , int j)
49 {
50 int m , n ;
51 maze[i][j] = 1 ;
52
53 if(i == ei && j == ej)
54 {
55 printf("Display the maze :\n");
56 for(m = 0 ; m < 9 ; m++)
57 {
58 for(n = 0 ; n < 9 ; n++)
59 {
60 if(maze[m][n] == 2)
61 {
62 printf("█");
63 }
64 else if(maze[m][n] == 1)
65 {
66 printf("◇");
67 }
68 else
69 {
70 printf(" ");
71 }
72 }
73 printf("\n");
74 }
75 success = 1 ;
76 }
77
78
79 if(maze[i][j + 1] == 0)
80 visit(i , j + 1);
81 if(maze[i + 1][j] == 0)
82 visit(i + 1 , j);
83 if(maze[i - 1][j] == 0)
84 visit(i - 1 , j);
85 if(maze[i][j - 1] == 0)
86 visit(i , j - 1);
87
88 maze[i][j] = 0 ;
89
90 return success ;
91 }

算法分析——走迷宫问题

转载于:https://www.cnblogs.com/liangyan19910818/archive/2011/08/19/2146140.html

上一篇:[深度优先搜索] leetcode 864 Shortest Path to Get All Keys


下一篇:BFS(二):数的变换