思路:
重点是如何记录并输出最短路径。我们可以用一个 node类型的 pre[ ][ ] 二维数组记录这个状态的前一个状态。如果是正向搜索(从(0,0)到(4,4)),找到结果时需要逆序输出;如果是逆向搜索(从(4,4)到(0,0))找到结果直接输出。
逆向搜索(时间少)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
-1,0,
1,0,
0,-1,
0,1
};
struct node
{
int x,y;
};
node pre[10][10]; //记录上一个状态
void bfs(int x,int y)
{
queue<node> q;
node w,h;
w.x = x;
w.y = y;
q.push(w);
vis[w.x][w.y] = 1;
while(!q.empty())
{
h = q.front();
q.pop();
for(int i=0;i<4;i++)
{
w.x = h.x+b[i][0];
w.y = h.y+b[i][1];
if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
{
q.push(w);
vis[w.x][w.y] = 1;
pre[w.x][w.y] = h;
if(w.x==0 && w.y==0)
{
while(!(w.x==4 && w.y==4))
{
printf("(%d, %d)\n",w.x,w.y);
w = pre[w.x][w.y];
}
return ;
}
}
}
}
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
}
memset(vis,0,sizeof(vis));
bfs(4,4);
printf("(4, 4)\n");
return 0;
}
正向搜索(比逆向搜索多用了一个栈)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
-1,0,
1,0,
0,-1,
0,1
};
struct node
{
int x,y;
};
node pre[10][10];
stack<node> s;
void bfs(int x,int y)
{
queue<node> q;
node w,h;
w.x = x;
w.y = y;
q.push(w);
vis[w.x][w.y] = 1;
while(!q.empty())
{
h = q.front();
q.pop();
for(int i=0;i<4;i++)
{
w.x = h.x+b[i][0];
w.y = h.y+b[i][1];
if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
{
q.push(w);
vis[w.x][w.y] = 1;
pre[w.x][w.y] = h;
if(w.x==4 && w.y==4)
{
while(!(w.x==0 && w.y==0))
{
s.push(w);
w = pre[w.x][w.y];
}
return ;
}
}
}
}
}
void print()
{
while(!s.empty())
{
node c = s.top();
printf("(%d, %d)\n",c.x,c.y);
s.pop();
}
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
}
memset(vis,0,sizeof(vis));
bfs(0,0);
printf("(0, 0)\n");
print();
return 0;
}
一开始用指针记录上一个状态,但是不知道哪里出了问题,请各位大佬指教(非常感谢)
错误代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
using namespace std;
const int maxn=1e6+10;
int vis[10][10];
int a[10][10];
int b[4][2]={
-1,0,
1,0,
0,-1,
0,1
};
struct node
{
int x,y;
node *pre; //记录它的上一种状态
};
node h[10000];
stack<node> s;
void bfs(int x,int y)
{
queue<node> q;
node w;
w.x = x;
w.y = y;
w.pre = NULL;
q.push(w);
vis[w.x][w.y] = 1;
int cnt = -1;
while(!q.empty())
{
h[++cnt] = q.front();
cout<<cnt<<endl;
q.pop();
for(int i=0;i<4;i++)
{
w.x = h[cnt].x+b[i][0];
w.y = h[cnt].y+b[i][1];
if(!vis[w.x][w.y] && a[w.x][w.y]==0 && w.x>=0&&w.x<5 && w.y>=0&&w.y<5)
{
cout<<h[cnt].x<<" "<<h[cnt].y<<endl;
cout<<w.x<<" "<<w.y<<endl;
q.push(w);
vis[w.x][w.y] = 1;
w.pre = &h[cnt];
if(w.x==4 && w.y==4)
{
while(w.pre)
{
s.push(w);
w = *w.pre;
}
return ;
}
}
}
}
}
void print()
{
while(!s.empty())
{
node c = s.top();
printf("(%d, %d)\n",c.x,c.y);
s.pop();
}
}
int main()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<5;j++)
scanf("%d",&a[i][j]);
}
memset(vis,0,sizeof(vis));
bfs(0,0);
print();
return 0;
}