转载于http://blog.csdn.net/wdkirchhoff/article/details/41121517
自己修改了部分
BFS、双向BFS和A*
光说不练是没用的,我们从广为人知的POJ 2243这道题谈起:题目大意:给定一个起点和一个终点,按骑士的走法(走日字),从起点到终点的最少移动多少次
设A为寻路起点,B为目标终点。
1 BFS
BFS其实是退化的A*算法,因为他没有启发函数做指引
Memory | Time |
---|---|
144K | 407MS |
简单的代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char ss[3];
char ee[3];
typedef struct node
{
int x;
int y;
int steps;
}node;
int d[8][2]={{-2,1},{-2,-1},{-1,-2},{-1,2},{2,-1},{2,1},{1,-2},{1,2}};
int visited[8][8];
node s,e;
int in(node n)
{
if(n.x<0||n.y<0||n.x>7||n.y>7)
return 0;
return 1;
}
void bfs()
{
queue<node>q;
memset(visited,0,sizeof(visited));
q.push(s);
visited[s.x][s.y]=1;
while(!q.empty())
{
node st=q.front();
q.pop();
if(st.x==e.x&&st.y==e.y)
{
printf("To get from %s to %s takes %d knight moves.\n",ss,ee,st.steps);
break;
}
for(int i=0;i<8;++i)
{
node t;
t.x=st.x+d[i][0];
t.y=st.y+d[i][1];
if(in(t)&&visited[t.x][t.y]==0)
{
visited[t.x][t.y]=1;
t.steps=st.steps+1;
q.push(t);
}
}
}
}
int main()
{
while(scanf("%s %s",ss,ee)==2)
{
s.x=ss[0]-'a';
s.y=ss[1]-'1';
e.x=ee[0]-'a';
e.y=ee[1]-'1';
bfs();
}
return 0;
}
2 双向BFS
双向bfs就是用两个队列,一个队列保存从起点开始的状态,另一个保存从终点开始向前搜索的状态,双向bfs主要是区分每个格子是从起点开始搜索到的还是从终点开始搜索到的.每个经过的格子结点保存到达该格子经过的步数,这样两边要是相交了相加就是结果
Memory | Time |
---|---|
144K | 141MS |
明显的省时间
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
char ss[3];
char ee[3];
typedef struct node
{
int x;
int y;
int steps;
}node;
int d[8][2]={{-2,1},{-2,-1},{-1,-2},{-1,2},{2,-1},{2,1},{1,-2},{1,2}};
int visited[8][8];
int color[8][8];//区分当前位置是哪个队列查找过了
node s,e;
int in(node n)
{
if(n.x<0||n.y<0||n.x>7||n.y>7)
return 0;
return 1;
}
int bfs()
{
queue<node>qf; //我发现如果把qf和qb放在外面的话,节省的时间挺惊人的,耗时16MS
queue<node>qb;
memset(visited,0,sizeof(visited));
memset(color,0,sizeof(color));
qf.push(s);
qb.push(e);
visited[s.x][s.y]=0;
visited[e.x][e.y]=1;
color[s.x][s.y]=1;//着色
color[e.x][e.y]=2;
while(!qf.empty()||!qb.empty())
{
if(!qf.empty())
{
node st=qf.front();
qf.pop();
for(int i=0;i<8;++i)
{
node t;
t.x=st.x+d[i][0];
t.y=st.y+d[i][1];
if(in(t))
{
if(color[t.x][t.y]==0){
visited[t.x][t.y]=visited[st.x][st.y]+1;
color[t.x][t.y]=1;
qf.push(t);
}
else if(color[t.x][t.y]==2){
return visited[st.x][st.y]+visited[t.x][t.y];
}
}
}
}
if(!qb.empty())
{
node st=qb.front();
qb.pop();
for(int i=0;i<8;++i)
{
node t;
t.x=st.x+d[i][0];
t.y=st.y+d[i][1];
if(in(t))
{
if(color[t.x][t.y]==0){
visited[t.x][t.y]=visited[st.x][st.y]+1;
color[t.x][t.y]=2;
qb.push(t);
}
else if(color[t.x][t.y]==1){
return visited[st.x][st.y]+visited[t.x][t.y];
}
}
}
}
}
}
int main()
{
// freopen("in.txt","r",stdin);
while(scanf("%s %s",ss,ee)==2)
{
s.x=ss[0]-'a';
s.y=ss[1]-'1';
e.x=ee[0]-'a';
e.y=ee[1]-'1';
s.steps=0;
e.steps=1;
if(s.x==e.x&&s.y==e.y)
printf("To get from %s to %s takes 0 knight moves.\n",ss,ee);
else
printf("To get from %s to %s takes %d knight moves.\n",ss,ee,bfs());
}
return 0;
}
3 A*算法
选择路径中经过哪个方格的关键是下面这个等式:F = G + H这里:
- G = 从起点A,沿着产生的路径,移动到网格上指定方格的移动耗费。
- H = 从网格上那个方格移动到终点B的预估移动耗费。这经常被称为启发式的,可能会让你有点迷惑。这样叫的原因是因为它只是个猜测。我们没办法事先知道路径的长度,因为路上可能存在各种障碍(墙,水,等等)。
A*算法步骤为:
- 把起始格添加到开启列表。
- 重复如下的工作:
- 寻找开启列表中F值最低的格子。我们称它为当前格。
- 把它切换到关闭列表。
- 对相邻的格中的每一个?
- 如果它不可通过或者已经在关闭列表中,略过它。反之如下。
- 如果它不在开启列表中,把它添加进去。把当前格作为这一格的父节点。记录这一格的F,G,和H值。
- 如果它已经在开启列表中,用G值为参考检查新的路径是否更好。更低的G值意味着更好的路径。如果是这样,就把这一格的父节点改成当前格,并且重新计算这一格的G和F值。如果你保持你的开启列表按F值排序,改变之后你可能需要重新对开启列表排序。
- 停止,当你
- 把目标格添加进了关闭列表,这时候路径被找到,或者
- 没有找到目标格,开启列表已经空了。这时候,路径不存在。
- 保存路径。从目标格开始,沿着每一格的父节点移动直到回到起始格。这就是你的路径。
可以这样说,BFS是A*算法的一个特例。对于一个BFS算法,从当前节点扩展出来的每一个节点(如果没有被访问过的话)都要放进队列进行进一步扩展。也就是说BFS的估计函数h永远等于0,没有一点启发式的信息,可以认为BFS是“最烂的”A*算法。
选取最小估价:如果学过数据结构的话,应该可以知道,对于每次都要选取最小估价的节点,应该用到最小优先级队列(也叫最小二叉堆)。在C++的STL里有现成的数据结构priorityqueue,可以直接使用。当然不要忘了重载自定义节点的比较操作符。
Memory | Time |
---|---|
154K | 47MS |
不过上面优化的双向BFS(16MS)
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdio>
#include<stdlib.h>
using namespace std;
char ss[3],ee[3];
int d[8][2]={{-2,1},{-2,-1},{-1,-2},{-1,2},{2,-1},{2,1},{1,-2},{1,2}};
int vis[8][8];
typedef struct node
{
int x,y;
int step;
int g,h,f;
friend bool operator <(const node &a,const node &b);
}node;
node s,e;
inline bool operator < (const node &a,const node &b)
{
return a.f>b.f;
}
bool in(const node & a)//判断knight是否在棋盘内
{
if(a.x<0 || a.y<0 || a.x>=8 || a.y>=8)
return false;
return true;
}
int Heuristic(const node &a)//曼哈顿(manhattan)估价函数
{
return (abs(a.x-e.x)+abs(a.y-e.y))*10;
}
priority_queue<node> que;
int Astar()
{
while(!que.empty()) que.pop();
memset(vis,0,sizeof(vis));
que.push(s);
node front,t;
while(!que.empty())
{
front = que.top();
que.pop();
vis[front.x][front.y] = 1;
if(front.x==e.x && front.y==e.y)
return front.step;
for(int i=0;i<8;i++)
{
t.x = front.x + d[i][0];
t.y = front.y + d[i][1];
if(in(t) && !vis[t.x][t.y])
{
t.g = 23 + front.g;
t.h = Heuristic(t);
t.f = t.g + t.h;
t.step = front.step + 1;
que.push(t);
}
}
}
}
int main()
{
while(scanf("%s %s",ss,ee)==2)
{
s.x = ss[0]-'a'; s.y = ss[1]-'1';
e.x = ee[0]-'a'; e.y = ee[1]-'1';
s.step = 0;
s.g = 0;
s.h = Heuristic(s);
s.f = s.g + s.h;
if(s.x==e.x && s.y==e.y)
printf("To get from %s to %s takes 0 knight moves.\n",ss,ee);
else
printf("To get from %s to %s takes %d knight moves.\n",ss,ee,Astar());
}
return 0;
}