Knight Moves - UVa 439 搜索
来源:互联网 发布:toupview显微图像软件 编辑:程序博客网 时间:2024/06/04 19:01
Knight Moves
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.
Input Specification
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 Specification
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.
题意:对于一个马,从一个位置到另一个位置至少需要走多少步。
思路:直接广搜。
AC代码如下:
#include<cstdio>#include<cstring>using namespace std;char s1[10],s2[10];int n,m,xx,yy,ox[8]={-2,-1,1,2,2,1,-1,-2},oy[8]={1,2,2,1,-1,-2,-2,-1};int vc[110][110][2],vis[110][110],t,ans;bool flag;void solve(int x,int y,int pos){ if(x==xx && y==yy) { flag=true; ans=pos; return; } int i,j; for(i=0;i<8;i++) if(x+ox[i]>0 && x+ox[i]<=8 && y+oy[i]>0 && y+oy[i]<=8 && vis[x+ox[i]][y+oy[i]]!=t) { vis[x+ox[i]][y+oy[i]]=t; vc[pos+1][0][0]++; vc[pos+1][vc[pos+1][0][0]][0]=x+ox[i]; vc[pos+1][vc[pos+1][0][0]][1]=y+oy[i]; }}int main(){ int i,j,k,x,y; while(~scanf("%s%s",s1,s2)) { t++; x=s1[0]-'a'+1; y=s1[1]-'0'; xx=s2[0]-'a'+1; yy=s2[1]-'0'; flag=false; vis[x][y]=t; for(i=1;i<=100;i++) vc[i][0][0]=0; vc[0][0][0]=1; vc[0][1][0]=x; vc[0][1][1]=y; for(i=0;i<=100;i++) { if(flag) break; for(j=1;j<=vc[i][0][0];j++) { if(flag) break; solve(vc[i][j][0],vc[i][j][1],i); } } printf("To get from %s to %s takes %d knight moves.\n",s1,s2,ans); }}
- Knight Moves - UVa 439 搜索
- UVa 439 - Knight Moves 搜索专题
- uva 439 knight moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva-439 - Knight Moves
- UVa 439 - Knight Moves
- UVa 439 - Knight Moves
- UVA 439 - Knight Moves
- uva 439 Knight Moves
- UVa 439 - Knight Moves
- uva 439 - Knight Moves
- UVa:439 Knight Moves
- uva 439 - Knight Moves
- uva 439 Knight Moves
- UVa 439 - Knight Moves
- 谫兴壳湛蠢临霖缕展袄袄男
- CF437div D 贪心+并查集
- oncreatedialog()与showdialog()的使用
- 币固谜滋嗜回悔玫赋必芽笨
- AAM详解
- Knight Moves - UVa 439 搜索
- 隐马尔科夫模型(HMM)详解
- 黑马程序员——C语言中的一些小问题
- 64位Ubuntu下运行某程序时碰到找不到动态库的问题
- 获取显示窗口警告 android The method getWidth() from the type Display is deprecated
- 每个程序员都应读的书
- [Windows]_[删除非空目录的注意要点]
- JAVA数据结构学习
- 泻我厝刭持蔽猿和甘映卜细