您的位置:首页 > 其它

POJ-2243-Knight Moves

2013-05-21 18:45 225 查看
BFS题~

代码:

#include<cstdio>
#include<cstring>
#include<queue>
#define MAX 10
#define inf 10000
using namespace std;
int xa,xb,ya,yb,ans;
int runx[10]={1,1,-1,-1,2,2,-2,-2},runy[10]={2,-2,2,-2,1,-1,1,-1};
queue<int> qx,qy;
int Is(int x,int y)
{
if(x<1||x>8||y<1||y>8)
return 1;
return 0;
}
void BFS(int x,int y)
{
int coua=1,coub=0;
qx.push(x);
qy.push(y);
ans=1;
while(1)
{
for(int i=0;i<coua;i++)
{
int xx=qx.front();
qx.pop();
int yy=qy.front();
qy.pop();
for(int j=0;j<8;j++)
{
int ita,itb;
ita=xx+runx[j];
itb=yy+runy[j];
if(Is(ita,itb))
continue;
if(ita==xb&&itb==yb)
return;
qx.push(ita);
qy.push(itb);
coub++;
}
}
coua=coub;
coub=0;
ans++;
}
}
int main()
{
char stra[5],strb[5];
while(scanf("%s%s",stra,strb)!=EOF)
{
xa=stra[0]-'a'+1;
ya=stra[1]-'0';
xb=strb[0]-'a'+1;
yb=strb[1]-'0';
ans=0;
if(!(xa==xb&&ya==yb))
BFS(xa,ya);
printf("To get from %s to %s takes %d knight moves.\n",stra,strb,ans);
while(!qx.empty())
qx.pop();
while(!qy.empty())
qy.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: