N-Queens

原题: https://leetcode.com/problems/n-queens/description/

题意: n皇后难题就是输入一个整数n,在n x n的棋盘上,每行放一个棋子,使得棋子所在的列和两条斜线上没有其他棋子,枚举所有的解决方案。

约定: (1)每个解决方案都包含一个不同的n皇后放置位置对应的棋盘配置;(2)“Q”代表皇后,“.”代表空格

例子:输入n=4,输出4-皇后难题的2个不同解决方案如下:)

[
 [".Q..",  // Solution 1
  "...Q",
  "Q...",
  "..Q."],

 ["..Q.",  // Solution 2
  "Q...",
  "...Q",
  ".Q.."]
]


标签: 皇后、棋子、queens、棋盘、solution、面试
猜你感兴趣的圈子:
LeetCode交流圈
  • Bingo
    2017-08-10 14:22:54 1楼#1层

    C++:

    class Solution {
    public:
        std::vector<std::vector<std::string> > solveNQueens(int n) {
            std::vector<std::vector<std::string> > res;
            std::vector<std::string> nQueens(n, std::string(n, '.'));
            std::vector<int> flag_col(n, 1), flag_45(2 * n - 1, 1), flag_135(2 * n - 1, 1);
            solveNQueens(res, nQueens, flag_col, flag_45, flag_135, 0, n);
            return res;
        }
    private:
        void solveNQueens(std::vector<std::vector<std::string> > &res, std::vector<std::string> &nQueens, std::vector<int> &flag_col, std::vector<int> &flag_45, std::vector<int> &flag_135, int row, int &n) {
            if (row == n) {
                res.push_back(nQueens);
                return;
            }
            for (int col = 0; col != n; ++col)
                if (flag_col[col] && flag_45[row + col] && flag_135[n - 1 + col - row]) {
                    flag_col[col] = flag_45[row + col] = flag_135[n - 1 + col - row] = 0;
                    nQueens[row][col] = 'Q';
                    solveNQueens(res, nQueens, flag_col, flag_45, flag_135, row + 1, n);
                    nQueens[row][col] = '.';
                    flag_col[col] = flag_45[row + col] = flag_135[n - 1 + col - row] = 1;
                }
        }
    };

  • Bingo
    2017-08-10 14:35:12 2楼#1层

    Java:

    public class Solution {
        public List<List<String>> solveNQueens(int n) {
            char[][] board = new char[n][n];
            for(int i = 0; i < n; i++)
                for(int j = 0; j < n; j++)
                    board[i][j] = '.';
            List<List<String>> res = new ArrayList<List<String>>();
            dfs(board, 0, res);
            return res;
        }
        
        private void dfs(char[][] board, int colIndex, List<List<String>> res) {
            if(colIndex == board.length) {
                res.add(construct(board));
                return;
            }
            
            for(int i = 0; i < board.length; i++) {
                if(validate(board, i, colIndex)) {
                    board[i][colIndex] = 'Q';
                    dfs(board, colIndex + 1, res);
                    board[i][colIndex] = '.';
                }
            }
        }
        
        private boolean validate(char[][] board, int x, int y) {
            for(int i = 0; i < board.length; i++) {
                for(int j = 0; j < y; j++) {
                    if(board[i][j] == 'Q' && (x + j == y + i || x + y == i + j || x == i))
                        return false;
                }
            }
            
            return true;
        }
        
        private List<String> construct(char[][] board) {
            List<String> res = new LinkedList<String>();
            for(int i = 0; i < board.length; i++) {
                String s = new String(board[i]);
                res.add(s);
            }
            return res;
        }
    }

  • 回复
隐藏