HDU1372(BFS)Knight Moves
来源:互联网 发布:java @apect 编辑:程序博客网 时间:2024/06/05 11:11
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.
最短路,BFS,行为char型,-97,对应vis数组。
可走的路为{{1,2}, {1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};
#include <iostream>#include <cstdio>#include <queue>#include <cstring> #include <fstream>int dir[8][2] = {{1,2}, {1,-2},{-1,2},{-1,-2},{2,1},{2,-1},{-2,1},{-2,-1}};int vis[9][9];using namespace std;struct node{char hang;int lie;int roat;node(char hang, int lie, int roat):hang(hang), lie(lie), roat(roat){}};int main(){char h1, h2;int l1, l2;//freopen("in.txt", "r", stdin);while(~scanf("%c%d %c%d", &h1, &l1, &h2, &l2)){getchar();queue<node> q;memset(vis, 0, sizeof(vis));q.push(node(h1, l1, 0));vis[h1 - 97][l1] = 1;while(!q.empty()){node p = q.front();q.pop();if(p.hang == h2 && p.lie == l2){cout<<"To get from "<<h1<<l1<<" to "<<h2<<l2<<" takes "<<p.roat<<" knight moves."<<endl;break;}for(int i = 0; i < 8; i ++){char ha = p.hang + dir[i][0];int li = p.lie + dir[i][1];if(ha >='a' && ha<='h' && li>=1 && li<=8 && !vis[ha - 97][li]){vis[ha - 97][li] = 1;q.push(node(ha, li, p.roat + 1));}}}}return 0;}
0 0
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- HDU1372 Knight Moves(BFS)
- HDU1372 Knight Moves(bfs)
- HDU1372(BFS)Knight Moves
- HDU1372 Knight Moves(bfs)
- Knight Moves hdu1372 bfs
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves (BFS)
- HDU1372:Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- hdu1372 Knight Moves(BFS)
- HDU1372 Knight Moves BFS
- HDU1372:Knight Moves(BFS)
- HDU1372 Knight Moves(BFS)
- hdu1372-Knight Moves BFS
- HDU1372 Knight Moves (BFS)
- QT开发(六十六)——登录对话框的验证机制
- QT开发(六十八)——QSS应用
- C++语言学习(五)——二阶构造模式
- C++语言学习(九)——多态
- VC++开发(一)——VC++自定义注释
- HDU1372(BFS)Knight Moves
- win10 中怎么将用户密码输入验证设置为当电脑从睡眠模式中唤醒时
- 优化-拉格朗日乘子法简述
- 【英语】三月小结
- 1058. 选择题(20) python 篇
- 翻转链表
- jq全选反选批量删除
- PL/SQL学习 第二节
- centos7 yum安装apache