uva 439(bfs)
来源:互联网 发布:php curl 请求头 编辑:程序博客网 时间:2024/06/05 20:33
题目:
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 e4
a1 b2
b2 c3
a1 h8
a1 h7
h8 a1
b1 c3
f6 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.
题意:可以看成一个8*8的数组,有一个人从起始点以中国象棋中‘马’的走法,就是走‘日’字,来寻找目标点,求出他的最短路径。╮(╯▽╰)╭
#include <cstdio>#include <cstring>#include <queue>using namespace std;int x1,x2,y1,y2;int vie[8][9];int flagx[8]={2,-2,2,-2,1,-1,1,-1};int flagy[8]={1,1,-1,-1,2,2,-2,-2};struct P{ int x; int y;}p;queue<P> q;void bfs(){ int x0,y0; while(!q.empty()) { for(int i=0;i<8;i++) { x0=x1+flagx[i]; y0=y1+flagy[i]; if(x0<0||x0>=8||y0<0||y0>=8) continue; if(vie[x0][y0]>=1) continue; vie[x0][y0]=vie[x1][y1]+1; p.x=x0; p.y=y0; q.push(p); if(x0==x2&&y0==y2) return; } q.pop(); p=q.front(); x1=p.x; y1=p.y; }}int main(){ char a,b,c,d; while(scanf("%c%c %c%c",&a,&b,&c,&d)!=EOF) { getchar(); x1=a-'a'; y1=b-'1'; x2=c-'a'; y2=d-'1'; memset(vie,0,sizeof(vie)); p.x=x1; p.y=y1; while(!q.empty()) q.pop(); q.push(p); if(p.x!=x2||p.y!=y2) bfs(); printf("To get from %c%c to %c%c takes %d knight moves.\n",a,b,c,d,vie[x2][y2]); } return 0;}
- uva 439(bfs)
- UVA 439 bfs搜索
- UVA 439 BFS入门
- UVa 439 Knight Moves (bfs)
- UVA 439 Knight Moves (BFS)
- UVA 439 Knight Moves(BFS)
- UVA 439-Knight Moves(bfs)
- UVA 439Knight Moves 基础BFS
- uva 439 Knight Moves(BFS)
- UVA 439 Knight Moves --DFS or BFS
- uva 439 Knight Moves(BFS遍历)
- UVa 439 Knight Moves(BFS应用)
- UVa 439 - Knight Moves (BFS)
- UVA - 439 - Knight Moves (BFS)
- hdu 1372&&uva 439 KnightMoves(bfs)
- UVa 439 Knight Moves——bfs
- [uva-439]Knight Moves(BFS)
- uva 784(bfs)
- 使用componentsSeparatedByCharactersInSet:方法限定输入字符的格式
- poj 2823 Sliding Window 单调队列
- myeclipse10配置maven
- Android菜鸟在成长-ListView1
- DetectSourceDemo
- uva 439(bfs)
- 【SQL】Oracle的内连接、左外连接、右外连接及全外连接
- 理解Linux系统/etc/init.d目录和/etc/rc.local脚本
- 自定义事件
- [生活]SAE初次体验
- hbase0.96 put流程 源代码分析
- 4.5poj2975Acesar密码
- js高级程序设计第三版 第四章
- 书写优雅的shell脚本(插曲) - kill