【Depth-first search DFS】Lintcode 33. N queen problem

Lintcode 33. N queen problem

Title description: Put n queens on the n*nchessboard. When any two queens are not in the same row, column, or diagonal, the two queens will not attack each other.
Given an integer n, it is required to return n*nall the placement schemes that place n queens on the chessboard without attacking each other.
Each solution (placement) is a chessboard style with different placement positions of n queens (see the example below), where'Q' and'.' represent a queen and a blank space respectively.
Insert picture description here
Problem-solving idea: Because the Queen’s placement method requires only one in the same row and the same column, for example, if you have placed it in the first column before, you can only place it in 2, 3, except for 1, next time. 4...For placing music, each placement method is actually one of the arrangements. For example, in the example 2 above, the first plan represents the arrangement 2413, and the second plan represents the arrangement 3142. So this question is actually an arrangement question. But it does not return all the permutation schemes, because some do not meet the requirements (if two queens are in the same row, the same column, and the same diagonal, the conditions are not met), so filter out when you need to find These do not meet the conditions of the program.
The conditions in the same row and the same column need to be judged, but what about the same diagonal?
Insert picture description here
It can be seen from the figure that the difference between the horizontal and vertical coordinates is equal from the upper left to the lower right, and the sum of the horizontal and vertical coordinates is equal from the upper right to the lower left. The two diagonals are excluded when the code is implemented.

class Solution {
    
    
public:
    /*
     * @param n: The number of queens
     * @return: All distinct solutions
     */
    vector<vector<string>> solveNQueens(int n) {
    
    
        vector<vector<string>> result;
        vector<int> col;//用于存放每个皇后所在的列
        search(n, col, result);
        return result;
    }
    
    //1. 递归的定义
    void search(int n, vector<int> &col, vector<vector<string>> &result) {
    
    
        //3. 递归的出口
        if (col.size() == n) {
    
    
            result.push_back(drawChessLine(col, n));
            return;
        }
        
        //2. 递归的拆解
        for (int now_col = 0; now_col < n; ++now_col) {
    
    
            if (!isValid(col, now_col)) {
    
    
                continue;
            }
            
            col.push_back(now_col);
            search(n, col, result);//递归语句上下保持对称
            col.pop_back();
        }
    }
    
    //对皇后摆放的位置(列)进行合法性判断, now_col为当前列,col为已经摆放好的列
    bool isValid(vector<int> &col, int now_col) {
    
    
        int now_row = col.size();//与now_col对应的当前行
        for (int rowIdx = 0; rowIdx < col.size(); ++rowIdx) {
    
    
            // 与之前的排放位置处于同一行或同一列的情况
            if (col[rowIdx] == now_col) {
    
    
                return false;
            }
            // 从右上到左下
            if (rowIdx + col[rowIdx] == now_row + now_col) {
    
    
                return false;
            }
            // 从左上到右下
            if (rowIdx - col[rowIdx] == now_row - now_col) {
    
    
                return false;
            }
        }
        return true;
    }
    
    // 将找到的皇后排放位置的数组排列整理为满足题目要求棋盘形式中的一行["..Q."]
    vector<string> drawChessLine(vector<int> &col, int n) {
    
    
        vector<string> result;
        for (int i = 0; i < n; ++i) {
    
    
            string tmp(n, '.');
            tmp[col[i]] = 'Q';
            result.push_back(tmp);
        }
        return result;
    }
    
};

Guess you like

Origin blog.csdn.net/phdongou/article/details/113789704