64,654
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#define M 7
#define N 7
struct point
{
int x;
int y;
};
int main()
{
int maze[M][N] =
{
0, 0, 1, 1 ,0, 1, 0,
0, 0, 1, 1 ,0, 1, 0,
0, 0, 0, 1 ,0, 1, 0,
0, 0, 0, 0 ,0, 0, 0,
0, 1, 1, 1 ,0, 1, 0,
0, 1, 1, 0 ,0, 1, 0,
0, 0, 1, 1 ,0, 0, 0,
};
int count = 0;
int i,j;
int flag = 0;
struct point route[50];
route[0].x = route[0].y = 0;
++count;
maze[0][0] = 1;
while(count>0)
{
i=route[count-1].x;
j=route[count-1].y;
if((i<M-1) && (maze[i+1][j]==0))
{
route[count].x = i + 1;
route[count].y = j;
maze[i+1][j] = 1;
++count;
}
else if((j<N-1) && (maze[i][j+1]==0))
{
route[count].x = i;
route[count].y = j + 1;
maze[i][j+1] = 1;
++count;
}
else if((i>0) && (maze[i-1][j]==0))
{
route[count].x = i - 1;
route[count].y = j;
maze[i-1][j] = 1;
++count;
}
else if((j>0) && (maze[i][j-1]==0))
{
route[count].x = i;
route[count].y = j - 1;
maze[i][j-1] = 1;
++count;
}
else
{
if((i==M-1) && (j==N-1))
{
flag = 1;
break;
}
--count;
}
}
if(flag == 1)
{
printf("Route exist!\n");
for(i=0; i<count-1; ++i)
{
printf("#%d: [%d, %d] -> [%d, %d]\n", i+1, route[i].x, route[i].y, route[i+1].x, route[i+1].y);
}
}
else
{
printf("Can't go through the maze!\n");
}
return 0;
}