当前位置:网站首页>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.
边栏推荐
- 【習題五】【數據庫原理】
- 高效能人士的七个习惯
- 【数据库原理及应用教程(第4版|微课版)陈志泊】【第六章习题】
- 已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
- CVPR 2022 image restoration paper
- rxjs Observable filter Operator 的实现原理介绍
- MySQL
- My creation anniversary: the fifth anniversary
- Sword finger offer 17 Print from 1 to the maximum n digits
- Elk note 24 -- replace logstash consumption log with gohangout
猜你喜欢

Grid connection - Analysis of low voltage ride through and island coexistence

已解决(机器学习中查看数据信息报错)AttributeError: target_names

Gan totem column bridgeless boost PFC (single phase) seven PFC duty cycle feedforward

Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window

Two solutions of leetcode101 symmetric binary tree (recursion and iteration)

2022-02-14 incluxdb cluster write data writetoshard parsing

sitesCMS v3.1.0发布,上线微信小程序

解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.

Flink SQL knows why (12): is it difficult to join streams? (top)

Seven habits of highly effective people
随机推荐
Task5: multi type emotion analysis
Differences and connections between final and static
rxjs Observable filter Operator 的实现原理介绍
Idea full text search shortcut ctr+shift+f failure problem
C graphical tutorial (Fourth Edition)_ Chapter 20 asynchronous programming: examples - using asynchronous
[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
【数据库原理及应用教程(第4版|微课版)陈志泊】【第五章习题】
【数据库原理及应用教程(第4版|微课版)陈志泊】【第七章习题】
Logback 日志框架
Logseq 评测:优点、缺点、评价、学习教程
IDEA 全文搜索快捷键Ctr+Shift+F失效问题
php:&nbsp; The document cannot be displayed in Chinese
剑指 Offer 16. 数值的整数次方
The difference between stratifiedkfold (classification) and kfold (regression)
Kotlin - 改良装饰者模式
人身变声器的原理
(first) the most complete way to become God of Flink SQL in history (full text 180000 words, 138 cases, 42 pictures)
剑指 Offer 15. 二进制中1的个数
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
剑指 Offer 17. 打印从1到最大的n位数