就是普通的跑迷宫了,但要注意他让输出每一步的坐标,这里就要在每一步保存上一步的坐标,最后递归输出,具体看实现
#include<queue>
#include<iostream>
#include<cstdio>
using namespace std;
struct Link
{
int n,pre;
};
Link a[5][5];
int next[]= {-1,0,0,1,0,-1,1,0};
void bfs()
{
a[0][0].n=1;
queue<int> q;
q.push(0);
while(a[4][4].pre==-1)
{
int t=q.front();
int x=t/10,y=t%10; //我这里把坐标用一个整数表示,十位代表 i,个位代表j
for(int i=0; i<8; i++)
{
int ti=x+next[i++];
int tj=y+next[i];
if(ti>=0&&ti<5&&tj>=0&&tj<5&&!a[ti][tj].n)
{
a[ti][tj].n=a[x][y].n+1;
a[ti][tj].pre=t;
q.push(ti*10+tj);
}
}
q.pop();
}
}
void output(int coor)
{
int i=coor/10,j=coor%10;
if(a[i][j].pre!=-1)
output(a[i][j].pre);
printf("(%d, %d)\n",i,j);
}
int main()
{
for(int i=0; i<5; i++)
for(int j=0; j<5; j++)
{
cin>>a[i][j].n;
a[i][j].pre=-1;
}
bfs();
output(44);
}