【BFS】UVA439 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.

题目大意

在一个棋盘上,横坐标为a-h共8个格子,纵坐标为1-8也是8个格子。问马从给定的起点走到终点最少需要多少步。

思路

首先马一定有一种走法可以走到终点,然后用bfs即可。

代码

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class UVA439 {
    private static boolean[][] marked;
    private static class Point{
        int x;
        int y;
        int d; // 移动格子数
        Point(int x, int y, int d){
            this.x = x;
            this.y = y;
            this.d = d;
        }
    }
    // 边界检查及重复遍历检查
    private static boolean check(int x, int y){
        return !(x < 0 || x >= 8 || y < 0 || y >= 8) && !marked[x][y];
    }
    private static int[] moveX = new int[]{-2, -1, 1, 2, 2, 1, -1, -2};
    private static int[] moveY = new int[]{1, 2, 2, 1, -1, -2, -2, -1};
    private static int search(int[] start, int[] target){
        marked = new boolean[8][8];
        // 初始化起点和终点
        Point s = new Point(start[0], start[1], 0);
        int tx = target[0], ty = target[1];
        Queue<Point> queue = new LinkedList<>();
        // 将起点加入队列中
        queue.add(s);
        while (!queue.isEmpty()){
            Point v = queue.poll();
            // 将判断放在这里是为了处理起点和终点相同的情况
            if(v.x == tx && v.y == ty){
                return v.d;
            }
            for(int i = 0; i < 8; i++){
                int wx = v.x + moveX[i], wy = v.y + moveY[i];
                if(!check(wx, wy)) continue;
                Point w = new Point(wx, wy, v.d + 1);
                marked[wx][wy] = true;
                queue.add(w);
            }
        }
        return -1;
    }
    // 坐标转换,将横坐标a-h转换为0-7,纵坐标1-8转换为0-7。
    private static int[] parsePoint(String s){
        return new int[]{s.charAt(0) - 'a', s.charAt(1) - '0' - 1};
    }
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            String[] line = in.nextLine().split("\\s+");
            int ans = search(parsePoint(line[0]), parsePoint(line[1]));
            System.out.printf("To get from %s to %s takes %d knight moves.\n", line[0], line[1], ans);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/a617976080/article/details/88759207