当前位置:网站首页>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.
边栏推荐
- A large select drop-down box, village in Chaoyang District
- luoguP3694邦邦的大合唱站队
- Seven habits of highly effective people
- Brief introduction to mvcc
- In the promotion season, how to reduce the preparation time of defense materials by 50% and adjust the mentality (personal experience summary)
- Sword finger offer 17 Print from 1 to the maximum n digits
- Understanding of CPU buffer line
- [exercise 5] [Database Principle]
- When we are doing flow batch integration, what are we doing?
- regular expression
猜你喜欢
剑指 Offer 14- II. 剪绳子 II
The 35 required questions in MySQL interview are illustrated, which is too easy to understand
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
Deeply understand the mvcc mechanism of MySQL
2022-02-14 incluxdb cluster write data writetoshard parsing
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
Sword finger offer 14- ii Cut rope II
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter III exercises]
人身变声器的原理
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
随机推荐
已解决(机器学习中查看数据信息报错)AttributeError: target_names
2022-02-14 incluxdb cluster write data writetoshard parsing
Task5: multi type emotion analysis
Finite State Machine FSM
php:&nbsp; The document cannot be displayed in Chinese
Mysqlbetween implementation selects the data range between two values
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
Kotlin - improved decorator mode
我的创作纪念日:五周年
Deeply understand the mvcc mechanism of MySQL
Sitescms v3.1.0 release, launch wechat applet
[Exercice 5] [principe de la base de données]
解决 System has not been booted with systemd as init system (PID 1). Can‘t operate.
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]
显卡缺货终于到头了:4000多块可得3070Ti,比原价便宜2000块拿下3090Ti
Flink code is written like this. It's strange that the window can be triggered (bad programming habits)
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
[Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter 6 exercises]
Sword finger offer 12 Path in matrix
CVPR 2022 image restoration paper