N-Queens II leetcode java
题目:
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
题解:
这道题跟NQueens的解法完全一样(具体解法参照N QueensN Queens leetcode java),只不过要求的返回值不同了。。所以要记录的result稍微改一下就好了。。。
因为涉及到递归,result传进去引用类型(List,数组之类的)才能在层层递归中得以保存,所以这里使用一个长度为1的数组帮助计数。
当然,也可以使用一个全局变量来帮助计数。
代码如下:
1 public int totalNQueens(int n) {
2 int[] res = {0};
3 if(n<=0)
4 return res[0];
5
6 int [] columnVal = new int[n];
7
8 DFS_helper(n,res,0,columnVal);
9 return res[0];
10 }
11
12 public void DFS_helper(int nQueens, int[] res, int row, int[] columnVal){
13 if(row == nQueens){
14 res[0] += 1;
15 }else{
16 for(int i = 0; i < nQueens; i++){
17 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
18
19 if(isValid(row,columnVal))
20 DFS_helper(nQueens, res, row+1, columnVal);
21 }
22 }
23 }
24
25 public boolean isValid(int row, int [] columnVal){
26 for(int i = 0; i < row; i++){
27 if(columnVal[row] == columnVal[i]
28 ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
29 return false;
30 }
31 return true;
2 int[] res = {0};
3 if(n<=0)
4 return res[0];
5
6 int [] columnVal = new int[n];
7
8 DFS_helper(n,res,0,columnVal);
9 return res[0];
10 }
11
12 public void DFS_helper(int nQueens, int[] res, int row, int[] columnVal){
13 if(row == nQueens){
14 res[0] += 1;
15 }else{
16 for(int i = 0; i < nQueens; i++){
17 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
18
19 if(isValid(row,columnVal))
20 DFS_helper(nQueens, res, row+1, columnVal);
21 }
22 }
23 }
24
25 public boolean isValid(int row, int [] columnVal){
26 for(int i = 0; i < row; i++){
27 if(columnVal[row] == columnVal[i]
28 ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
29 return false;
30 }
31 return true;
使用全局变量来记录结果的代码是:
1 int res;
2 public int totalNQueens(int n) {
3 res = 0;
4 if(n<=0)
5 return res;
6
7 int [] columnVal = new int[n];
8
9 DFS_helper(n,0,columnVal);
10 return res;
11 }
12
13 public void DFS_helper(int nQueens, int row, int[] columnVal){
14 if(row == nQueens){
15 res += 1;
16 }else{
17 for(int i = 0; i < nQueens; i++){
18 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
19
20 if(isValid(row,columnVal))
21 DFS_helper(nQueens, row+1, columnVal);
22 }
23 }
24 }
25
26 public boolean isValid(int row, int [] columnVal){
27 for(int i = 0; i < row; i++){
28 if(columnVal[row] == columnVal[i]
29 ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
30 return false;
31 }
32 return true;
33 }
2 public int totalNQueens(int n) {
3 res = 0;
4 if(n<=0)
5 return res;
6
7 int [] columnVal = new int[n];
8
9 DFS_helper(n,0,columnVal);
10 return res;
11 }
12
13 public void DFS_helper(int nQueens, int row, int[] columnVal){
14 if(row == nQueens){
15 res += 1;
16 }else{
17 for(int i = 0; i < nQueens; i++){
18 columnVal[row] = i;//(row,columnVal[row)==>(row,i)
19
20 if(isValid(row,columnVal))
21 DFS_helper(nQueens, row+1, columnVal);
22 }
23 }
24 }
25
26 public boolean isValid(int row, int [] columnVal){
27 for(int i = 0; i < row; i++){
28 if(columnVal[row] == columnVal[i]
29 ||Math.abs(columnVal[row]-columnVal[i]) == row-i)
30 return false;
31 }
32 return true;
33 }