解题报告
BFS水。。。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
using namespace std;
int mmap[10][10];
bool vis[10][10];
struct node
{
int x,y,step;
}; char str1[3],str2[3];
int dx[8]={1,-1,2,-2,-2,-1,1,2};
int dy[8]={2,2,1,1,-1,-2,-2,-1};
void bfs(int s1,int e1,int s2,int e2)
{
vis[s1][e1]=true;
queue<node >Q;
node next,tail;
tail.x=s1;
tail.y=e1;
tail.step=0;
Q.push(tail);
while(!Q.empty())
{
tail=Q.front();
Q.pop();
if(tail.x==s2&&tail.y==e2)
{
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,tail.step);
return ;
}
for(int i=0;i<8;i++)
{
next.x=tail.x+dx[i];
next.y=tail.y+dy[i];
if(next.x>=1&&next.x<=8&&next.y>=1&&next.y<=8&&!vis[next.x][next.y])
{
next.step=tail.step+1;
vis[next.x][next.y]=true;
Q.push(next);
}
}
}
}
int main()
{
while(~scanf("%s%s",str1,str2))
{
memset(vis,0,sizeof(vis));
int s1=str1[1]-'0';
int e1=str1[0]-'a'+1;
int s2=str2[1]-'0';
int e2=str2[0]-'a'+1;
bfs(s1,e1,s2,e2);
}
return 0;
}