八皇后问题及其推广
2016-01-25 14:44
423 查看
package nqueen; /** * Created by kai_li on 2016/1/25. */ public class NQueen { static int NQUEEN = 8; final static int SIZE = NQUEEN; static int COUNT = 0; static int[] cols = new int[SIZE] ; public static void place(int n) { for (int i = 0; i < SIZE; i++) { boolean compatible = compatible(n, i); if (compatible) { cols = i; if (n == SIZE - 1) { printSolution(); COUNT ++; return; } else { place(n + 1); } } else { continue; } } } public static void printSolution() { for (int col : cols) { System.out.print(col); } System.out.println(); return; } public static boolean compatible(int rowIndex, int val) { for (int i = 0; i < rowIndex; i++) { if (cols[i] == val) { return false; } if (rowIndex - i == val - cols[i] ) { return false; } if (rowIndex - i == cols[i] - val) { return false; } } return true; } public static void main(String[] args) { place(0); System.out.println(COUNT); } }
相关文章推荐
- 数字图像处理10--基于特征提取的拼接方法
- RHCE 学习笔记(12) 归档压缩和文件传递
- 周爱民:真正的架构师是没有title的(图灵访谈)
- 贪心算法,递归算法,动态规划算法比较与总结
- 机器学习算法之决策树(三)
- C#使用iTextSharp打印PDF
- Windows Server 2008 R2 RDS在线激活
- ubuntu 环境下开发android 程序,识别手机步骤。
- jquery ui dialog替代confirm实例分析
- leetcode之Remove Linked List Elements
- 开源控件库
- 语音识别书籍
- Android之数据持久化三(sqllite数据库)二
- 购买价格与商品价格和SKU价格的关系
- 编写松耦合的程序——迪米特原则
- POJ 3414 dfs 回溯
- iOS 在viewController中监听Home键触发以及重新进入界面的方法
- 验证码实用工具Jtaptcha
- dev使用3
- iOS--BaiduMap自定义annotation和annotationView