回溯法 8皇后
来源:互联网 发布:java项目流程 编辑:程序博客网 时间:2024/05/16 18:35
问题太经典,就不描述问题了:
public class HuangHou8 {static int n=8;static int tot=0;static int[] C;static int[][] vis=new int[3][16];public static void main(String[] args) {tot=0;C=new int[8];search(0);System.out.println(tot);tot=0;C=new int[8];search2(0);System.out.println(tot);}public static void search(int cur){if(cur==n){tot++;for (int i = 0; i < C.length; i++) {System.out.print(C[i]+" ");}System.out.println();}else{for (int i = 0; i < n; i++) {int ok=1;C[cur]=i;for (int j = 0; j < cur; j++) {if(C[cur]==C[j]||cur-C[cur]==j-C[j]||cur+C[cur]==j+C[j]){ok=0;break;}}if(ok==1){search(cur+1);}}}}public static void search2(int cur){if(cur==n){tot++;}else{for (int i = 0; i < n; i++) {if(vis[0][i]==0&&vis[1][cur+i]==0&&vis[2][cur-i+n]==0){C[cur]=i;vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=1;search2(cur+1);vis[0][i]=vis[1][cur+i]=vis[2][cur-i+n]=0;}}}}}
- 回溯法 8皇后
- 8皇后回溯法
- 回溯法---->8-皇后问题
- 8皇后问题------回溯法
- 回溯法解决2n皇后(8皇后)问题
- 回溯法求解8皇后问题(转帖)
- 回溯法求解8皇后问题
- 回溯法之二---8皇后问题
- 8皇后-----回溯法C++编程练习
- 回溯法——8皇后问题
- 回溯法 解决 8皇后问题
- 回溯法解决8皇后问题
- 八皇后回溯法
- 回溯法, 八皇后
- 八皇后,回溯法
- 回溯法-n皇后
- 八皇后 回溯法
- 回溯法八皇后
- FZU 2102 Solve equation
- 数组大折腾(任务2)
- 第一章
- 删除桌面无法删除的网页快捷方式
- Joomla 3.2取消注册验证的Agree radio
- 回溯法 8皇后
- memset的使用
- FZU 2104 Floor problem
- java.lang.OutOfMemoryError: bitmap size exceeds VM budget
- 怎样连接Oracle数据库
- 成为优秀程序员的方法就是抛开编程?
- SGU 195 New Year Bonus Grant(树形dp)
- makefile的全面深入浅出教程
- tomcat部署web项目的几种方法