当前位置:网站首页>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.
边栏推荐
- The network card fails to start after the cold migration of the server hard disk
- Typeerror resolved: argument 'parser' has incorrect type (expected lxml.etree.\u baseparser, got type)
- Finite State Machine FSM
- Flink SQL knows why (13): is it difficult to join streams? (next)
- 18W word Flink SQL God Road manual, born in the sky
- [Database Principle and Application Tutorial (4th Edition | wechat Edition) Chen Zhibo] [Chapter IV exercises]
- Sword finger offer 11 Rotate the minimum number of the array
- Detailed explanation of multithreading
- 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
- [colab] [7 methods of using external data]
猜你喜欢
Sword finger offer 14- ii Cut rope II
【电脑插入U盘或者内存卡显示无法格式化FAT32如何解决】
Resolved (error in viewing data information in machine learning) attributeerror: target_ names
CVPR 2022 | 美团技术团队精选6篇优秀论文解读
Flink SQL knows why (VIII): the wonderful way to parse Flink SQL tumble window
Tutoriel PowerPoint, comment enregistrer une présentation sous forme de vidéo dans Powerpoint?
Setting up remote links to MySQL on Linux
已解决TypeError: Argument ‘parser‘ has incorrect type (expected lxml.etree._BaseParser, got type)
Today's sleep quality record 77 points
【R】 [density clustering, hierarchical clustering, expectation maximization clustering]
随机推荐
Multi table query of MySQL - multi table relationship and related exercises
PowerPoint 教程,如何在 PowerPoint 中將演示文稿另存為視頻?
Setting up Oracle datagurd environment
Finite State Machine FSM
剑指 Offer 17. 打印从1到最大的n位数
MySQL installation, uninstallation, initial password setting and general commands of Linux
Tencent cloud tdsql database delivery and operation and maintenance Junior Engineer - some questions of Tencent cloud cloudlite certification (TCA) examination
[colab] [7 methods of using external data]
这本数学书AI圈都在转,资深ML研究员历时7年之作,免费电子版可看
【历史上的今天】7 月 3 日:人体工程学标准法案;消费电子领域先驱诞生;育碧发布 Uplay
父亲和篮球
R语言gt包和gtExtras包优雅地、漂亮地显示表格数据:nflreadr包以及gtExtras包的gt_plt_winloss函数可视化多个分组的输赢值以及内联图(inline plot)
Kivy tutorial how to load kV file design interface by string (tutorial includes source code)
剑指 Offer 14- II. 剪绳子 II
编程内功之编程语言众多的原因
Slf4j log facade
json序列化时案例总结
Reptile
Logback 日志框架
剑指 Offer 12. 矩阵中的路径