http://poj.org/problem?id=2243
Knight Moves
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8713 | Accepted: 4999 |
Description
A friend of you is doing research on the Traveling Knight Problem (TKP) where you are to find the shortest closed tour of knight moves that visits each square of a given set of n squares on a chessboard exactly once. He thinks that the most difficult part of the problem is determining the smallest number of knight moves between two given squares and that, once you have accomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Of course you know that it is vice versa. So you offer him to write a program that solves the "difficult" part.
Your job is to write a program that takes two squares a and b as input and then determines the number of knight moves on a shortest route from a to b.
Input
The input will contain one or more test cases. Each test case consists of one line containing two squares separated by one space. A square is a string consisting of a letter (a-h) representing the column and a digit (1-8) representing the row on the chessboard.
Output
For each test case, print one line saying "To get from xx to yy takes n knight moves.".
Sample Input
e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6
Sample Output
To get from e2 to e4 takes 2 knight moves. To get from a1 to b2 takes 4 knight moves. To get from b2 to c3 takes 2 knight moves. To get from a1 to h8 takes 6 knight moves. To get from a1 to h7 takes 5 knight moves. To get from h8 to a1 takes 6 knight moves. To get from b1 to c3 takes 1 knight moves. To get from f6 to f6 takes 0 knight moves.
Source
题意:和poj1915一样。求从起点到终点的最小步数,毫无疑问bfs。。这种题我应该马上敲出来的,嗨。。。我个2货。。。。
#include<iostream>
#include<string>
using namespace std;
#define max 10
int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int vis[max][max],step[max][max],sx,sy,ex,ey;
int xx[100],yy[100];
void bfs(int i,int j)
{
int front,rear,k,x,y;
front=0;rear=0;
vis[i][j]=1; //标记已访问
step[i][j]=0; //步数初始为0
xx[rear]=i; //进队列
yy[rear++]=j;
while(rear>front)
{
i=xx[front]; //提取对头元素
j=yy[front++];
for(k=0;k<8;k++) //向8个方向搜索
{
x=i+dir[k][0]; //下一个点的坐标
y=j+dir[k][1];
if(!vis[x][y]&&x>0&&x<=8&&y>0&&y<=8)//注意不要踏出棋盘&&没有访问过
{
vis[x][y]=1;
step[x][y]=step[i][j]+1; //不是+1
xx[rear]=x; //把点 入队列
yy[rear++]=y;
//printf("%d%d ",x,y);
}
}
}
}
int main()
{
string s1,s2;//从字符串中分离出字母和数字,注意技巧
while(cin>>s1>>s2)
{
memset(vis,0,sizeof(vis));
memset(step,0,sizeof(step));
sx=s1[0]-'a'+1;
//起点
sy=s1[1]-'0';
ex=s2[0]-'a'+1; //终点
ey=s2[1]-'0';
//printf("%d%d%d%d",sx,sy,ex,ey);
if(sx==ex&&sy==ey)
{
cout<<"To get from "<<s1<<" to "<<s2<<" takes 0 knight moves."<<endl;
continue;
}
else
{
bfs(sx,sy);
cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<step[ex][ey]<<" knight moves."<<endl;
}
}
return 0;
}
#include<string>
using namespace std;
#define max 10
int dir[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};
int vis[max][max],step[max][max],sx,sy,ex,ey;
int xx[100],yy[100];
void bfs(int i,int j)
{
}
int main()
{
}