ZOJ 1091 Knight Moves

来源:互联网 发布:淘宝客隐藏优惠券设置 编辑:程序博客网 时间:2024/05/17 01:05
ZOJ Problem Set - 1091
Knight Moves

Time Limit: 2 Seconds    Memory Limit: 65536 KB

A friend of you is doing research on the Traveling KnightProblem (TKP) where you are to find the shortest closed tour ofknight moves that visits each square of a given set of nsquares on a chessboard exactly once. He thinks that the mostdifficult part of the problem is determining the smallest number ofknight moves between two given squares and that, once you haveaccomplished this, finding the tour would be easy.
Of course you know that it is vice versa. So you offer him to writea program that solves the "difficult" part.

Your job is to write a program that takes two squares aand b as input and then determines the number of knightmoves on a shortest route from a to b.

Input Specification

The input file will contain one or more test cases. Each test caseconsists of one line containing two squares separated by one space.A square is a string consisting of a letter (a-h) representing thecolumn 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 toyy 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
源码:
ZOJ <wbr>1091 <wbr>Knight <wbr>Moves

解题报告:
首先读清题意,knight翻译为骑士,其实就是马走日的意思,然后就是求从一点到另一点马要走的步数。
这一题的方法,就是搜索,广搜和深搜感觉都可以。本题用的深搜,记录每点的移动步数,对于下次访问时,如果当前步数小的话就重新赋值。
需要注意的是,24行先对棋谱初始化为1,而不是0.


0 0