HDU 1372 Knight Moves
来源:互联网 发布:大数据分析人物 编辑:程序博客网 时间:2024/05/20 05:26
Knight Moves
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10362 Accepted Submission(s): 6097
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.
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 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.
水题 bfs跑一遍就行了
GNU C++略坑 G++一次过 GNU C++硬是格式错误..最后一组test完了居然不能输出\n
#include<iostream>#include<stdlib.h>#include<stdio.h>#include<string>#include<vector>#include<deque>#include<queue>#include<algorithm>#include<set>#include<map>#include<stack>#include<time.h>#include<math.h>#include<list>#include<cstring>#include<fstream>//#include<memory.h>using namespace std;#define ll long long#define ull unsigned long long#define pii pair<int,int>#define INF 1000000007#define pll pair<ll,ll>#define pid pair<int,double>//#define CHECK_TIMEstruct P{ int x,y;};int mp[9][9];int di[8][2]={{2,-1},{2,1}, {-2,-1},{-2,1}, {-1,-2},{1,-2}, {-1,2},{1,2}};int bfs(P st,P end){ for(int i=1;i<9;++i) fill(mp[i],mp[i]+9,INF); mp[st.x][st.y]=0; deque<P>de; de.push_back(st); while(mp[end.x][end.y]>=INF){ P f=de.front(); de.pop_front(); for(int i=0;i<8;++i){ P to={f.x+di[i][0],f.y+di[i][1]}; if(to.x>0&&to.x<9&&to.y>0&&to.y<9) if(mp[to.x][to.y]==INF){ mp[to.x][to.y]=mp[f.x][f.y]+1; de.push_back(to); } } } return mp[end.x][end.y];}int main(){ //freopen("/home/lu/文档/r.txt","r",stdin); //freopen("/home/lu/文档/w.txt","w",stdout); char sq[2][3]; int t=0; while(~scanf("%s",sq[0])){ if(t!=0) putchar('\n'); ++t; P st={sq[0][0]-'a'+1,sq[0][1]-'0'}; scanf("%s",sq[1]); P end={sq[1][0]-'a'+1,sq[1][1]-'0'}; printf("To get from %2s to %2s takes %d knight moves.",sq[0],sq[1],bfs(st,end)); } return 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
- 设计模式学习笔记--简单工厂模式(Simple Factory Pattern)【创建型模式】
- Linux文件目录操作命令 mv
- 第二章——类型、运算符与表达式
- jsession的作用
- Struts-Filter配置详解
- HDU 1372 Knight Moves
- P1103 校门外的树
- smarty中的在模板页中应用js css img等文件时,地址是相对与模板页对应的php文件
- 【框架-MFC】禁用鼠标拖拽标题栏移动窗口
- 移动端学习总结——性能优化
- poj2287 Tian Ji -- The Horse Racing(2016xynu暑期集训检测 -----C题)
- GitHub如何在网页面板创建文件夹
- Nginx负载均衡:分布式/热备Web Server的搭建
- Manacher's ALGORITHM: O(n)时间求字符串的最长回文子串