当前位置:网站首页>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.
边栏推荐
- Deeply understand the mvcc mechanism of MySQL
- Solve system has not been booted with SYSTEMd as init system (PID 1) Can‘t operate.
- Huffman coding experiment report
- Sitescms v3.1.0 release, launch wechat applet
- Luogup3694 Bangbang chorus standing in line
- 正则表达式
- Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward
- sitesCMS v3.0.2发布,升级JFinal等依赖
- regular expression
- 高效能人士的七个习惯
猜你喜欢

道路建设问题
![[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number](/img/9d/6118b699c0d90810638f9b08d4f80a.jpg)
[combinatorics] permutation and combination (the combination number of multiple sets | the repetition of all elements is greater than the combination number | the derivation of the combination number

双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆

显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti

高效能人士的七个习惯

【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay

Sword finger offer14 the easiest way to cut rope

regular expression
[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈..."/>开始报名丨CCF C³[email protected]奇安信:透视俄乌网络战 —— 网络空间基础设施面临的安全对抗与制裁博弈...

人身变声器的原理
随机推荐
SLF4J 日志门面
Sword finger offer 12 Path in matrix
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
【R】【密度聚类、层次聚类、期望最大化聚类】
php:&nbsp; The document cannot be displayed in Chinese
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
Analysis of the influence of voltage loop on PFC system performance
Annotation and reflection
OpenHarmony应用开发之ETS开发方式中的Image组件
Kotlin - 改良装饰者模式
R语言使用data函数获取当前R环境可用的示例数据集:获取datasets包中的所有示例数据集、获取所有包的数据集、获取特定包的数据集
【習題五】【數據庫原理】
C graphical tutorial (Fourth Edition)_ Chapter 18 enumerator and iterator: enumerator samplep340
18W word Flink SQL God Road manual, born in the sky
Setting up Oracle datagurd environment
剑指 Offer 16. 数值的整数次方
Fabric.js 更换图片的3种方法(包括更换分组内的图片,以及存在缓存的情况)
Slf4j log facade
Mysqlbetween implementation selects the data range between two values