广搜
无语,结构体竟然犯错,害我找问题……
#include <iostream>
#include <queue>
using namespace std;
typedef struct{
char c1,c2;
}node;
node n1,n2,at;
int dx[8]={-2,-1,1,2,2,1,-1,-2};
int dy[8]={1,2,2,1,-1,-2,-2,-1};
int main()
{
node m;
int x,y;
int vis[10][10];
while(cin>>n1.c1>>n1.c2>>n2.c1>>n2.c2)
{
memset(vis,0,sizeof(vis));
queue<node> q;
q.push(n1);
vis[n1.c1-'a'][n1.c2-'1']=1;
while(!q.empty())
{
m=q.front();
q.pop();
if(m.c1==n2.c1&&m.c2==n2.c2)
break;
for(int i=0;i<8;i++)
{
x=m.c1-'a'+dx[i];
y=m.c2-'1'+dy[i];
if(x>=0&&x<8&&y>=0&&y<8&&!vis[x][y])
{
at.c1=x+'a';
at.c2=y+'1';
vis[x][y]=vis[m.c1-'a'][m.c2-'1']+1;
q.push(at);
}
}
}
printf("To get from %c%c to %c%c takes %d knight moves.\n",n1.c1,n1.c2,n2.c1,n2.c2,vis[n2.c1-'a'][n2.c2-'1']-1);
}
return 0;
}