题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
思路:8个方向依次入队即可,只需标记访问,广度优先搜索是按照层次来搜索,如果存在一条通路,那么自然是最短路了。
AC代码:62MS 352K,还可以优化。
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include <fstream>
using namespace std;
struct node{
int x,y;
int step;
}p,q;
queue<node> Q;
int dir[8][2]={-2,-1,-2,1,-1,-2,-1,2,1,-2,1,2,2,-1,2,1};
int sx,sy,endx,endy;
bool visit[10][10];
bool isbond(node &a){
if(a.x<0 || a.x>7 || a.y<0 || a.y>7)return 1;
return 0;
}
int bfs(){
memset(visit,0,sizeof(visit));
while(!Q.empty())Q.pop();
p.step=0;
visit[p.x][p.y]=1;
Q.push(p);
if(p.x==endx&&p.y==endy){
return 0;
}
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(p.x==endx&&p.y==endy)return p.step;
for(int i=0;i<8;i++)
{
q.x=p.x+dir[i][0];
q.y=p.y+dir[i][1];
if(isbond(q))continue;
if(visit[q.x][q.y])continue;
q.step=p.step+1;
visit[q.x][q.y]=1;
Q.push(q);
}
}
}
int main()
{
string a,b;
// ifstream fin;
// fin.open("aaa.txt");
while(cin>>a>>b)
{
p.x=a[0]-'a';
p.y=a[1]-'1';
endx=b[0]-'a';
endy=b[1]-'1';
cout<<"To get from "<<a<<" to "<<b<<" takes ";
cout<<bfs()<<" knight moves."<<endl;
}
return 0;
}