FreeCMS商业版

功能强大、开源、代码通俗易懂、注释详细、面向二次开发友好!

承接网站建设、软件开发、微信开发项目,价格从优,欢迎咨询合作!

FreeCMS商业版V2.8已发布!购买请联系客服QQ 124812878 查看更新日志



在线演示 立即购买 下载免费版 下载商业版用户手册
客服QQ: 124812878 联系电话: 18339991503 Email: freeteam@foxmail.com

八皇后算法 回溯 递归 java - Ctrl+C&Ctrl+V - ITeye博客

加入收藏夹】     【打印】     【关闭】 来源: 日期:2018-03-07 22:00:02 点击量: 收藏

 

八皇后算法 回溯 递归 java

 

 


          

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

 

算法:

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

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

 

 



 

 

代码

import java.util.concurrent.atomic.AtomicInteger;

public class EightQueue {
	 
	public static void main(String[] args) {
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/ testCheck(); 
		for (int i = 4; i < 9; i++) {
			int rows = i; http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/行 一排
			int cols = i; http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/列
			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();
			http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/System.out.println("第"+ai.get()+"种解法");
			http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/print(queue); 
			http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/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; http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/行 一排
		int cols = 8; http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/列
		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();
		}
		
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		false false false true  true  true  true  true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		false false false false false false false false 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		false false false true  true  true  true  true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		true  false true  false true  true  true  true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		true  false true  true  false true  true  true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		true  false true  true  true  false true  true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		true  false true  true  true  true  false true 
http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/		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;
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/判断四条线是否有冲突
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/1.横向是否有冲突
		for (int i = 0; i < cols; i++) {
		  if (queue[i][y] == 1) {
			return false;
		  }
		}
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/2.纵是否有冲突
		for (int i = 0; i < rows; i++) {
		  if (queue[x][i] == 1) {
			return false;
		  }
		}
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/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;
			  }
		 }
		http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/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:http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/http:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp:http://knight-black-bob.iteye.com/http://knight-black-bob.iteye.com/knight-black-bob.iteye.comhttp://knight-black-bob.iteye.com/



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


上一条


下一条

相关新闻
自定义表单

本例固定链接: 分享到:
更多