Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
即将拥有人鱼线的大哥
Fucking Algorithm
提交
1a247f1a
F
Fucking Algorithm
项目概览
即将拥有人鱼线的大哥
/
Fucking Algorithm
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
F
Fucking Algorithm
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
1a247f1a
编写于
3月 20, 2021
作者:
B
BruceCat
提交者:
GitHub
3月 20, 2021
浏览文件
操作
浏览文件
下载
差异文件
【51.N皇后】【Java】
【51.N皇后】【Java】
上级
47c62fe8
10b33ea2
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
65 addition
and
2 deletion
+65
-2
算法思维系列/回溯算法详解修订版.md
算法思维系列/回溯算法详解修订版.md
+65
-2
未找到文件。
算法思维系列/回溯算法详解修订版.md
浏览文件 @
1a247f1a
...
...
@@ -309,4 +309,67 @@ def backtrack(...):
<img
src=
"../pictures/qrcode.jpg"
width=
200
>
</p>
======其他语言代码======
\ No newline at end of file
======其他语言代码======
由
[
kepler-zc
](
https://github.com/kepler-zc
)
提供 51.N皇后 Java 解法代码:
```
java
class
solution
{
private
List
<
List
<
String
>>
res
=
new
ArrayList
<>();
// 输入棋盘边长 n,返回所有合法的放置
public
List
<
List
<
String
>>
solveNQueens
(
int
n
){
// '.'表示空,'Q'表示皇后,初始化空棋盘
char
[][]
chess
=
new
char
[
n
][
n
];
for
(
int
i
=
0
;
i
<
n
;
i
++)
{
Arrays
.
fill
(
chess
[
i
],
'.'
);
}
// 已经不能放置皇后的列(被占用)
boolean
[]
usedCol
=
new
boolean
[
n
];
// 已经不能放置皇后的正斜线 , 按右上角到左下角排列 , 共2n-1条
boolean
[]
usedSlash
=
new
boolean
[
2
*
n
-
1
];
// 已经不能放置皇后的反斜线 , 按左上角到右下角排列 , 共2n-1条
boolean
[]
usedBackSlash
=
new
boolean
[
2
*
n
-
1
];
backtrack
(
chess
,
0
,
usedCol
,
usedSlash
,
usedBackSlash
);
return
res
;
}
// 路径:chess 中小于 row 的那些行都已经成功放置了皇后
// 选择列表:第 row 行的所有列都是放置皇后的选择
// 结束条件:row 超过 棋盘最后一行
private
void
backtrack
(
char
[][]
chess
,
int
row
,
boolean
[]
usedCol
,
boolean
[]
usedSlash
,
boolean
[]
usedBackSlash
)
{
// 触发结束条件
if
(
row
==
chess
.
length
){
res
.
add
(
construct
(
chess
));
return
;
}
for
(
int
col
=
0
;
col
<
chess
.
length
;
col
++)
{
// 对合法选择进行回溯操作
// 分别检查列,左上方, 右上方是否存在皇后冲突,都不冲突集为合法选择。
if
(!
usedCol
[
col
]
&&
!
usedSlash
[
row
-
col
+
usedCol
.
length
-
1
]
&&
!
usedBackSlash
[
col
+
row
]){
// 做选择
chess
[
row
][
col
]
=
'Q'
;
usedCol
[
col
]
=
true
;
// 对坐标为[i,j]的点对应的正斜线和反斜线的索引分别为:row-col+n-1; col+row
usedSlash
[
row
-
col
+
chess
.
length
-
1
]
=
true
;
usedBackSlash
[
col
+
row
]
=
true
;
// 进入下一行
backtrack
(
chess
,
row
+
1
,
usedCol
,
usedSlash
,
usedBackSlash
);
// 撤销选择
chess
[
row
][
col
]
=
'.'
;
usedCol
[
col
]
=
false
;
usedSlash
[
row
-
col
+
chess
.
length
-
1
]
=
false
;
usedBackSlash
[
col
+
row
]
=
false
;
}
}
}
private
List
<
String
>
construct
(
char
[][]
chess
)
{
// 数组转List
List
<
String
>
path
=
new
ArrayList
<>();
for
(
char
[]
chars
:
chess
)
{
path
.
add
(
new
String
(
chars
));
}
return
path
;
}
}
```
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录