图搜索-DFS-N皇后(N-Queens)
来源:互联网 发布:逆战卡数据教程 编辑:程序博客网 时间:2024/05/30 02:52
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens’ placement, where ‘Q’ and ‘.’ both indicate a queen and an empty space respectively.
非常典型的回溯问题,需要注意的点:
1、剪枝(确定性优化:不需要的就砍掉);
2、A*(不确定性优化:改变搜索顺序,不改变搜索的结果,不一定更优);
3、DFS限制搜索长度,探测长度寻找最优解—>判定问题.
//记录每列走第几行 public static int[] path= new int[100]; public static List<List<String>> res = new ArrayList<>(); //记录历史上走过的行 public static boolean[] row = new boolean[100]; //辅助判断对角线是否被走过,例如 x+y=k,记录所有已走的 x+y的和,不能有两个点该和相同,例如 colIdx + public static boolean[] diagonal = new boolean[100]; //辅助判断反对角线是否被走过,例如 y = x + colIdx - i, 记录所有已走的 x + colIdx = y + i 的点的值, //例如 colIdx - i + n -1 为避免负数 public static boolean[] reverse = new boolean[100]; public List<List<String>> solveNQueens(int n) { res.clear(); dfs(0,n); return res; } public void dfs(int colIdx, int n){ if(colIdx >= n){ //根据 path 信息记录该组结果,之后返回好进行其它结果的回溯探测 List<String> chess = new ArrayList<String>(); for(int i = 0 ;i < n; i++) { String tmpRow = ""; for (int j = 0; j < n; j++) { if (path[i] == j) tmpRow += 'Q'; else tmpRow += '.'; } chess.add(tmpRow); } res.add(chess); return; } //对某一列而言,任意行都有可能走 for(int i = 0 ; i < n; i++){ if(!row[i] && !diagonal[colIdx + i] && !reverse[colIdx - i + n -1]){ path[colIdx] = i; row[i] = true; diagonal[colIdx + i] = true; reverse[colIdx - i + n -1] = true; dfs(colIdx+1,n); row[i] = false; diagonal[colIdx + i] = false; reverse[colIdx - i + n -1] = false; } } }
0 0
- 图搜索-DFS-N皇后(N-Queens)
- 【N皇后&DFS】N-Queens
- 【N皇后&DFS】N-Queens II
- N-Queens N皇后问题 DFS
- N-皇后 N-Queens
- 【搜索-DFS】N皇后问题
- LeetCode-51-N-Queens 八皇后问题dfs
- HDU 2553 N皇后问题 (搜索DFS)
- HDU-N皇后问题-DFS深度搜索
- DFS 搜索 Problem 1019 N皇后问题
- Leetcode dfs N-Queens
- N-Queens DFS
- N-Queens II DFS
- 51. N-Queens(dfs)
- [LeetCode] [N皇后问题] N-Queens & N-Queens II
- leetcode-N-Queens 搜索
- N-Queens八皇后问题
- BZOJ 4809: 皇后 (n皇后问题)dfs搜索入门
- Linux下的进程控制块task_struct结构体
- Linux Ubuntu 修改网卡名字
- POJ
- GitHub入门与实践(三)——初步体验Git与GitHub的联动
- 魔王语言解释 C++
- 图搜索-DFS-N皇后(N-Queens)
- 使用PHP制作主机宕机检测,并发送Mail通知
- 考研英语
- light oj 1421
- CSS预处理器——Sass、LESS和Stylus区别及联系
- codforces Codeforces Round #409 div2 C 二分搜索
- cocoa pods 升级遇到的问题
- 容器对路径的处理
- sh脚本语法_基础