Special backtracking Leetcode of -51. N queens (N-Queens)
n research queens problem is how to n queens placed n × n on the board, and the queen can not attack each other to each other.
A Method for Solving the picture shows the 8 queens problem.
Given an integer n , returns all the different n solutions to the problem of the Queen.
Each solution contains an explicit n queens problem pieces placed embodiment, the program 'Q'
and '.'
represent the space and queen.
Example:
Input: 4 Output: [ [ ".Q ..", // Solution. 1 "... Q", "Q ...", "..Q."], [ "..Q.", // Solution 2 "Q ...", "... Q", ".Q .."] ] explanation: there are two different solution of 4 queens problem.
Analysis: Enter a N, N * N find this the panel, N Queen solution. Requirement is placed upon a queen, the ranks of the queen can not exist other queen, and the two diagonals have not queen.
Using backtracking can answer this question, first initializes a array of N * N, and arranged on its value ''.
char mp[][] = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { mp[i][j] = '.'; } }
Then we deposit a new answer to the List,
List<List<String>> ans = new ArrayList<>();
Before writing DFS, we need to write a boolean function type ok for a board to determine whether the requirements:
public boolean ok(char[][] mp, int len, int x, int y) { // check row for (int i = 0; i < len; i++) { if (i == y) continue; if (mp[x][i] == 'Q') return false; } // check col for (int i = 0; i < len; i++) { if (i == x) continue; if (mp[i][y] == 'Q') return false; } // x=1 y=3 int cnt = 0; int up = 0; int down = 0; for(int i=y+1;i<len;i++){ up = (++cnt)*-1+x; down = cnt*1+x; if(up<len && up>=0){ //System.out.println("mp[up][i]=["+up+"]["+i+"]"); if(mp[up][i]=='Q') return false; } if(down>=0 && down<len){ //System.out.println("mp[down][i]=["+down+"]["+i+"]"); if(mp[down][i]=='Q'){ return false; } } } //System.out.println("other"); cnt = 0; for(int i=y-1;i>=0;i--){ up = (++cnt)*-1+x; down = cnt*1+x; if(up<len && up>=0){ //System.out.println("mp[up][i]=["+up+"]["+i+"]"); if(mp[up][i]=='Q') return false; } if(down>=0 && down<len){ //System.out.println("mp[down][i]=["+down+"]["+i+"]"); if(mp[down][i]=='Q'){ return false; } } } return true; }
Next dfs start writing function,
the first parameter is mp array, this is the sub-board,
the second parameter is n, represents the size of the board,
the third parameter is i, the 2-dimensional matrix is converted to a dimensions of, for example, i = 1 corresponds to (0,1) at this point, and so on.
The fourth parameter is the queen, used to keep the number of pieces of the discharge current.
public void dfs(char[][] mp, int len, int i,int queen) { int x = i / len; int y = i % len; if ((x >= len || y >= len)) { if(queen!=len) return; List<String> list = new ArrayList<>(); for (int a = 0; a < len; a++) { String tmp = ""; for (int b = 0; b < len; b++) { tmp += mp[a][b]; } list.add(tmp); } ans.add(list); return; } dfs(mp,len,i+1,queen); if (ok(mp, len, x, y)) { mp[x][y] = 'Q'; dfs(mp, len, i + 1,queen+1); mp[x][y] = '.'; } }
All together, and finally the AC code is:
class Solution { List<List<String>> ans = new ArrayList<>(); public List<List<String>> solveNQueens(int n) { char mp[][] = new char[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { mp[i][j] = '.'; } } dfs(mp, n, 0,0); return ans; } public void dfs(char[][] mp, int len, int i,int queen) { int x = i / len; int y = i % len; if ((x >= len || y >= len)) { if(queen!=len) return; List<String> list = new ArrayList<>(); for (int a = 0; a < len; a++) { String tmp = ""; for (int b = 0; b < len; b++) { tmp += mp[a][b]; } list.add(tmp); } ans.add(list); return; } dfs(mp,len,i+1,queen); if (ok(mp, len, x, y)) { mp[x][y] = 'Q'; dfs(mp, len, i + 1,queen+1); mp[x][y] = '.'; } } public boolean ok(char[][] mp, int len, int x, int y) { // check row for (int i = 0; i < len; i++) { if (i == y) continue; if (mp[x][i] == 'Q') return false; } // check col for (int i = 0; i < len; i++) { if (i == x) continue; if (mp[i][y] == 'Q') return false; } // x=1 y=3 int cnt = 0; int up = 0; int down = 0; for(int i=y+1;i<len;i++){ up = (++cnt)*-1+x; down = cnt*1+x; if(up<len && up>=0){ //System.out.println("mp[up][i]=["+up+"]["+i+"]"); if(mp[up][i]=='Q') return false; } if(down>=0 && down<len){ //System.out.println("mp[down][i]=["+down+"]["+i+"]"); if(mp[down][i]=='Q'){ return false; } } } //System.out.println("other"); cnt = 0; for(int i=y-1;i>=0;i--){ up = (++cnt)*-1+x; down = cnt*1+x; if(up<len && up>=0){ //System.out.println("mp[up][i]=["+up+"]["+i+"]"); if(mp[up][i]=='Q') return false; } if(down>=0 && down<len){ //System.out.println("mp[down][i]=["+down+"]["+i+"]"); if(mp[down][i]=='Q'){ return false; } } } return true; } }