uva 439 Knight Moves
来源:互联网 发布:js bytebuffer 编辑:程序博客网 时间:2024/06/05 20:52
Knight Moves
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国际象棋棋盘上的两个格子(列为a~h,行为1~8),求马最少要多少步从起点跳到终点,马的移动方式为走“日”子的对角线,如a1到b3
即求最短路
/******************************
//#include <bits/stdc++.h>#include "iostream"#include "string"#include "cstdio"#include "cstring"#include "cmath"#include "map"#include "stack"#include "queue"#include "algorithm"using namespace std;typedef long long ll;const int maxn = 1e6 + 5;struct node{ //定义点 int x; int y; int d; node(int a,int b, int c) { x = a; y = b; d = c; }};int endx,endy,vis[9][9];int to[8][2] = {{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2},{-2,-1}}; //移动方式int bfs(int x,int y) { //bfs求最短路,x,y是初始位置下标 if(x == endx && y == endy) return 0; memset(vis,0,sizeof(vis)); node s(x,y,0); queue<node> q; q.push(s); while(!q.empty()) { node u = q.front();q.pop(); for(int i = 0; i < 8; i++) { int r = u.x + to[i][0]; int c = u.y + to[i][1]; if(!vis[r][c] && r > 0 && i < 9 && c > 0 && c < 9) { if(r == endx && c == endy) { return u.d + 1; } node v(r,c,u.d+1); q.push(v); vis[r][c] = 1; } } }return -1;}int main(){ ios::sync_with_stdio(false); string s1,s2; int m,n; while(cin >> s1 >> s2) { memset(vis,0,sizeof(vis)); m = (s1[0] - 'a') + 1; n = (s1[1] - '0'); endx = (s2[0] - 'a') + 1; endy = (s2[1] - '0'); cout << "To get from " <<s1 << " to " << s2 << " takes " << bfs(m,n) << " knight moves." << endl; } return 0;}
- uva 439 knight moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva 439 - Knight Moves
- uva-439 - Knight Moves
- UVa 439 - Knight Moves
- UVa 439 - Knight Moves
- UVA 439 - Knight Moves
- uva 439 Knight Moves
- UVa 439 - Knight Moves
- uva 439 - Knight Moves
- UVa:439 Knight Moves
- uva 439 - Knight Moves
- uva 439 Knight Moves
- UVa 439 - Knight Moves
- uva 439 - Knight Moves
- UVA 439 - Knight Moves
- 从零开始学算法——选择排序
- 应用数据plist存储方法
- 简单的彷spring注解初始化action
- 跳转连接oracle
- AndroidStudio 中如何查看获取MD5和SHA1值(应用签名)
- uva 439 Knight Moves
- Linux内核构造数据包并发送(Netfilter方式)
- Swift之!与?的区别
- matplotlib 基础教程
- centos 6.5 eclipse 安装svn
- 基数排序
- Genymotion模拟器的使用
- ARP原理
- hdu 1829 A Bug's Life