骑士在棋盘上的打印路径

时间:2019-04-17 12:32:03

标签: java graph

我有一个任务要打印两件事:骑士获得棋子需要多少步骤,第二是实际路径。虽然我发现了如何计算步骤的数量,但是却无法弄清楚如何收集路径,因为该算法的工作方式是尝试每个可能的步骤,因此很难选择正确的步骤。咨询表示赞赏!感谢您的建议!

package chess;

import java.io.File;
import java.io.FileReader;
import java.util.*;

class Node
{
    int x, y, dist;

    public Node(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public Node(int x, int y, int dist) {
        this.x = x;
        this.y = y;
        this.dist = dist;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Node node = (Node) o;

        if (x != node.x) return false;
        if (y != node.y) return false;
        return dist == node.dist;
    }

    @Override
    public int hashCode() {
        int result = x;
        result = 31 * result + y;
        result = 31 * result + dist;
        return result;
    }
};

class Main
{
    public static Node Encode(char letter, int number)
    {

        return new Node(letter-'a'+1, number);
    }

    public static Node Decoding(Node node) 
    {           
            char result = 0;
            switch (node.x)
            {
                    case 1:
                    {
                            result = 'a';
                            break;
                    }
                    case 2:
                    {
                            result = 'b';
                            break;
                    }
                    case 3:
                    {
                            result = 'c';
                            break;
                    }
                    case 4:
                    {
                            result = 'd';
                            break;
                    }
                    case 5:
                    {
                            result = 'e';
                            break;
                    }
                    case 6:
                    {
                            result = 'f';
                            break;
                    }
                    case 7:
                    {
                            result = 'g';
                            break;
                    }
                    case 8:
                    {
                            result = 'h';
                            break;
                    }
            }
            return new Node(result,node.y);
    }

    private static int row[] = { 2, 2, -2, -2, 1, 1, -1, -1 };
    private static int col[] = { -1, 1, 1, -1, 2, -2, 2, -2 };

    private static boolean valid(int x, int y, int N)
    {
        if (x < 0 || y < 0 || x >= N || y >= N)
            return false;

        return true;
    }

    // Find minimum number of steps taken by the knight
    // from source to reach destination using BFS
    public static List<Node> result = new ArrayList<Node>();
    public static int BFS(Node src, Node dest, int N)
    {
        // map to check if matrix cell is visited before or not
        Map<Node, Boolean> visited = new HashMap<>();
        // create a queue and enqueue first node
        Queue<Node> q = new ArrayDeque<>();
        q.add(src);
        result.add(src);
        // run till queue is not empty
        while (!q.isEmpty())
        {
            // pop front node from queue and process it
            Node node = q.poll();

            int x = node.x;
            int y = node.y;
            int dist = node.dist;
            // if destination is reached, return distance
            if (x == dest.x && y == dest.y)
                return dist;

            // Skip if location is visited before
            if (visited.get(node) == null)
            {
                // mark current node as visited
                visited.put(node, true);

                // check for all 8 possible movements for a knight
                // and enqueue each valid movement into the queue
                for (int i = 0; i < 8; ++i)
                {
                    // Get the new valid position of Knight from current
                    // position on chessboard and enqueue it in the
                    // queue with +1 distance
                    int x1 = x + row[i];
                    int y1 = y + col[i];

                    if (valid(x1, y1, N))
                    {
                        q.add(new Node(x1, y1, dist + 1));
                    }
                }
            }
        }
        // return INFINITY if path is not possible
        return Integer.MAX_VALUE;
    }

    static File file = new File("in.txt");
       private static Scanner sc;
        public static void readFile()
        {
            try
            {
                sc = new Scanner(new FileReader(file));
            }
            catch (Exception e)
            {
                System.out.println("file not found");
            }
        }


    public static void main(String[] args)
    {
        int N = 8;
        readFile();
        String srcStr = sc.next();
        String destStr = sc.next();
        char srcX = srcStr.charAt(0);
        int srcY = Character.getNumericValue(srcStr.charAt(1));
        char destX = destStr.charAt(0);
        int destY = Character.getNumericValue(destStr.charAt(1));
        Node src = Encode(srcX,srcY);
        Node dest =  Encode(destX,destY);
        BFS(src,dest,N);
        for (Node e : result)
        {
            System.out.println(e.x + " " + e.y);
        }
          System.out.println(result.size());
        System.out.println("Minimum number of steps required is " + BFS(src, dest, N));

    }
}

0 个答案:

没有答案