简体   繁体   English

回溯N皇后算法

[英]Backtracking N queen Algorithm

I was seeing the below code and trying to understand how it works.我看到了下面的代码并试图了解它是如何工作的。 I got stuck in backtracking .我陷入了回溯。 could some one explain how the r value is going from 2 to 1. when it failed to find the suitable position for the queen to place .有人能解释一下 r 值是如何从 2 变为 1 的。当它未能找到适合女王放置的位置时。 ` Below is the debugging section : ` 下面是调试部分:

nQueen (mat=0x7fffffffddb0, r=2) at N_Queens_problem.cpp:47
47      for (int i = 0; i < N; i++) 
(gdb) print i
$1 = 3
(gdb) n
62  }
(gdb) print i
No symbol "i" in current context.
(gdb) print i
No symbol "i" in current context.
**(gdb) print r
$2 = 2**
(gdb) n
nQueen (mat=0x7fffffffddb0, r=1) at N_Queens_problem.cpp:47
47      for (int i = 0; i < N; i++) 
**(gdb) print r
$3 = 1**
(gdb)

Below is the nqueen function :下面是 nqueen 函数:

void nQueen(char mat[][N], int r)
{
    // if N queens are placed successfully, print the solution
    if (r == N)
    {
        for (int i = 0; i < N; i++) 
        {
            for (int j = 0; j < N; j++)
                cout << mat[i][j] << " ";
            cout << endl;
        }
        cout << endl;

        return;
    }

    // place Queen at every square in current row r
    // and recur for each valid movement    
    for (int i = 0; i < N; i++) 
    {
        // if no two queens threaten each other
        if (isSafe(mat, r, i)) 
        {
            // place queen on current square
            mat[r][i] = 'Q';

            // recur for next row
            nQueen(mat, r + 1);

            // backtrack and remove queen from current square
            mat[r][i] = '-';
        }
    }
}

Below is the function to check whether the queen is safe to place :下面是检查女王是否可以安全放置的功能:

bool isSafe(char mat[][N], int r, int c)
{
    // return false if two queens share the same column
    for (int i = 0; i < r; i++)
        if (mat[i][c] == 'Q')
            return false;

    // return false if two queens share the same \ diagonal
    for (int i = r, j = c; i >= 0 && j >= 0; i--, j--)
        if (mat[i][j] == 'Q')
            return false;

    // return false if two queens share the same / diagonal
    for (int i = r, j = c; i >= 0 && j < N; i--, j++)
        if (mat[i][j] == 'Q')
            return false;

    return true;
}

Say you have placed two queens and going to place the 3rd queen.假设您已经放置了两个皇后,并将放置第三个皇后。

  1. Till now there will be two recursive calls to nQueens() in the stack.到目前为止,堆栈中将有两次对nQueens()递归调用。 One for the queen 0 and one for the queen 3.一为皇后0,一为皇后3。
  2. nQueen(mat, r + 1); will be called with nQueen(mat, 2) and third nQueens() will be pushed to stack.将使用nQueen(mat, 2)调用,第三个 nQueens() 将被推入堆栈。

considering that it is not safe to place the queen at any of the N locations of 3rd row and考虑到将皇后放在第三排的 N 个位置中的任何一个都是不安全的,并且

  1. if (isSafe(mat, r, i)) will return false for all columns 0 to N. if (isSafe(mat, r, i))将为所有列 0 到 N 返回 false。
  2. Ultimately the loop for (int i = 0; i < N; i++) will end for r == 2 and stack will unwind.最终for (int i = 0; i < N; i++)的循环将以r == 2结束并且堆栈将展开。
  3. Stack will remove the topmost call and we will be left function calls in stack(bcak to step 1).堆栈将删除最顶层的调用,我们将在堆栈中留下函数调用(回到第 1 步)。
  4. And loop for (int i = 0; i < N; i++) will try to reposition the 2nd queen.并且循环for (int i = 0; i < N; i++)将尝试重新定位第二个皇后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM