题目描述
定义一个二维数组:
int maze[5][5] = {
0,1,0,0,0,
0,1,0,1,0,
0,0,0,0,0,
0,1,1,1,0,
0,0,0,1,0,
};
它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编程序找出从左上角到右下角的最短路线。
输入格式:
一个5 × 5的二维数组,表示一个迷宫。数据保证有唯一解。
输出格式:
左上角到右下角的最短路径,格式如样例所示。
样例输入:
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0
样例输出:
(0, 0)
(1, 0)
(2, 0)
(2, 1)
(2, 2)
(2, 3)
(2, 4)
(3, 4)
(4, 4)
时间限制:
1000
空间限制:
65536
我的代码
#include<bits/stdc++.h>
using namespace std;
int head,tail;
int dx[4]={-1,1,0,0};
int dy[4]={0,0,-1,1};
int n,vis[6][6],a[6][6];
struct node{
int x,y,step,pre;
}que[100005];
void f(int x)
{
if(x==-1)
{
return ;
}
else
{
f(que[x].pre);
printf("(%d, %d)\n",que[x].x,que[x].y);
}
}
void bfs()
{
que[++tail]=node{0,0,0,-1};
vis[0][0]=1;
while(tail>=head)
{
for(int i=0;i<4;i++)
{
int tx=que[head].x+dy[i];
int ty=que[head].y+dy[i];
if(tx>=0 and ty>=0 and tx<=5 and ty<=5 and vis[tx][ty]==0 and a[tx][ty]==0)
{
que[++tail]=node{tx,ty,que[head].step+1,head};
vis[tx][ty]=1;
if(tx==4 and ty==4)
{
f(tail);
exit(0);
}
}
}
head++;
}
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
{
cin>>a[i][j];
}
}
bfs();
}
请各位大佬看看有什么问题