一个八皇后问题代码实现
2014-11-02 20:53
330 查看
求8*8的棋盘有多少皇后位置方式。
public class Empress { private int n ; private int[] x ; private long sum ; private static int h ; public Empress(){ this.sum = 0 ; this.n = 8 ; this.x = new int[n+1]; h = 1 ; } public boolean place (int k){ for (int j = 1 ; j < k ; j++){ if ( (Math.abs(k - j)) == (Math.abs(x[j]-x[k])) || (x[j] == x[k]) ){ return false ; } } return true ; } public void backTrace (int t){ if (t > n){ sum ++ ; }else { for (int i = 1 ; i <= n ; i++){ x[t] = i ; if (place (t)){ backTrace (t+1); } } } } public long getAllValidNumbers(){ backTrace(1); return this.sum; } public static void main (String[] args){ Empress em = new Empress(); System.out.println(em.getAllValidNumbers()); } }
相关文章推荐
- 四皇后问题的代码实现(java)
- 14行Python代码实现 n 皇后问题(可求出所有结果)
- n皇后问题代码实现(已运行通过)
- c++递归实现n皇后问题代码(八皇后问题)
- “约瑟夫问题”实现代码
- C语言实现N皇后问题源代码
- C++实现 八皇后问题及其扩展N皇后问题(经典回溯算法)
- 通过8皇后问题浅析回溯法的递归实现
- 关于CLR中堆排序若干问题的代码实现
- 人狼羊菜过河问题 (Java代码实现)
- 连续邮资问题的实现代码
- 链栈代码实现及背包问题解决
- 8皇后问题(java算法实现)
- 折半查找实现算法二(递归办法)PS:编译后有一个warning,但不影响结果,代码设计上应该还有些问题
- N皇后问题的求解(C++代码)
- 数独问题的一种简单算法代码实现
- 同样一个问题的两种 java 实现代码 的比较
- java代码中实现字符编码转换(解决中文乱码问题)
- .net从DataGridView复制到Excel.代码实现。解决乱码问题
- n皇后实现代码