package com.recursion;
public class Queue8 {
//定义一个max表示共有多少个皇后
int max = 8;
//定义数组array,用于保存皇后放置位置的结果,比如arr = {0,4,7,5,2,6,1,3}
int[] array = new int[max];
static int count = 0; //共多少解法
public static void main(String[] args) {
//测试
Queue8 queue8 = new Queue8();
queue8.check(0);
System.out.println("一共有"+count+"解法");
}
//编写一个方法,放置第n个皇后
//特别主意:check 是每一次递归时,进入到check中都有for(int i = 0;i<max;i++),因此会产生回溯
private void check(int n)
{
//如果n==8,说明八个皇后已经放好了
if(n == max)
{
print();
return;
}
//如果没有到最后,依次放入皇后,并判断时候冲突
for(int i = 0;i < max;i++)
{
八皇后问题详细思路分析
最新推荐文章于 2024-12-11 14:28:05 发布