Knight Moves
来源:互联网 发布:mysql统计不重复数据 编辑:程序博客网 时间:2024/06/10 22:47
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 655 Accepted Submission(s): 470Problem 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.
国际象棋
#include <iostream>
#include <cstring>#include <string>#include <queue>using namespace std;int visit[10][10],ans;struct node{int x;int y;};void bfs(int sx,int sy,int ex,int ey){node d,D;queue<node>q;while(!q.empty())q.pop();d.x=sx,d.y=sy;q.push(d);int move[8][2]={-2,1, -2,-1, 2,-1, 2,1, 1,2, -1,2, -1,-2, 1,-2 };while(!q.empty()){d=q.front();q.pop();if(d.x==ex && d.y==ey){ans=visit[d.x][d.y];return ;}for(int i=0;i<8;i++){D.x=d.x+move[i][0];D.y=d.y+move[i][1];if(!visit[D.x][D.y] && D.x>=1 && D.y>=1 && D.x<=8 && D.y<=8){visit[D.x][D.y]=visit[d.x][d.y]+1;q.push(D);}}}}int main(){string s1,s2;while(cin>>s1>>s2){memset(visit,0,sizeof(visit));ans=0;int sx,sy,ex,ey;sx=s1[1]-'0';ex=s2[1]-'0';sy=s1[0]-'a'+1;ey=s2[0]-'a'+1;bfs(sx,sy,ex,ey);cout<<"To get from "<<s1<<" to "<<s2<<" takes "<<ans<<" knight moves."<<endl;}}
阅读全文
1 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
- sqldeveloper 数据导入导出
- Hibernate Validator验证注解说明
- 05. Ping 包分析 ❀ 数据包分析工具 Wireshark
- yAutoGUI 简介【转】
- 如何解决APICloud Studio在IOS上无法USB真机同步?
- Knight Moves
- JavaScript——鼠标滑过事件
- VC保证应用程序只有一个实例在运行
- 书籍-分布式系统常用技术及案例分析
- Collection和map
- SSD的安装配置和测试以及自己遇到的一些坑
- 剑指offer面试题18:删除链表中重复的节点-java
- 闪屏页面
- 242. Valid Anagram的C++解法