HDU1372 Knight Moves
来源:互联网 发布:战地2 for mac 百度云 编辑:程序博客网 时间:2024/05/17 02:01
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
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;int step;int visit[10][10];int to[8][2]={-1,2,1,2,-2,1,2,1,-1,-2,-2,-1,1,-2,2,-1};//象棋可走的8个方向int ex,ey;char a1,a2;int b1,b2;struct node{ int x,y,step;};int check(int x,int y){ if(x<0||x>7||y<0||y>7||visit[x][y]==1) return 1; return 0;}int bfs(){ int i; queue<node>Q; node p,q,next; q.x=a1-'a'; q.y=b1-1; q.step=0; ex=a2-'a'; ey=b2-1; memset(visit,0,sizeof(visit)); visit[q.x][q.y]=1; Q.push(q); 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=to[i][0]+p.x; next.y=to[i][1]+p.y; if(next.x==ex&&next.y==ey) return p.step+1; if(check(next.x,next.y)) continue; next.step=p.step+1; visit[next.x][next.y]=1; Q.push(next); } } return 0;}int main(){ while(scanf("%c%d %c%d",&a1,&b1,&a2,&b2)!=EOF) { printf("To get from %c%d to %c%d takes %d knight moves.\n",a1,b1,a2,b2,bfs()); getchar(); } return 0;}
0 0
- Knight Moves hdu1372 bfs
- hdu1372 Knight Moves
- hdu1372-Knight Moves--搜索
- HDU1372:Knight Moves
- hdu1372题目Knight Moves
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves
- hdu1372 Knight Moves (BFS)
- HDU1372 Knight Moves
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves
- hdu1372 Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves BFS
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- Knight Moves hdu1372
- HDU1372----Knight Moves
- [LNMPA搭建四]Apache详细安装步骤
- 伸展树(splay)
- python中的setattr的使用
- mac El Capitan+ brew update error 无法访问
- 所谓数据结构
- HDU1372 Knight Moves
- hdu1044 Collect More Jewels —— dfs + bfs
- vs如何运行外部 C++ 文件
- NYOJ小白的成长之路 A+B problem
- Betheme 主题,超强大的wordpress建站主题,很适合企业站
- 蓝桥杯寒假任务之矩阵乘法
- 有效的括号序列
- hdu1045 Fire Net —— dfs
- ubuntu16.04 Atom编辑器配置python环境