【问题标题】:How do you find possible x, y values for the Knight's Tour Problem?您如何找到 Knight's Tour Problem 的可能 x、y 值?
【发布时间】:2021-11-26 04:14:56
【问题描述】:

我正在研究骑士的巡回赛问题,当国际象棋骑士棋子恰好移动到网格上的每个方格一次时,就会获得解决方案。但是,在查看该问题的不同解决方案时,我不断看到一组特定的数字:

int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };

这些整数究竟是从哪里来的?有没有一种方法可以在没有这些特定整数的情况下解决问题,或者它们对问题至关重要?作为参考,这是我正在查看的完整代码。 (信用 GeeksforGeeks)

// Java program for Knight Tour problem
class KnightTour {
    static int N = 8;
 
    /* A utility function to check if i,j are
       valid indexes for N*N chessboard */
    static boolean isSafe(int x, int y, int sol[][])
    {
        return (x >= 0 && x < N && y >= 0 && y < N
                && sol[x][y] == -1);
    }
 
    /* A utility function to print solution
       matrix sol[N][N] */
    static void printSolution(int sol[][])
    {
        for (int x = 0; x < N; x++) {
            for (int y = 0; y < N; y++)
                System.out.print(sol[x][y] + " ");
            System.out.println();
        }
    }
 
    /* This function solves the Knight Tour problem
       using Backtracking.  This  function mainly
       uses solveKTUtil() to solve the problem. It
       returns false if no complete tour is possible,
       otherwise return true and prints the tour.
       Please note that there may be more than one
       solutions, this function prints one of the
       feasible solutions.  */
    static boolean solveKT()
    {
        int sol[][] = new int[8][8];
 
        /* Initialization of solution matrix */
        for (int x = 0; x < N; x++)
            for (int y = 0; y < N; y++)
                sol[x][y] = -1;
 
        /* xMove[] and yMove[] define next move of Knight.
           xMove[] is for next value of x coordinate
           yMove[] is for next value of y coordinate */

        int xMove[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
        int yMove[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
 
        // Since the Knight is initially at the first block
        sol[0][0] = 0;
 
        /* Start from 0,0 and explore all tours using
           solveKTUtil() */
        if (!solveKTUtil(0, 0, 1, sol, xMove, yMove)) {
            System.out.println("Solution does not exist");
            return false;
        }
        else
            printSolution(sol);
 
        return true;
    }
 
    /* A recursive utility function to solve Knight
       Tour problem */
    static boolean solveKTUtil(int x, int y, int movei,
                               int sol[][], int xMove[],
                               int yMove[])
    {
        int k, next_x, next_y;
        if (movei == N * N)
            return true;
 
        /* Try all next moves from the current coordinate
            x, y */
        for (k = 0; k < 8; k++) {
            next_x = x + xMove[k];
            next_y = y + yMove[k];
            if (isSafe(next_x, next_y, sol)) {
                sol[next_x][next_y] = movei;
                if (solveKTUtil(next_x, next_y, movei + 1,
                                sol, xMove, yMove))
                    return true;
                else
                    sol[next_x][next_y]
                        = -1; // backtracking
            }
        }
 
        return false;
    }
 
    /* Driver Code */
    public static void main(String args[])
    {
        // Function Call
        solveKT();
    }
}
// This code is contributed by Abhishek Shankhadhar

【问题讨论】:

    标签: java arrays backtracking chess


    【解决方案1】:

    它只是一个详尽的列表,列出了一个骑士可以做出的所有动作,相对于其当前位置。该列表由国际象棋规则(以及棋盘的矩形网格)确定。

    例如,两个数组中的第 0 个条目表示“右侧 2 个正方形和向前 1 个正方形” - 假设 x 坐标从左到右增加,y 坐标从前到后增加。

    您可以通过列举“一个方向上的两个正方形,然后是与之成直角的一个正方形”的所有可能组合来自己确定列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      相关资源
      最近更新 更多