当前位置:网站首页>8皇后问题
8皇后问题
2022-07-03 12:36:00 【51CTO】
(一)问题描述

(二)问题分析

(三)代码实现
package
recursion;
/**
* @author Jin
* @ Date 2022年07月2022/7/1日23:04
*/
public
class
Queen {
/**定义一个max表示有几个皇后*/
int
max
=
8;
static
int
count
=
0;
/**定义数组array,保存皇后放置的位置,比如arr={0,4,7,5,2,6,1,3}*/
int[]
array
=
new
int[
max];
public
static
void
main(
String[]
args) {
Queen
queue8
=
new
Queen();
queue8.
check(
0);
System.
out.
println(
"总共有解法: "
+
count
+
"种");
}
/**方法:放置第n个皇后*/
private
void
check(
int
n){
if(
n
==
max){
print();
return;
}
//依次放入皇后n,并判断是否冲突
for (
int
i
=
0;
i
<
max ;
i
++) {
//将第n个皇后放到该行的第一列
array[
n]
=
i;
//判断当放置第n个皇后位置是否冲突
if(
judge(
n)){
//如果位置不冲突,就开始放第(n+1)个皇后
check(
n
+
1);
}
//如果冲突,就将第n个皇后放到该行的下一个位置,继续判断
}
}
/**当放置第n个皇后时,就去检查该皇后是否和前面已经摆放好的皇后是否冲突
* 注意:check是每一次递归时,进入到check中都会有 for(int i=0;i<max;i++) ,因此会有回溯
* */
private
boolean
judge(
int
n){
//n表示第n个皇后
for (
int
i
=
0;
i
<
n ;
i
++) {
/**
* 说明:
* (1) array[i]==array[n] :表示判断两个皇后是否在同一列
* (2) Math.abs(n-i)==Math.abs(array[n]-array[i])):表示判断两个皇后是否在同一斜线(仔细思考)
* (3) 无需判断是否在同一行(n一直在递增)
* */
if(
array[
i]
==
array[
n]
||
Math.
abs(
n
-
i)
==
Math.
abs(
array[
n]
-
array[
i])){
return
false;
}
}
return
true;
}
/**写一个方法,可以将皇后的摆放位置输出*/
private
void
print(){
count
++;
for (
int
i
=
0;
i
<
array.
length;
i
++) {
System.
out.
print(
array[
i]
+
" ");
}
System.
out.
println();
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
边栏推荐
- 这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
- Sword finger offer 15 Number of 1 in binary
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第三章习题】
- Useful blog links
- 剑指 Offer 14- II. 剪绳子 II
- 2022-02-13 plan for next week
- Flink SQL knows why (16): dlink, a powerful tool for developing enterprises with Flink SQL
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
- Kotlin - 改良装饰者模式
- rxjs Observable filter Operator 的实现原理介绍
猜你喜欢
![【R】 [density clustering, hierarchical clustering, expectation maximization clustering]](/img/a2/b287a5878761ee22bdbd535cae77eb.png)
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]

106. 如何提高 SAP UI5 应用路由 url 的可读性

Flink SQL knows why (7): haven't you even seen the ETL and group AGG scenarios that are most suitable for Flink SQL?

Flink SQL knows why (13): is it difficult to join streams? (next)

Annotation and reflection

Servlet

PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?

February 14, 2022, incluxdb survey - mind map

这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看

Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
随机推荐
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
2022-01-27 redis cluster technology research
剑指 Offer 14- I. 剪绳子
C graphical tutorial (Fourth Edition)_ Chapter 15 interface: interfacesamplep268
Kotlin - 改良装饰者模式
Logback 日志框架
Finite State Machine FSM
Elk note 24 -- replace logstash consumption log with gohangout
开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...
STM32 and motor development (from MCU to architecture design)
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
Grid connection - Analysis of low voltage ride through and island coexistence
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter V exercises]
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
阿南的疑惑
MySQL constraints
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Differences and connections between final and static
道路建设问题
Flink SQL knows why (XIV): the way to optimize the performance of dimension table join (Part 1) with source code