Sicily 1936. Knight Moves

1936. Knight MovesConstraints

Time Limit: 1 secs, Memory Limit: 32 MB

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

There are multiple test cases. The first line contains an integer T, indicating the number of 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 Input8e2 e4 a1 b2 b2 c3 a1 h8 a1 h7 h8 a1 b1 c3 f6 f6 Sample OutputTo 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.非常蛋疼的一道题,,居然不给用gets!

原来的代码:0.01s

#include <queue>#include <iostream>#include <stdio.h>#include <map>#include <algorithm>#include <string>#include <string.h>using namespace std;#define MAX 8bool map[MAX][MAX];bool vis[MAX][MAX];int all_ans[MAX * MAX + 5][MAX * MAX + 5];struct point {int ii;int jj;};point start, end;bool the_same_point(point a, point b) {return a.ii == b.ii && a.jj == b.jj;}void output(int ans) {printf("To get from %c%d to %c%d takes %d knight moves.\n", start.jj + 'a', start.ii + 1, end.jj + 'a', end.ii + 1, ans);all_ans[start.ii * 10 + start.jj][end.ii * 10 + end.jj] = ans;}void bfs() {memset(vis, 0, sizeof(vis));queue<point> q;q.push(start);int size, step = 0;point temp, temp_next;while (!q.empty()) {step++;size = q.size();while (size–) {temp = q.front();q.pop();if (temp.ii > 1 && temp.jj > 0 && !vis[temp.ii – 2][temp.jj – 1]) {temp_next.ii = temp.ii – 2;temp_next.jj = temp.jj – 1;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii > 1 && temp.jj < 7 && !vis[temp.ii – 2][temp.jj + 1]) {temp_next.ii = temp.ii – 2;temp_next.jj = temp.jj + 1;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii > 0 && temp.jj < 6 && !vis[temp.ii – 1][temp.jj + 2]) {temp_next.ii = temp.ii – 1;temp_next.jj = temp.jj + 2;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii < 7 && temp.jj < 6 && !vis[temp.ii + 1][temp.jj + 2]) {temp_next.ii = temp.ii + 1;temp_next.jj = temp.jj + 2;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii < 6 && temp.jj < 7 && !vis[temp.ii + 2][temp.jj + 1]) {temp_next.ii = temp.ii + 2;temp_next.jj = temp.jj + 1;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii < 6 && temp.jj > 0 && !vis[temp.ii + 2][temp.jj – 1]) {temp_next.ii = temp.ii + 2;temp_next.jj = temp.jj – 1;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii < 7 && temp.jj > 1 && !vis[temp.ii + 1][temp.jj – 2]) {temp_next.ii = temp.ii + 1;temp_next.jj = temp.jj – 2;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}if (temp.ii > 0 && temp.jj > 1 && !vis[temp.ii – 1][temp.jj – 2]) {temp_next.ii = temp.ii – 1;temp_next.jj = temp.jj – 2;if (the_same_point(temp_next, end)) {output(step);}q.push(temp_next);vis[temp_next.ii][temp_next.jj] = true;}}}}int main() {int case_num;scanf("%d\n", &case_num);char temp[5];while (case_num–) {scanf("%s", temp);start.ii = temp[1] – '0' – 1;start.jj = temp[0] – 'a';scanf("%s", temp);end.ii = temp[1] – '0' – 1;end.jj = temp[0] – 'a';if (the_same_point(start, end)) {output(0);} else {bfs();}}return 0;}

流转的时光,都成为命途中美丽的点缀,

Sicily 1936. Knight Moves

相关文章:

你感兴趣的文章:

标签云: