Knight Moves
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11197 | Accepted: 6318 |
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
分析:这道题目也是BFS的水题,主要意思就是给你出发坐标,和目标坐标,然后移动路径只能跳日字格,和中国象棋中的马的走法一直,问你最快需要多少步。
//BFS 图的遍历 广度优先搜索POJ 2243 #include<cstdio> #include<queue> #include<string.h> using namespace std; struct Point{ int x; int y; int step; }; const int MAXN = 10; char a[3]; char b[3]; int stax,stay,endx,endy,step; bool visit[MAXN][MAXN]; queue<Point> pl; int dir[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{-2,-1},{-1,-2},{1,-2},{2,-1}}; int BFS(int stx,int sty,int edx,int edy){ if(stx == edx && sty == edy)return 0; Point p; p.x = stx; p.y = sty; p.step = 0; pl.push(p); visit[stx][sty] = 1; while(!pl.empty()){ Point temp = pl.front(); pl.pop(); for(int i=0;i<8;i++){ Point newP; newP.x = temp.x + dir[i][0]; newP.y = temp.y + dir[i][1]; newP.step = temp.step + 1; if(newP.x == edx && newP.y == edy){ return newP.step; } if(newP.x>0&&newP.y>0&&newP.x<=8&&newP.y<=8&&!visit[newP.x][newP.y]) pl.push(newP); visit[newP.x][newP.y] = 1; } } } int main(){ while(scanf("%s %s",a,b)== 2){ while(!pl.empty()){ pl.pop(); } a[2] = '\0'; b[2] = '\0'; memset(visit,0,sizeof(visit)); stax = (int)(a[1]-48); stay = (int)(a[0]-96); endx = (int)(b[1]-48); endy = (int)(b[0]-96); int ans = BFS(stax,stay,endx,endy); printf("To get from %s to %s takes %d knight moves.\n",a,b,ans); } return 0; }
相关推荐
该题求解从一个坐标到达另一个坐标的最短步数,移动规则需要按照题目给的方式来移动,即按照国际象棋马的走法一致。
北大 acm JudgeOnline poj1915 Knight Moves c++源代码
北大POJ2488-A Knight's Journey 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
北大POJ1159-Palindrome 解题报告+AC代码
poj分类poj分类poj分类poj分类
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
北大POJ2002-Squares 解题报告+AC代码
POJ1048,加强版的约瑟夫问题 难度中等
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
poj 1001答案
POJ2968代码有用,欢迎下载,POJ代码