Uva 439 - Knight Moves
来源:互联网 发布:h5 webapp源码下载 编辑:程序博客网 时间:2024/06/05 21:04
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
Output
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>using namespace std;int map[10][10],sx,sy,ex,ey;int dx[]={-1,-2,-2,-1,1,2,2,1};int dy[]={-2,-1,1,2,2,1,-1,-2};string a,b;void print(){for(int i=1;i<=8;i++){for(int j=1;j<=8;j++){cout<<map[i][j]<<" ";}cout<<endl;}}int avil(int x,int y){if(map[x][y]==-1||x>8||x<=0||y>8||y<=0) return 0;else return 1;}void tre(string a,string b){sx=9-(a[1]-'0'),ex=9-(b[1]-'0');if(a[0]=='a') sy=1;else if(a[0]=='b') sy=2;else if(a[0]=='c') sy=3;else if(a[0]=='d') sy=4;else if(a[0]=='e') sy=5;else if(a[0]=='f') sy=6;else if(a[0]=='g') sy=7;else if(a[0]=='h') sy=8;if(b[0]=='a') ey=1;else if(b[0]=='b') ey=2;else if(b[0]=='c') ey=3;else if(b[0]=='d') ey=4;else if(b[0]=='e') ey=5;else if(b[0]=='f') ey=6;else if(b[0]=='g') ey=7;else if(b[0]=='h') ey=8;}void bfs(){int A[65][3];int head=1,tail=1;map[sx][sy]=-1;A[head][0]=sx,A[head][1]=sy,A[head][2]=0;tail++;while(head<tail){head++;int tempx,tempy;for(int i=0;i<=7;i++){tempx=dx[i]+A[head-1][0];tempy=dy[i]+A[head-1][1];if(avil(tempx,tempy)==1){A[tail][0]=tempx,A[tail][1]=tempy,A[tail][2]=A[head-1][2]+1;map[tempx][tempy]=-1;//cout<<"("<<A[tail][0]<<","<<A[tail][1]<<")"<<tail<<" "<<A[tail][2]<<endl;if(tempx==ex&&tempy==ey){cout<<"To get from "<<a<<" to "<<b<<" takes "<<A[tail][2]<<" knight moves."<<endl;}tail++;}}}}int main(){//freopen("input.txt","r",stdin);while(cin>>a>>b){tre(a,b);memset(map,0,sizeof(map));//print();//cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<endl;if(a==b) cout<<"To get from "<<a<<" to "<<b<<" takes 0 knight moves."<<endl;else bfs();}return 0;}
一.分析
本体就是一道很简单的bfs
- 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
- UVA 439 - Knight Moves
- 去除字符串中的某个字符
- SQL 错误 [1476] [22012]: ORA-01476: 除数为 0
- Windows7 下Tomcat多实例部署
- 我的maven学习(一)
- Caused by: java.lang.ClassNotFoundException[android的终极解决错误]
- Uva 439 - Knight Moves
- unity序列化系统小结-来自unity手册(Script Serialization)
- sas attrib _all_ label=""
- Ubuntu 14.04 安装 WPS
- 《Maven实战》整理
- git学习——上传项目代码到github
- iOS:苹果企业证书通过网页分发安装app
- iOS 搜索功能实现
- Android学习第九篇——2D动画翻转