1 #include <cstdlib>
2 #include <iostream>
3 #include <stdio.h>
4 #include <string.h>
5 #include <conio.h>
6 #define MAZE_INIT_SIZE 50
7 using namespace std;
8 struct P{
9 int row;
10 int col;
11 int pre;
12 };
13
14 struct MAZE{
15 char m[MAZE_INIT_SIZE][MAZE_INIT_SIZE];
16 int row,col;
17 P s;//起点
18 P e;//终点
19 };
20
21 int f_row[4]={1,-1,0,0};
22 int f_col[4]={0,0,1,-1};
23
24 int check(int i,int j,struct MAZE maze)//判断
25 {
26
27 if(i<0||j<0||i>=maze.row||j>=maze.col)
28 return 0;
29 if(maze.m[i][j]=='o')
30 return 1;
31 return 0;
32 }
33 void search(struct MAZE &maze);
34 void maze_game()
35 {
36 struct MAZE maze;
37
38 printf("%15s","");
39 cout<<"请输入迷宫的行数";
40 cin>>maze.row;
41 while(maze.row<1)
42 {printf("%15s","");cout<<"输入错误,请重新输入";cin>>maze.row;}
43
44
45
46 printf("%15s","");
47 cout<<"请输入迷宫的列数";
48 cin>>maze.col;
49 while(maze.col<1)
50 {printf("%15s","");cout<<"输入错误,请重新输入";cin>>maze.col;}
51 system("cls");
52 printf("%15s","");cout<<"注意:坐标从0开始\n";
53 printf("%15s","");
54 cout<<"请输入迷宫的起始点坐标";
55 cin>>maze.s.row;
56 cin>>maze.s.col;
57 maze.s.pre=-1;
58 while(maze.s.row<0||maze.s.col<0||maze.s.row>=maze.row||maze.s.col>=maze.col)
59 {printf("%15s","");cout<<"输入错误,请重新输入";cin>>maze.s.row>>maze.s.col;}
60
61 printf("%15s","");
62 cout<<"请输入迷宫的起始点坐标";
63 cin>>maze.e.row;
64 cin>>maze.e.col;
65 maze.e.pre=-1;
66 while(maze.e.row<0||maze.e.col<0||maze.e.col>=maze.col||maze.e.row>=maze.row)
67 {printf("%15s","");cout<<"输入错误,请重新输入";cin>>maze.e.row>>maze.e.col;}
68 system("cls");
69 //printf("%15s","");
70 cout<<"请输入迷宫地形,o表示空地#表示墙壁\n";
71
72 int i,j;
73 for(i=0;i<maze.row;i++)
74 for(j=0;j<maze.col;j++)
75 {/*printf("\n%15s","");*/cin>>maze.m[i][j];}
76 /*cout<<maze.row<<endl<<maze.col<<endl;
77 cout<<maze.s.row<<' '<<maze.s.col<<endl;
78 cout<<maze.e.row<<' '<<maze.e.col<<endl;
79 for(i=0;i<maze.row;i++)
80 {for(j=0;j<maze.col;j++)
81 cout<<maze.m[i][j];
82 cout<<endl;
83 }*/
84 search(maze);
85 }
86 void output(int end,P sq[]);
87 void search(struct MAZE &maze)
88 {
89 P sq[MAZE_INIT_SIZE*MAZE_INIT_SIZE];
90 int begin=0,end=1;
91 maze.m[maze.s.row][maze.s.col]='#';
92 sq[0].pre=-1;
93 sq[0].row=maze.s.row;
94 sq[0].col=maze.s.col;
95 int i,j,k;
96 while(begin<end)
97 {
98 for(k=0;k<4;k++)
99 {
100 i=sq[begin].row+f_row[k];
101 j=sq[begin].col+f_col[k];
102 if(check(i,j,maze))
103 {sq[end].row=i;
104 sq[end].col=j;
105 sq[end].pre=begin;
106 maze.m[i][j]='#';
107
108 if((i==maze.e.row)&&(j==maze.e.col))
109 {
110 system("cls");
111 printf("%5s","");cout<<"路径\n";
112 printf("%5s","");
113 output(end,sq);
114 cout<<endl;
115 return ;
116 }
117 end++;
118 }
119 }
120 begin++;
121 }
122 system("cls");
123 printf("%15s","");
124 cout<<"没有可达路径";
125 }
126
127
128 void output(int end,P sq[])//输出
129 {
130 if(end==0)
131 {cout<<"("<<sq[end].row<<","<<sq[end].col<<")";return ;}
132
133 if(end>0)
134 {output(sq[end].pre,sq);cout<<"-->("<<sq[end].row<<","<<sq[end].col<<")";return ;}
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150 int main(int argc, char *argv[])
151 {maze_game();
152 system("PAUSE");
153 return EXIT_SUCCESS;
154 }
155
156