当前位置:网站首页>8 Queen question
8 Queen question
2022-07-03 13:24:00 【51CTO】
( One ) Problem description

( Two ) Problem analysis

( 3、 ... and ) Code implementation
package
recursion;
/**
* @author Jin
* @ Date 2022 year 07 month 2022/7/1 Japan 23:04
*/
public
class
Queen {
/** Define a max It means there are several queens */
int
max
=
8;
static
int
count
=
0;
/** Define an array array, Save where the queen is placed , such as 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(
" There are solutions in total : "
+
count
+
" Kind of ");
}
/** Method : Place the second n A queen */
private
void
check(
int
n){
if(
n
==
max){
print();
return;
}
// Put the queen in turn n, And determine if there is a conflict
for (
int
i
=
0;
i
<
max ;
i
++) {
// Will be the first n Put queens in the first column of the row
array[
n]
=
i;
// Judge when placing the n Whether the positions of the Queens conflict
if(
judge(
n)){
// If the location does not conflict , Start to put the first (n+1) A queen
check(
n
+
1);
}
// If conflict , Will be the first n The queen is placed in the next position of the line , Continue to judge
}
}
/** When placing the first n When I was a queen , Go and check whether the queen conflicts with the queen already placed in front
* Be careful :check It's every recursion , Enter into check There will be for(int i=0;i<max;i++) , So there will be backtracking
* */
private
boolean
judge(
int
n){
//n It means the first one n A queen
for (
int
i
=
0;
i
<
n ;
i
++) {
/**
* explain :
* (1) array[i]==array[n] : It means to judge whether two queens are in the same column
* (2) Math.abs(n-i)==Math.abs(array[n]-array[i])): It means to judge whether two queens are in the same slash ( Think about )
* (3) There is no need to judge whether it is on the same line (n Has been increasing )
* */
if(
array[
i]
==
array[
n]
||
Math.
abs(
n
-
i)
==
Math.
abs(
array[
n]
-
array[
i])){
return
false;
}
}
return
true;
}
/** Write a method , You can output the Queen's placement */
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.
边栏推荐
- OpenHarmony应用开发之ETS开发方式中的Image组件
- 用户和组命令练习
- Start signing up CCF C ³- [email protected] chianxin: Perspective of Russian Ukrainian cyber war - Security confrontation and sanctions g
- Logseq 评测:优点、缺点、评价、学习教程
- 道路建设问题
- Task6: using transformer for emotion analysis
- Sitescms v3.1.0 release, launch wechat applet
- PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
- regular expression
- Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
猜你喜欢

Box layout of Kivy tutorial BoxLayout arranges sub items in vertical or horizontal boxes (tutorial includes source code)

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

mysql更新时条件为一查询

刚毕业的欧洲大学生,就能拿到美国互联网大厂 Offer?

MyCms 自媒体商城 v3.4.1 发布,使用手册更新

MySQL constraints

Kivy教程之 如何自动载入kv文件

Libuv库 - 设计概述(中文版)

AI 考高数得分 81,网友:AI 模型也免不了“内卷”!

PowerPoint 教程,如何在 PowerPoint 中将演示文稿另存为视频?
随机推荐
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [sqlserver2012 comprehensive exercise]
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
Kotlin - improved decorator mode
[sort] bucket sort
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
JS 将伪数组转换成数组
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
OpenHarmony应用开发之ETS开发方式中的Image组件
2022-02-09 survey of incluxdb cluster
The reasons why there are so many programming languages in programming internal skills
Kivy教程之 盒子布局 BoxLayout将子项排列在垂直或水平框中(教程含源码)
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
剑指 Offer 17. 打印从1到最大的n位数
Sword finger offer 12 Path in matrix
Logback log framework
71 articles on Flink practice and principle analysis (necessary for interview)
Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
剑指 Offer 14- I. 剪绳子
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)