LeetCode-51. N-Queens (JAVA)(打印N皇后解集)

来源:互联网 发布:产品设计学什么软件 编辑:程序博客网 时间:2024/06/16 09:49

51. N-Queens

The n-queens puzzle is the problem of placingn queens on ann×n chessboard such that no two queens attack each other.

Given an integer n, return all distinct solutions to then-queens puzzle.

Each solution contains a distinct board configuration of then-queens' placement, where'Q' and'.' both indicate a queen and an empty space respectively.

For example,
There exist two distinct solutions to the 4-queens puzzle:

N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行、同一列、同一斜线上的皇后都会自动攻击)。

LeetCode-52. N-Queens II (JAVA)(N皇后解集个数)

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

一:

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;}// 按列进行放置,若到了第colIndex列,//那么第0~colIndex-1列已经是放置好的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] = '.';}}}// x == i 同一行// x + j == y + i (y -x == j - i,斜率1,在同一条直线上) 同一主斜行// x + y == i + j(x-i=-(y-j),斜率-1,在同一条直线上) 同一副斜行private boolean validate(char[][] board, int x, int y) {for (int i = 0; i < board.length; i++) {// 判断放置第j列的时候,是否与前面的冲突,// 不需要判断y == j(循环j<y),只是与前面的进行比较for (int j = 0; j < y; j++) {// same as if(board[i][j] == 'Q' //&& (Math.abs(x - i) ==// Math.abs(y - j) || x == i))if (board[i][j] == 'Q' && (x - y == i - j || 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;}

二:

一个N长的数组就可以解决 int[n],例如int[0]=1表示在Q放在第1行的第2列,int[2]=3表示在Q放在第3行的第4列。

public List<List<String>> solveNQueens(int n) {List<List<String>> res = new ArrayList<List<String>>();int[] queenList = new int[n]; // 第i个位置存放的数表示row行时,Q的列placeQueen(queenList, 0, n, res);// 在第0行放Qreturn res;}private void placeQueen(int[] queenList, int row, int n, List<List<String>> res) {// 如果已经填满,就生成结果if (row == n) {ArrayList<String> list = new ArrayList<String>();for (int i = 0; i < n; i++) {String str = "";for (int col = 0; col < n; col++) {if (queenList[i] == col) {str += "Q";} else {str += ".";}}list.add(str);}res.add(list);}// 按照行进行放置// 循环每一列for (int col = 0; col < n; col++) {// 如果在该列放入Q不冲突的话if (isValid(queenList, row, col)) { // 没有回溯,因为没有修改原结果集// 只是临时记录结果queenList[row] = col;placeQueen(queenList, row + 1, n, res);}}}private boolean isValid(int[] queenList, int row, int col) {for (int i = 0; i < row; i++) {// pos为列int pos = queenList[i];// 和新加入的Q处于同一列if (pos == col) {return false;}// 在新加入的Q的右对角线上if (pos + row - i == col) {return false;}// 在新加入的Q的左对角线上if (pos - row + i == col) {return false;}}return true;}


 

0 0