八皇后算法 回溯 递归 java

简介:   八皇后算法 回溯 递归 java                            国际象棋棋盘                               其中 一种解法     算法: 1.

 

八皇后算法 回溯 递归 java

 

 


          

            国际象棋棋盘                               其中 一种解法
 
 

 

算法:

1.判断 是否是 在米字形 上

2. 递归查找 下一个,没有,返回上一行,换一个位置继续查找(n 盘 n 皇后问题,一行有且之有一个位置)

 

 



 

 

代码

import java.util.concurrent.atomic.AtomicInteger;

public class EightQueue {
	 
	public static void main(String[] args) {
		// testCheck(); 
		for (int i = 4; i < 9; i++) {
			int rows = i; //行 一排
			int cols = i; //列
			int [][] queue = new int[rows][cols];
			AtomicInteger backtrack = backtrack(queue,0,new AtomicInteger(0));
			System.out.println(i+"阶"+backtrack.get());
		}
	} 
	
	static AtomicInteger backtrack(int [][] queue,int row,AtomicInteger ai){
		int rows = queue.length ;
		int cols = queue[0].length ;  
		
		if (row == -1) {
			return ai;
		} 
		if (row == rows) { 
			ai.getAndIncrement();
			//System.out.println("第"+ai.get()+"种解法");
			//print(queue); 
			//System.out.println();
			
			return backtrack(queue, row-1,ai);
		} 
		
		int j = 0;
	    for (int i = 0 ; i< rows; i++) {
	    	if (queue[row][i] == 1) {
				j = i+1;
				queue[row][i] = 0;
			}
		}
		boolean flag =false;
		for (; j < cols; j++) {
			if (!flag && check(row, j, queue)) {
				queue[row][j] = 1; 
				flag =true;
			}
		}
		if (!flag) { 
		   return	backtrack(queue, row-1,ai);
		}
		return backtrack(queue, row+1,ai);
	}
	
	static void print(int [][] queue){ 
		int rows = queue.length;
		int cols = queue[0].length; 
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				System.out.print(queue[i][j]);
			}
			System.out.println();
		}
	}
	
	static void testCheck(){
		int rows = 8; //行 一排
		int cols = 8; //列
		int x= 1,y =1;
		
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < cols; j++) {
				int [][] queue = new int[rows][cols];
				queue[i][j] = 1;
				boolean check = check(x, y, queue);
				System.out.print(check +" ");
			}
			System.out.println();
		}
		
//		false false false true  true  true  true  true 
//		false false false false false false false false 
//		false false false true  true  true  true  true 
//		true  false true  false true  true  true  true 
//		true  false true  true  false true  true  true 
//		true  false true  true  true  false true  true 
//		true  false true  true  true  true  false true 
//		true  false true  true  true  true  true  false 
	}

	
	
	static boolean check(int x,int y ,int [][] queue){
		int rows = queue.length;
		int cols = queue[0].length;
		//判断四条线是否有冲突
		//1.横向是否有冲突
		for (int i = 0; i < cols; i++) {
		  if (queue[i][y] == 1) {
			return false;
		  }
		}
		//2.纵是否有冲突
		for (int i = 0; i < rows; i++) {
		  if (queue[x][i] == 1) {
			return false;
		  }
		}
		//3.米撇是否有冲突
		for (int i = 0; i < rows; i++) {
			  if ( y-i+x >= 0 && y-i+x <= rows-1  && queue[y-i+x][i] == 1) {
				return false;
			  }
		 }
		//4.米捺是否有冲突
		for (int i = 0; i < rows; i++) {
			  if ( y-x+i >= 0 && y-x+i <= rows-1 && queue[i][ y-x+i] == 1) {
				return false;
			  }
		 }
		return true;
	}
	
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

捐助开发者 

在兴趣的驱动下,写一个免费的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(支持支付宝和微信 以及扣扣群),没钱捧个人场,谢谢各位。

 

个人主页http://knight-black-bob.iteye.com/



 
 
 谢谢您的赞助,我会做的更好!

目录
相关文章
|
15天前
|
算法
算法系列--递归(一)--与链表有关(上)
算法系列--递归(一)--与链表有关
27 0
|
1月前
|
存储 算法 Java
Java数据结构与算法-java数据结构与算法(二)
Java数据结构与算法-java数据结构与算法
90 1
|
1天前
|
算法
数据结构与算法-递归思想
数据结构与算法-递归思想
6 0
|
3天前
|
设计模式 算法 Java
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
[设计模式Java实现附plantuml源码~行为型]定义算法的框架——模板方法模式
|
18天前
|
存储 算法 搜索推荐
【数据结构与算法】归并排序(详解:递归与非递归的归并排序 | 赠:冒泡排序和选择排序)
【数据结构与算法】归并排序(详解:递归与非递归的归并排序 | 赠:冒泡排序和选择排序)
|
18天前
|
算法 安全 Java
java代码 实现AES_CMAC 算法测试
该代码实现了一个AES-CMAC算法的简单测试,使用Bouncy Castle作为安全提供者。静态变量K定义了固定密钥。`Aes_Cmac`函数接受密钥和消息,返回AES-CMAC生成的MAC值。在`main`方法中,程序对给定的消息进行AES-CMAC加密,然后模拟接收ECU的加密结果并进行比较。如果两者匹配,输出&quot;验证成功&quot;,否则输出&quot;验证失败&quot;。辅助方法包括将字节转为16进制字符串和将16进制字符串转为字节。
|
25天前
|
搜索推荐 Java
Java排序算法
Java排序算法
18 0
|
25天前
|
搜索推荐 Java
Java基础(快速排序算法)
Java基础(快速排序算法)
24 4
|
28天前
|
存储 算法 JavaScript
Java入门高频考查算法逻辑基础知识3-编程篇(超详细18题1.8万字参考编程实现)
解决这类问题时,建议采取下面的步骤: 理解数学原理:确保你懂得基本的数学公式和法则,这对于制定解决方案至关重要。 优化算法:了解时间复杂度和空间复杂度,并寻找优化的机会。特别注意避免不必要的重复计算。 代码实践:多编写实践代码,并确保你的代码是高效、清晰且稳健的。 错误检查和测试:要为你的代码编写测试案例,测试标准的、边缘情况以及异常输入。 进行复杂问题简化:面对复杂的问题时,先尝试简化问题,然后逐步分析和解决。 沟通和解释:在编写代码的时候清晰地沟通你的思路,不仅要写出正确的代码,还要能向面试官解释你的
33 0
|
1月前
|
XML 存储 算法
Java数据结构与算法-java数据结构与算法(五)
Java数据结构与算法-java数据结构与算法
49 0