当前位置:网站首页>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.
边栏推荐
- Can newly graduated European college students get an offer from a major Internet company in the United States?
- JSP and filter
- 人身变声器的原理
- Server coding bug
- Today's sleep quality record 77 points
- Ubuntu 14.04 下开启PHP错误提示
- Task5: multi type emotion analysis
- Luogup3694 Bangbang chorus standing in line
- PowerPoint tutorial, how to save a presentation as a video in PowerPoint?
- 2022-02-11 heap sorting and recursion
猜你喜欢
JSP and filter
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Elk note 24 -- replace logstash consumption log with gohangout
AI 考高数得分 81,网友:AI 模型也免不了“内卷”!
Introduction to the implementation principle of rxjs observable filter operator
Seven habits of highly effective people
The shortage of graphics cards finally came to an end: 3070ti for more than 4000 yuan, 2000 yuan cheaper than the original price, and 3090ti
今日睡眠质量记录77分
Flink SQL knows why (12): is it difficult to join streams? (top)
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
随机推荐
In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
常见的几种最优化方法Matlab原理和深度分析
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Will Huawei be the next one to fall
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
rxjs Observable filter Operator 的实现原理介绍
My creation anniversary: the fifth anniversary
Comprehensive evaluation of double chain notes remnote: fast input, PDF reading, interval repetition / memory
mysqlbetween实现选取介于两个值之间的数据范围
服务器硬盘冷迁移后网卡无法启动问题
The principle of human voice transformer
正则表达式
2022-02-11 practice of using freetsdb to build an influxdb cluster
Swiftui development experience: the five most powerful principles that a programmer needs to master
双链笔记 RemNote 综合评测:快速输入、PDF 阅读、间隔重复/记忆
2022-02-14 analysis of the startup and request processing process of the incluxdb cluster Coordinator
Image component in ETS development mode of openharmony application development
Sword finger offer 15 Number of 1 in binary
Cadre de logback
Red Hat Satellite 6:更好地管理服务器和云