poj1915 BFS
D - 广搜 基础
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
Background
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
![](http://7xjob4.com1.z0.glb.clouddn.com/d2b26809455ce72d961db3fab309bf1a)
Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him?
The Problem
Your task is to write a program to calculate the minimum number of moves needed for a knight to reach one point from another, so that you have the chance to be faster than Somurolov.
For people not familiar with chess, the possible knight moves are shown in Figure 1.
Input
The input begins with the number n of scenarios on a single line by itself.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Next follow n scenarios. Each scenario consists of three lines containing integer numbers. The first line specifies the length l of a side of the chess board (4 <= l <= 300). The entire board has size l * l. The second and third line contain pair of integers {0, ..., l-1}*{0, ..., l-1} specifying the starting and ending position of the knight on the board. The integers are separated by a single blank. You can assume that the positions are valid positions on the chess board of that scenario.
Output
For each scenario of the input you have to calculate the minimal amount of knight moves which are necessary to move from the starting point to the ending point. If starting point and ending point are equal,distance is zero. The distance must be written on a single line.
Sample Input
3 8 0 0 7 0 100 0 0 30 50 10 1 1 1 1
Sample Output
5 28 0
代码;
/*
简单BFS,寻找最短路径长度
*/
#include <iostream>
#include <queue>
#include <stack>
#include <algorithm>
#include <queue>
#include <stack>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const double eps=1e-8;
const double pi=acos(-1.0);
int m[305][305];//用来标记,避免重复,bfs常见剪枝
int ax,ay,bx,by;
struct nod
{
int x,y;
int step;
};
int f[8][2]={{-2,1},{-2,-1},{-1,2},{-1,-2},{1,2},{1,-2},{2,1},{2,-1}};
queue<nod> q;
int ans;
int l;
void bfs()
{
nod t;
while(!q.empty())
{
t=q.front();
q.pop();
if(t.x==bx&&t.y==by)
{
ans=t.step;
return;
}
for(int i=0;i<8;i++)
{
int xx=t.x+f[i][0];
int yy=t.y+f[i][1];
nod n;
n.x=xx,n.y=yy,n.step=t.step+1;
if(xx>=0&&xx<l&&yy>=0&&yy<l&&m[n.x][n.y]==0)
{q.push(n);m[n.x][n.y]=1;}//在放入队列时就要标记,而不是吧每一次对队列头部进行标记,我又sb了
}
}
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&l);
memset(m,0,sizeof(m));
scanf("%d%d%d%d",&ax,&ay,&bx,&by);
nod k;
k.x=ax,k.y=ay,k.step=0;
q.push(k);
m[ax][ay]=1;
bfs();
cout<<ans<<endl;
while(!q.empty())
q.pop();//对于有多组数据情况,一定要记得清空队列
}
return 0;
}