您的位置:首页 > 其它

uva 439 Knight Moves

2015-12-15 18:44 435 查看
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.

/[b]******************************[/b]

输入标准8*8国际象棋棋盘上的两个格子(列为a~h,行为1~8),求马最少要多少步从起点跳到终点,马的移动方式为走“日”子的对角线,如a1到b3

即求最短路

/[b]******************************[/b]

//#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 bfs