#include<bits/stdc++.h>
using namespace std;
int Vis[205][205];
const int inf=0x3f3f3f3f;
int minhh(int x,int y)
{
if(x<=y)return x;
else return y;
}
struct glass
{
int x,y,steps;
}gg;
int main()
{
int x1,x2,y1,y2,t,x,y;
char a,b;
queue<struct glass>q;
while(scanf("%c %d %c %d",&a,&y1,&b,&y2)!=EOF)
{
gg.y=y1;t=a;x1=t;gg.x=x1-96;//注意这里的变量类型转换
t=b;x2=t-96;
gg.steps=0;
memset(Vis,0,sizeof(Vis));
q.push(gg);
while(!q.empty())
{
gg=q.front();
q.pop();
if(gg.x==x2&&gg.y==y2){
break;}
if(Vis[gg.x][gg.y]==0)
{
Vis[gg.x][gg.y]=1;
x=gg.x;y=gg.y;
for(t=-2;t<=2;t++)
{
if(t==0)t++;
gg.steps++;gg.x+=t;if((t+4)%2)gg.y+=2;else gg.y+=1;//注意对应关系
if(gg.x>=1&&gg.x<=8&&gg.y>=1&&gg.y<=8)q.push(gg);
gg.x=x;gg.y=y;
gg.x+=t;if((t+4)%2)gg.y-=2;else gg.y-=1;
if(gg.x>=1&&gg.x<=8&&gg.y>=1&&gg.y<=8)q.push(gg);
gg.x=x;gg.y=y;gg.steps--;
}
}
}
printf("To get from %c%d to %c%d takes %d knight moves.\n",a,y1,b,y2,gg.steps);
getchar();//清空输入流,不然会输出错误
while(!q.empty())//清空队列
{
q.pop();
}
}
return 0;
}
总结
1.scanf有时候需要提前清空输入流