Knight Moves
来源:互联网 发布:阿里云架设服务器 编辑:程序博客网 时间:2024/04/30 23:01
Problem 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 file 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 e4a1 b2b2 c3a1 h8a1 h7h8 a1b1 c3f6 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
University of Ulm Local Contest 1996
Recommend
Eddy
#include<iostream>
#include<string.h>
#include<queue>
using namespace std;
int direction[8][2]= { -2, 1, 2, 1, 2, -1, -2, -1, 1, 2, 1, -2, -1, 2, -1, -2 };
bool isvisit[9][9];
bool cross(int a,int b)
{
if(a>=1&&a<=8&&b>=1&&b<=8&&!isvisit[a][b])
{
return true;
}
return false;
}
typedef struct node{
int x;
int y;
int count;
}Node;
int s1,s2,d1,d2;
char str1[2],str2[2];
void bfs()
{
Node start={s1,s2,0};
queue<Node> myqueue;
myqueue.push(start);
isvisit[s1][s2]=1;
while(!myqueue.empty())
{
Node temp=myqueue.front();
myqueue.pop();
int prex=temp.x;
int prey=temp.y;
int precount=temp.count;
if(prex==d1&&prey==d2)
{
printf("To get from %s to %s takes %d knight moves.\n",str1,str2,precount);
}
for(int i=0;i<8;i++)
{
int curx=prex+direction[i][0];
int cury=prey+direction[i][1];
if(cross(curx,cury))
{
int curcount=precount+1;
Node now={curx,cury,curcount};
isvisit[curx][cury]=1;
myqueue.push(now);
}
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
int i=0;
while(scanf("%s%s",str1,str2)!=EOF)
{
s1=(str1[0]-'a');
s1++;
s2=str1[1]-'0';
d1=(str2[0]-'a');
d1++;
d2=str2[1]-'0';
memset(isvisit,false,sizeof(isvisit));
bfs();
}
return 0;
}
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- Knight Moves
- read 与 fread 的区别的误解(转载)
- PKU1679(The Unique MST)判断最小生成树的唯一性-次小生成树
- Ant学习总结
- 一念成佛,一念成魔
- flickr 对于分布式系统生成全局唯一ID的解决方案
- Knight Moves
- android 横竖屏模式的设置
- IPv6 troubleshooting
- altera 官方FPGA视频教程下载
- hau2188
- java 调用url打开浏览器显示网页
- 调用Emgu中函数进行高斯背景建模
- xx is not in the sudoers file 问题解决
- 月黑风高