Knight Moves
来源:互联网 发布:数控 对零件的编程 编辑:程序博客网 时间:2024/05/01 12:39
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 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<cstdio>#include<queue>#include<cstring>using namespace std;const int dx[] = {1,1,-1,-1,2,2,-2,-2};const int dy[] = {2,-2,2,-2,1,-1,1,-1};int vis[101][101]={0};struct node{ int x; int y; int d;};int check(int x,int y){ if(x<=0||x>8||y<=0||y>8||vis[x][y]) return 0; return 1;}char s[5],g[5];int sx,sy,gx,gy;int bfs(void){ queue<node> Q; memset(vis,0,sizeof(vis)); node head,next; vis[sx][sy]=1; head.x=sx; head.y=sy; head.d=0; Q.push(head); while(!Q.empty()) { head=Q.front(); Q.pop(); if(head.x==gx&&head.y==gy) return head.d; for(int i=0;i<8;i++) { next.x=head.x+dx[i]; next.y=head.y+dy[i]; if(check(next.x,next.y)) { if(head.x==gx&&head.y==gy) return head.d+1; next.d=head.d+1; vis[next.x][next.y]=1; Q.push(next); } } }}void solve(void){ int ans = bfs(); printf("To get from %s to %s takes %d knight moves.\n", s, g, ans);}int main(){ while(cin>>s>>g) { sx=s[0]-'a'+1;//这一点输入 sy=s[1]-'1'+1; gx=g[0]-'a'+1; gy=g[1]-'1'+1; solve(); } return 0;}
0 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
- 如何区分控制台程序和MFC程序
- 详细解析oracle中left join,right join,inner join用法
- hdu 4513 KMP里next数组的运用
- iOS double类型精度问题以及计算问题
- 解释器(Interpreter)
- Knight Moves
- CocoaPods 详解之----使用篇
- 灰度图像--频域滤波 同态滤波
- Android应用优化(6)工具篇
- POJ 1942 Paths on a Grid (组合数学)
- 数据库系统的三级模式结构
- 点击LinearLayout使用selector改变TextView字体颜色
- springmvc+spring+hibernate 整合 annotation(全注解)方式
- 平面点集的最小包围圆 hdu 3932