HDU 1372Knight Moves(bfs)
来源:互联网 发布:铁路 纪录片 知乎 编辑:程序博客网 时间:2024/05/29 17:24
Knight Moves
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
Source
University of Ulm Local Contest 1996
代码:
#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct node{ int x,y,step;};char s[5],s1[5];int vis[10][10];int to[8][2]= {-2,1,-1,2,1,2,2,1,2,-1,1,-2,-1,-2,-2,-1};int n,m,sx,sy,ex,ey,ans;int check(int x,int y){ if(x<0 || x>=8 || y<0 || y>=8||vis[x][y]) return 1; return 0;}int bfs(){ int i; memset(vis,0,sizeof(vis)); queue<node> Q; node a,next,p; a.x = s[0]-'a'; a.y = s[1]-'1'; ex=s1[0]-'a'; ey=s1[1]-'1'; a.step = 0; vis[a.x][a.y]=1; Q.push(a); while(!Q.empty()) { p = Q.front(); Q.pop(); if(p.x==ex&&p.y==ey) { return p.step; } for(i = 0; i<8; i++) { next.x=p.x+to[i][0]; next.y=p.y+to[i][1]; if(next.x==ex&&next.y==ey) { return p.step+1; } if(check(next.x,next.y)) continue; next.step=p.step+1; vis[next.x][next.y] = 1; Q.push(next); } } return 0;}int main(){ while(scanf("%s %s",s,s1)!=EOF) { printf("To get from %s to %s takes %d knight moves.\n",s,s1,bfs()); } return 0;}
阅读全文
0 0
- 【hdu 1372】 Knight Moves (BFS)
- hdu 1372 Knight Moves (水bfs)
- hdu 1372 Knight Moves(BFS)
- Hdu 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- hdu 1372 Knight Moves(bfs)
- HDU-#1372 Knight Moves(双向BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(bfs)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- hdu 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves (BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372 Knight Moves(BFS)
- HDU 1372Knight Moves(bfs)
- 【HDU 1372 Knight Moves(BFS)】
- 2017 ACM-ICPC乌鲁木齐网络赛 G. Query on a string 【KMP+树状数组】
- POJ 1873 The Fortified Forest(凸包+状态枚举)
- S5PV210 启动流程
- 顺序表应用2:多余元素删除之建表算法
- 设计模式享元模式
- HDU 1372Knight Moves(bfs)
- gradle和gradle wrapper
- java环境安装
- dedescms 标签
- LeetCode #241 Different Ways to Add Parentheses
- Just a Hook (区间更新)
- 脸型分类-Face shape classification using Inception v3
- M
- TensorFlow-mnist-学习-2