Knight Moves
来源:互联网 发布:python神经网络预测 编辑:程序博客网 时间:2024/05/01 15:03
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4331 Accepted Submission(s): 2690
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
/*简单BFS*/#include<iostream>#include<queue>using namespace std;typedef struct infor {int x,y,step;}infor;int s[10][10],move[8][2]={{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}};int sx,sy,ex,ey,vi[10][10],min1;queue<infor>q;bool inmap(int x,int y){if(x>=0&&x<8&&y>=0&&y<8)return true;elsereturn false;}void BFS(){infor a;a.x=sx;a.y=sy;a.step=0;vi[a.x][a.y]=1;q.push(a);while(!q.empty()){infor b=q.front();q.pop();for(int i=0;i<8;i++){infor c=b;c.x+=move[i][0];c.y+=move[i][1];if(inmap(c.x,c.y)&&!vi[c.x][c.y]){vi[c.x][c.y]=1;c.step++;if(c.x==ex&&c.y==ey)min1=min1>c.step?c.step:min1;q.push(c);}}}}int main(){char a,b;int n,m;while(cin>>a>>n>>b>>m){memset(vi,0,sizeof(vi));min1=1000000;sx=a-'a';sy=n-1;ex=b-'a';ey=m-1;if(sx==ex&&sy==ey)cout<<"To get from "<<a<<n<<" to "<<b<<m<<" takes "<<"0"<<" knight moves."<<endl;else{BFS();cout<<"To get from "<<a<<n<<" to "<<b<<m<<" takes "<<min1<<" knight moves."<<endl;}}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
- POJ3225--Help with Intervals
- Android常用URI收藏
- php随机密码的函数
- Cocoa框架
- Redis C客户端API介绍
- Knight Moves
- java实现站内搜索
- 写个OpenCV的小程序
- hdu2255二分最优匹配KM标准模板
- 如何设计数据库(一)
- (学习Asp.net Ajax笔记一)使用 Microsoft AJAX Library 创建自定义客户端脚本之一
- 【设计模式】之十二Adapter
- sql_trace导致数据库提示错误。
- 点击文章内页不跳转