数独-dfs
来源:互联网 发布:马氏链模型matlab编程 编辑:程序博客网 时间:2024/05/22 15:01
题目:
数独是一个我们都非常熟悉的经典游戏,运用计算机我们可以很快地解开数独难题,现在有一些简单的数独题目,请编写一个程序求解。
* 数独规则:玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字
* 并满足每一行、每一列、每一个粗线宫(3*3)内的数字均含1-9,不重复
* 输入描述:
* 输入9行,每行为空格隔开的9个数字,为0的地方就是需要填充的。
* 输出描述:
* 输出九行,每行九个空格隔开的数字,为解出的答案。
测试输入:
8 0 0 0 0 0 0 0 0
0 0 3 6 0 0 0 0 0
0 7 0 0 9 0 2 0 0
0 5 0 0 0 7 0 0 0
0 0 0 0 4 5 7 0 0
0 0 0 1 0 0 0 3 0
0 0 1 0 0 0 0 6 8
0 0 8 5 0 0 0 1 0
0 9 0 0 0 0 4 0 0
测试输出:
8 1 2 7 5 3 6 4 9
9 4 3 6 8 2 1 7 5
6 7 5 4 9 1 2 8 3
1 5 4 2 3 7 8 9 6
3 6 9 8 4 5 7 2 1
2 8 7 1 6 9 5 3 4
5 2 1 9 7 4 3 6 8
4 3 8 5 2 6 9 1 7
7 9 6 3 1 8 4 5 2
使用dfs解:
public class 数独 { public static void main(String[] args){ Scanner sc=new Scanner(System.in); int[][] data=new int[9][9]; //要判定行、列、3X3,所以三个list //每个均含数字1~9,所以是set ArrayList<HashSet<Integer>> row=new ArrayList<HashSet<Integer>>(); ArrayList<HashSet<Integer>> col=new ArrayList<HashSet<Integer>>(); ArrayList<HashSet<Integer>> squ=new ArrayList<HashSet<Integer>>(); for(int i=0;i<9;i++){ row.add(new HashSet<Integer>()); col.add(new HashSet<Integer>()); squ.add(new HashSet<Integer>()); } for(int i=0;i<9;i++){ for(int j=0;j<9;j++){ data[i][j]=sc.nextInt(); if(data[i][j]!=0){ row.get(i).add(data[i][j]); col.get(j).add(data[i][j]); //一共9个3*3方阵,i/3*3+j/3判断属于哪个方阵(不可将i/3*3约分,int是向下取整) squ.get(i/3*3+j/3).add(data[i][j]); } } } dfs(data,row,col,squ,0); //打印9*9方阵 for(int i=0;i<9;i++){ for(int j=0;j<8;j++){ System.out.print(data[i][j]+" "); } System.out.println(data[i][8]); } sc.close(); } public static boolean dfs(int[][] data,ArrayList<HashSet<Integer>> row,ArrayList<HashSet<Integer>> col,ArrayList<HashSet<Integer>> squ,int index){ if(index==81){ return true; } int m=index/9;//行 int n=index%9;//列 int k=m/3*3+n/3;//三宫 if(data[m][n]!=0){//不需要填充,继续搜索 return dfs(data,row,col,squ,index+1); }else{ for(int i=1;i<=9;i++){//可填充1~9 if(!row.get(m).contains(i) && !col.get(n).contains(i) && !squ.get(k).contains(i)){//未填充过 data[m][n]=i; row.get(m).add(i); col.get(n).add(i); squ.get(k).add(i); if(dfs(data,row,col,squ,index+1)){//该路可走 return true; } //---不可填充该数 data[m][n]=0; row.get(m).remove(i); col.get(n).remove(i); squ.get(k).remove(i); } } return false;//所有数都不能填充,死路 } }}
阅读全文
0 0
- dfs数独--poj2676
- 数独(DFS)
- 数独(DFS)
- 数独-DFS
- 数独-dfs
- 数独(DFS)
- 数独—dfs
- poj 2676 数独 ---dfs
- hdu 1426 数独, dfs
- NYOJ 722--数独【DFS】
- poj2676数独(dfs)
- DFS 2676POJSudoku数独
- HDU5547-Sodoku 数独 DFS
- dfs(0634-数独)
- Sudoku 数独问题(dfs)
- 洛谷 1784 数独 dfs
- POJ2676数独(DFS)
- hdu 3111 Sudoku(DFS数独)
- SpringMVC与ajax交互流程与实现
- 矩阵键盘
- POJ-3278 Catch that cow
- git学习------>从SVN迁移到Git之后,项目开发代码继续在SVN提交,如何同步迁移之后继续在SVN提交的代码到Git?
- Spring4.3+Hibernate5.2.10整合
- 数独-dfs
- java 实现二叉树【递归/非递归】
- bzoj 3208: 花神的秒题计划Ⅰ
- 为何将INF设置为0x3f3f3f3f
- Python多线程--(1)之基本概念
- shape与reshape
- create 与 store中的关系
- hi3516a制作内核和文件系统
- 百度DNS解析过程