hdu1372 Knight Moves(BFS)
来源:互联网 发布:网络教育学历有用吗 编辑:程序博客网 时间:2024/05/16 08:22
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5519 Accepted Submission(s): 3370
Problem 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<stdio.h>#include<string.h>int mov[8][2]={-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};bool vis[10][10];char ss[5][5];int s[5][5],dist;struct maze{int x,y,dis;}q[70];int main(){int i,j,nx,ny,fro,rea;while(scanf("%s%s",ss[0],ss[1])!=EOF){s[0][0]=ss[0][0]-'a';s[0][1]=ss[0][1]-'1';s[1][0]=ss[1][0]-'a';s[1][1]=ss[1][1]-'1';if(strcmp(ss[0],ss[1])==0) { printf("To get from %s to %s takes 0 knight moves.\n",ss[0],ss[1]); continue; }memset(vis,0,sizeof(vis));maze tem1,tem2;fro=0;rea=1;tem1.x=s[0][0];tem1.y=s[0][1];tem1.dis=0;vis[s[0][0]][s[0][1]]=1;q[0]=tem1;int ed=0;while(fro<rea&&!ed){tem1=q[fro++];for(i=0;i<8;i++){nx=tem1.x+mov[i][0];ny=tem1.y+mov[i][1];if(nx>=0&&nx<8&&ny>=0&&ny<8&&!vis[nx][ny]){tem2.x=nx;tem2.y=ny;tem2.dis=tem1.dis+1;if(nx==s[1][0]&&ny==s[1][1]){dist=tem2.dis;ed=1;break;}vis[nx][ny]=1;q[rea++]=tem2;}}}printf("To get from %s to %s takes %d knight moves.\n",ss[0],ss[1],dist);}return 0;}
0 0
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- HDU1372 Knight Moves(BFS)
- HDU1372 Knight Moves(bfs)
- HDU1372(BFS)Knight Moves
- HDU1372 Knight Moves(bfs)
- Knight Moves hdu1372 bfs
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves BFS
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- hdu1372-Knight Moves BFS
- HDU1372 Knight Moves (BFS)
- ghost误操作,整个硬盘只剩一个C盘
- Linux下Pdf Studio的破解
- Google C++编程风格指南
- HDU1867-KMP算法的应用
- poj 2892 hdu1540 线段树 求过某点的最大连续区间。
- hdu1372 Knight Moves(BFS)
- Python_C3_变量【上】
- WebLogin 创建域
- html 弹出div移动居中(好用版)
- iframe的src赋值问题(服务器端)
- 数据库的并发操作
- 基础加强____【集合框架体系个人总结】
- 6sigma 基本概念
- redis安装,redis c++客户端,redis cluster集群测试