FTPrep, 51 N-Queens

来源:互联网 发布:triz 矛盾矩阵 编辑:程序博客网 时间:2024/06/06 09:31

看懂别人的代码,一句句搞懂,各个变量的取名也比较make sense

这样就有可视化的效果,思路也容易清晰了。

代码:

class Solution {    public List<List<String>> solveNQueens(int n) {        List<List<String>> result = new ArrayList<>();        if(n==0) return result;        helper(0, n , new int[n], result);        return result;    }            private void helper(int row, int n, int[] colForRow, List<List<String>> result){        if(row==n){            List<String> solution = new ArrayList<>();            // based on the colForRow, which is 1-dimension, you need to build a 2-D solution, which is List<String>;            for(int i=0; i<n; i++){   // for each row, need to have 1 string to represent it, so need to have one StringBuilder                StringBuilder rowStr = new StringBuilder();                for(int j=0; j<n; j++){                    if(j==colForRow[i]) rowStr.append("Q");  // so for each row, there is only one Q, whose position is indicated by colForRow[i];                    else rowStr.append(".");                }                solution.add(rowStr.toString());            }            result.add(solution);            return;        }        for(int i=0; i<n; i++){  // when row is before n, meaning the table is not completely checked and finished            colForRow[row]=i; // for the current row, you can set the Q at any col position from 0 to n-1, but you need check each choice            if(check(colForRow, row)) helper(row+1, n, colForRow, result); // if check returns true, go to the next row            // other wise doing nothing but go back to the for loop and reset the col num for current row.        }    }        private boolean check(int[] colForRow, int row){  // at this interface, you have a colForRow for all the rows before this "row", and you want check the number at this row is capatible with previous rows in the colForRow        for(int i=0; i<row; i++){            if(colForRow[row]==colForRow[i] || Math.abs(colForRow[row]-colForRow[i])==row-i) return false;        } // 1, check if the same col number; 2, both diagnal directions;        return true;    }}//  Math.abs(colForRow[row]-row)==Math.abs(colForRow[i]-i); is wrong, because you over constrain the condition, like the matrix//  x x x x x//  x x x t x//  x x x x x//  x f x x x//  x x x x x//  not only you check t, but also you check the f, which is the diagnol mirrow of point t.

无comment代码:

class Solution {      public List<List<String>> solveNQueens(int n) {          List<List<String>> result = new ArrayList<>();          if(n==0) return result;          helper(0, n , new int[n], result);          return result;      }                private void helper(int row, int n, int[] colForRow, List<List<String>> result){          if(row==n){              List<String> solution = new ArrayList<>();              for(int i=0; i<n; i++){                 StringBuilder rowStr = new StringBuilder();                  for(int j=0; j<n; j++){                      if(j==colForRow[i]) rowStr.append("Q");                    else rowStr.append(".");                  }                  solution.add(rowStr.toString());              }              result.add(solution);              return;          }          for(int i=0; i<n; i++){            colForRow[row]=i;               if(check(colForRow, row)) helper(row+1, n, colForRow, result);        }      }            private boolean check(int[] colForRow, int row){        for(int i=0; i<row; i++){              if(colForRow[row]==colForRow[i] || Math.abs(colForRow[row]-colForRow[i])==row-i) return false;          }        return true;      }}


原创粉丝点击