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; }}
阅读全文
0 0
- FTPrep, 51 N-Queens
- FTPrep: 52 N-Queens II
- [leetcode 51] N-Queens
- LeetCode(51) N-Queens
- leetcode-51-N-Queens
- leetcode 51: N-Queens
- 51 N-Queens
- LeetCode 51: N-Queens
- LeetCode 51 - N-Queens
- Leetcode 51 N-Queens
- LeetCode 51 N-Queens
- leetcode(51):N-Queens
- LeetCode 51 N-Queens
- 51- N Queens
- Leetcode 51 N-Queens
- LeetCode 51 N-Queens II
- [leetcode-51]N-Queens(java)
- LeetCode(51) N-Queens
- Cocos2d从入门到睡觉:学习(一)
- Linux内核链表listnode
- 51Nod-1023-石子归并 V3
- 反射的应用、获取Class类实例
- DataServices常见问题处理
- FTPrep, 51 N-Queens
- [Hexo] Hexo更换主题
- 深度学习概论:为什么深度学习会兴起?
- SHiro学习笔记
- [html] div标签
- 51Nod 1081 子段求和(数组/树状数组)
- 51Nod-2006 飞行员配对(二分图最大匹配,匈牙利算法)
- Java程序员必知的8大排序
- [html] h标签