新博文地址:
N-Queens
写道
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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
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.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[
[".Q..", // Solution 1
"...Q",
"Q...",
"..Q."],
["..Q.", // Solution 2
"Q...",
"...Q",
".Q.."]
]
很经典的八皇后问题,跟N-Queens II没啥区别。有啥不明白的看这里。
代码如下:
ArrayList<String[]> result = new ArrayList<String[]>(); public ArrayList<String[]> solveNQueens(int n) { if(n <= 0){ return result; } int[] hash = new int[n]; place(0,n,hash); return result; } private void place(int number,int n,int [] hash){ if(number == n){//load successfully! fill String[] by hash String[] strs = new String[n]; for(int i = 0; i < n; i++){ strs[i] = ""; for(int j = 0 ; j < n;j++){ if(j != hash[i]){ strs[i] += "."; }else{ strs[i] += 'Q'; } } } result.add(strs); } for(int i = 0 ; i < n; i++){ if(isConflict(hash,number,i)){ continue; } hash[number] = i; place(number + 1, n,hash); } } private boolean isConflict(int[] hash,int row,int column){ if(row == 0){ return false; } for(int i = 0; i < row;i++){ if(hash[i] == column || hash[i] - i == column - row || hash[i] + i == column + row){ return true; } } return false; }
相关推荐
leetcode 71 Python用 Python 编写 Leetcode (数据科学家的解决方案) - 易于理解的最佳解决方案,可以通过所有 Leetcode 测试用例,对于非 ...leetcode ...leetcode ...N-Queens (HARD) Leetcode 52. N-
leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 ...norvig神牛Python代码写的很飘逸,果然是有LISP...N-Queens N-Queens II Balanced Binary Tree Binar
leetcode 530 力码练习 前 250 个问题 1 二和 :check_mark: 3 无重复字符的最长子串 :check_mark: 4 两个有序数组的中位数 5 最长回文子串 7 反转整数 8 字符串到整数 (atoi) 10 正则表达式匹配 11 盛水的容器 12 ...
leetcode怎么销号 LeetCode-Solutions :green_heart:My ...N-Queens Hard 回溯 0053 Maximum Subarray Easy 动态规划 0069 Sqrt(x) Easy 二分、牛顿迭代 0070 Climbing Stairs Easy 动态规划 0075 Sort Colors M
leetcode ...NQueens * 回溯法- 网易2017算法工程师笔试题3 * 贪心法- Dijkstra算法 ShortestDistanceAlgorithm * 动态规划- Floyd最短路径算法 ShortestDistanceAlgorithm * 动态规划- 最长公共子序列 ...
051:N-Queens 052:N-Queens II 071: Letter Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也可以用这种思想来解释。只不过是特殊的递归而已。(只有两路,不用循环) 题型二:动态规划...
leetcode中国力码 我的 Leetcode 生活经历! 我创建了这个存储库来分享我对 leetcode 问题的解决方案。 另外,我会补充一下我在解决问题时的想法,也会补充一些简单的测试用例以供...N-Queens(硬) 56. 合并间隔(中)
leetcode ...N-Queens II 最大子阵列 螺旋矩阵 跳跃游戏 合并间隔 插入间隔 螺旋矩阵 II 排列顺序(无法访问文章) 61-70 轮换名单 独特的路径 独特的路径 II 最小路径和(无法访问文章) 有效号码 加一
leetcode 推开箱子进深 Leetcode-May-Challenge-2021 它包含 Leetcode May Challenge 2021 的解决方案 比赛链接: 问题 问题名称 点击打开问题 无向图中的连通分量数 ...N-皇后 ...N-Queens II 最大差距 搜索建议系统
leetcode 530 leetcode 我对 leetcode 的解决方案。 使用c++解决问题。 # 标题 解决方案 标签 方法 1185 一周中的天 大批 5499 检测长度模式重复 k 次或多次 大批 ...n-queens-ii DFS,搜索 1003 替
N-Queens 572 另一棵树的子树98 验证二叉搜索树1048 最长的字符串链101 对称树第151话第177话218 天际线问题242 有效字谜378 排序矩阵中的第 K 个最小元素第489章 扫地机器人406按高度重构队列592 分数加减法920 ...
N-Queens II 平衡二叉树 二叉树中序遍历 二叉树最大路径和 将排序数组转换为二叉搜索树 将排序列表转换为二叉搜索树 将二叉树展平到链表 二叉树的最大深度 二叉树的最小深度 路径和 排列 排列二 在每个节点中填充下...
leetcode 棋盘算法实现 有趣的算法或有趣的算法实现 动态规划(非常基础)/递归 ...nQueens.py 很少包含外部库,所以下面的就可以了。 编译任何 C 程序: gcc -o X Xc 执行 python 脚本: chmod 755 X.py --> ./X.py
N 个节点 中等的 20 有效括号 简单的 21 合并两个排序列表 简单的 22 生成括号 中等的 23 合并 k 个排序列表 难的 26 从排序数组中删除重复项 简单的 27 删除元素 简单的 30 连接所有单词的子串 难的 32 最长有效...
比如n-queens-ii对应链接为: 题目 url add-two-numbers find-peak-element longest-common-subsequence longest-consecutive-sequence max-area-of-island next-greater-element-ii serialize-and-deserialize-...
最大公共字符串leetcode 这本书的在线版本可以在 [gitbook.com] 上查看 进步写作 104 二叉树的最大深度 111 二叉树的最小深度 174 地牢游戏 84 直方图中最大的矩形 85 最大矩形 198 强盗 第213话 第42章 截留雨水 11...
leetcode 2 和 c PlayLeetCode 基于c++的LeetCode训练 尖端: 单击问题的Number (#) 单击Solution ([00xx-Solution]) 到解决方案 # 标题 困难 方法 解决方案 二和 简单的 ...N ...N-皇后 ...N-Queens II 难的
leetcode力扣是什么 leetcode-按类别 看了一些leetcode刷题指南,总结一下两个重点,一是...N-Queens (hard) 132 Palindrome Partitioning (hard) 212 Word Search II (hard) DFS /二叉树 the difference between df
leetcode中文版lintcode题解 相关链接 目录 指数 标题 代码 困难 1 A + ...N-皇后 ...N-Queens II 中等的 35 反向链表 简单的 36 反向链表 II 中等的 37 反转 3 位整数 幼稚的 38 搜索二维矩阵 II 中等
49组Anagrams视频讲解50 Pow(x,n)视频讲解51 N-Queens视频讲解52 N-Queens II视频讲解53最大子数组视频讲解54螺旋矩阵视频讲解55跳转游戏视频讲解56合并间隔视频讲解57插入间隔视频讲解59螺旋矩