HDU 1372 Knight Moves
来源:互联网 发布:韩国代购淘宝店推荐 编辑:程序博客网 时间:2024/06/07 23:22
转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6011 Accepted Submission(s): 3621
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<cstdio>#include<iostream>#include<algorithm>#include<queue># define m 1000using namespace std;bool visit[m][m];struct node{ int x,step,y;};queue<node>q;int ansx;int ansy;int xx[8]={-2,-1,1,2,2,1,-1,-2};int yy[8]={1,2,2,1,-1,-2,-2,-1};node front,rear;int BFS(int x,int y){ int dx,dy,i; if(x==ansx && y==ansy) return 0; front.x=x;front.y=y;front.step=0; q.push(front); visit[x][y]=true; while(!q.empty()) { front=q.front(); q.pop(); for(i=0;i<8;i++) { dx=front.x+xx[i];dy=front.y+yy[i]; if(dx>=1 && dx<=8 && dy>=1 && dy<=8 && !visit[dx][dy]) { visit[dx][dy]=true; if(dx==ansx && dy==ansy) return front.step+1; rear.x=dx;rear.y=dy;rear.step=front.step+1; q.push(rear); } } }}int main(){ int i,ans; char a[5],b[5]; while(scanf("%s %s",a,b)!=EOF) { int x1=a[0]-'a'+1; int y1=a[1]-'0'; ansx=b[0]-'a'+1; ansy=b[1]-'0'; while(!q.empty()) q.pop(); memset(visit,false,sizeof(visit)); ans= BFS ( x1,y1); printf("To get from %s to %s takes %d knight moves.\n",a,b,ans); } return 0;}
1 0
- HDU 1372 knight moves
- HDU 1372 Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- hdu 1372 Knight Moves
- Hdu 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU 1372 Knight Moves
- HDU-1372Knight Moves
- HDU-1372-Knight Moves
- hdu 1372 Knight Moves
- hdu-1372-Knight Moves
- HDU 1372 Knight Moves
- HDU-1372-knight moves
- hdu(1372) Knight Moves
- hdu 1372 Knight Moves
- hdu 1372 Knight Moves
- 设计模式总结(二)
- java wait notifyAll 生产者 消费者 BlockingDeque
- C语言 高精度乘法
- 线程
- UC编程6-目录操作和创建子进程fork()函数1
- HDU 1372 Knight Moves
- WEB安全编程(防止黑客攻击第一道关卡)
- Java Card CAP 文件组件分析 12—— Debug Component
- 灯火阑珊,谁在思念着谁?谁又在等待着谁?
- C语言 高精度除法
- VC6.0 编译器 功能 英文翻译
- 日专家评普京访华与中俄军演同期:对俄不利
- nutch-1.7-学习笔记(1)-org.apache.nutch.crawl.Injector.java-Filesystem.get()
- 将String类型转为int类型