ZOJ 1091 Knight Moves(各大算法集锦)

来源:互联网 发布:金棕榈 软件 编辑:程序博客网 时间:2024/05/21 15:13

Knight Moves

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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 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.

Source: University of Ulm Local Contest 1996


前面写了一篇用A*算法解决此题,不过是为了讲解A*算法而写的,显然本题正统解决有三种:深搜,广搜,打表(floyd)。下面通过代码,相信在最短路径的算法方面会有一定的收获。


深搜:

#include <stdio.h>#include <memory.h>int knight[8][8];int x[] = {1,1,2,2,-1,-1,-2,-2};int y[] = {2,-2,1,-1,2,-2,1,-1};void DFS(int si, int sj, int moves){if(si<0 || sj<0 || si>=8 || sj>=8 || moves>=knight[si][sj]) return;knight[si][sj] = moves;for (int i=0; i<8; i++)DFS(si+x[i], sj+y[i], moves+1);}int main(){char a[10], b[10];while(scanf("%s%s", a, b)!=EOF){memset(knight, 10, sizeof(knight));//最多走10步DFS(a[0]-'a', a[1]-'1', 0);printf("To get from %s to %s takes %d knight moves.\n",a, b, knight[b[0]-'a'][b[1]-'1']);}return 0;}
广搜:

#include <iostream>#include <queue>using namespace std;struct point{int x,y;int c;}from, to;int main(){queue<point> q;char src[3], dist[3];int dx[] = {1,1,2,2,-1,-1,-2,-2};int dy[] = {2,-2,1,-1,2,-2,1,-1};while(scanf("%s%s", src, dist)!=EOF){printf("To get from %s to %s ", src, dist);while(!q.empty()) q.pop();from.x = src[0]-'a';from.y = src[1]-'1';from.c = 0;to.x = dist[0]-'a';to.y = dist[1]-'1';q.push(from);point temp;while(true){from = q.front();q.pop();if (from.x==to.x && from.y==to.y) break;for(int i=0; i<8; i++){temp.x = from.x + dx[i];temp.y = from.y + dy[i];temp.c = from.c + 1;if(temp.x<0 || temp.x>7 || temp.y<0 || temp.y>7) continue;q.push(temp);}}printf("takes %d knight moves.\n", from.c);}}
打表(floyd):

/*8×8方格为64个元素,下面和floyd做法类似,初始化即可*/ #include <stdio.h>#include <stdlib.h>int i, j, m;void shortested(int k[][64]){int x, y;for(i = 0; i < 64; ++i)for(j = 0; j < 64; ++j) {x = abs(i/8-j/8);y = abs(i%8-j%8);if (x==1 && y==2 || x==2 && y==1)k[i][j] = k[j][i] = 1;}for(m = 0; m < 64; ++m)for(i = 0; i < 64; ++i)for(j = 0; j < 64; ++j)if(k[i][m] + k[m][j] < k[i][j])k[i][j] = k[i][m] + k[m][j];}int main(){int knight[64][64];for(i = 0; i < 64; ++i)for(j = 0; j < 64; ++j)knight[i][j] = 10;for(i = 0; i < 64; ++i)    knight[i][i] = 0; shortested(knight);char s[5], t[5];while (scanf("%s%s", s, t)!=EOF) {int x = (s[0]-'a')*8+(s[1]-'1');int y = (t[0]-'a')*8+(t[1]-'1');printf("To get from %s to %s ", s, t);printf("takes %d knight moves.\n", knight[x][y]);//直接查表输出结果}return 0;}
通过分析代码可知广搜适合棋盘较大,数据量较小;打表适合棋盘较小,数据量较大的情况。

原创粉丝点击