单向BFS需要搜索N层才能到达终点,在每个层需要进行的判断量(即通常的那个for循环)为X。那么,单BFS的运算量为:X^N。
如果换成双BFS,那么前后各搜索 N/2层,那么总的运算量为:2 * ( X ^ ( N/2 ) )。显然当X比较大时,在运算量上不仅仅不仅仅是减半那么简单。特别的,如果X=1,那么双BFS也就退化成了单向BFS了,实际上,此时也就是可以用DFS来进行深搜了,而且代码相对来说更加简洁。
poj 2243
Knight Moves
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 14679 Accepted: 8226
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves.
To get from a1 to b2 takes 4 knight moves.
To get from b2 to c3 takes 2 knight moves.
To get from a1 to h8 takes 6 knight moves.
To get from a1 to h7 takes 5 knight moves.
To get from h8 to a1 takes 6 knight moves.
To get from b1 to c3 takes 1 knight moves.
To get from f6 to f6 takes 0 knight moves.
双向BFS:
#include
#include
#include
#include
using namespace std;
struct knight
{
int x,y,step;
};
int dir[8][2]={{-2,-1},{-2,1},{2,-1},{2,1},{-1,-2},{-1,2},{1,-2},{1,2}};
int sx, sy, ex, ey;
int visit[8][8];
int color[8][8];
int bfs();
int main()
{
int x1, x2;
char y1, y2;
while(scanf("%c%d %c%d", &y1, &x1, &y2, &x2) != EOF)
{
getchar();
sx = x1 - 1;
sy = y1 - 'a';
ex = x2 - 1;
ey = y2 - 'a';
memset(visit, -1, sizeof(visit));
memset(color, 0, sizeof(color));
int cost = bfs();
printf("To get from %c%d to %c%d takes %d knight moves.\n", y1, x1, y2, x2, cost);
}
return 0;
}
int bfs()
{
if(sx == ex && sy == ey)
return 0;
queue que_front;
queue que_back;
knight front, back;
front.x = sx; front.y = sy; front.step = 0;
back.x = ex; back.y = ey; back.step = 1;
que_front.push(front);
que_back.push(back);
visit[sx][sy] = 0;
visit[ex][ey] = 1;
color[sx][sy] = 1;
color[ex][ey] = 2;
int ans1 = 0, ans2 = 0;
while(!que_front.empty() || !que_back.empty())
{
if(!que_front.empty())
{
front = que_front.front();
que_front.pop();
for(int i = 0; i < 8; i++)
{
int dx = front.x + dir[i][0];
int dy = front.y + dir[i][1];
if(dx >= 0 && dx < 8 && dy >= 0 && dy < 8 && color[dx][dy] != 1)
{
if(color[dx][dy] == 0)
{
knight tmp;
tmp.x = dx; tmp.y = dy;
tmp.step = front.step + 1;
visit[dx][dy] = tmp.step;
color[dx][dy] = 1;
que_front.push(tmp);
}
else
return front.step + visit[dx][dy];
}
}
}
if(!que_back.empty())
{
back = que_back.front();
que_back.pop();
for(int i = 0; i < 8; i++)
{
int dx = back.x + dir[i][0];
int dy = back.y + dir[i][1];
if(dx >= 0 && dx < 8 && dy >= 0 && dy < 8 && color[dx][dy] != 2)
{
if(color[dx][dy] == 0)
{
knight tmp;
tmp.x = dx; tmp.y = dy;
tmp.step = back.step + 1;
visit[dx][dy] = tmp.step;
color[dx][dy] = 2;
que_back.push(tmp);
}
else
return back.step + visit[dx][dy];
}
}
}
}
return -1;
}