java使用回溯法求解数独示例

时间:2023-08-17 14:39:10 


import java.util.Calendar;
import java.util.Date;

public class Matrix {

 private int matrix[][];
 private long timeAfter=0;
 private long timeBefore =0;
 public Matrix(int m[][]) {
  matrix = new int[9][9];
  for (int i=0; i<9 ; i++)
   for(int j= 0; j<9; j++)
          matrix[i][j]=m[i][j];
  this.timeBefore = Calendar.getInstance().getTimeInMillis();
 }
 public void backTrack(int i, int j)
 {
  //回收系统内存资源
  System.gc();
  if( i==8 && j>=9 )
  {
   this.timeAfter = Calendar.getInstance().getTimeInMillis();
   //成功输出矩阵
   this.showMatrix();
   return;
  }

  if(j == 9) {j = 0; i++;}

  if(matrix[i][j] == 0)
  {
   //数字为零
      for(int k=1; k<=9; k++)
      {
       if(bound(i,j,k))
       {
        matrix[i][j] = k ;
        //符合条件,查找下一个方格
        backTrack(i,j+1);
        matrix[i][j] = 0 ;
       }
      }
  }else
  {
      //数字不为零,直接查找下一个
      backTrack(i, j+1);
  }
 }
 /**
  * 判断要填入的数字和同行同列以及同一九宫格内数字是否重复
  */
 private boolean bound(int i, int j, int k) {
  int m = i/3;
  int n = j/3;
  for(int p = 0; p<9; p++)
  {
   if(k == matrix[i][p])
   {
    return false;
   }
   if(k == matrix[p][j])
   {
    return false;
   }
   if(k == matrix[3*m+p/3][3*n+p%3])
   {
    return false;
   }
  }
  return true;
 }
 /**
  * 打印解题时间
  * @return
  */
 public long printTime()
 {
  return this.timeAfter-this.timeBefore;
 }
 /**
  * 打印矩阵
  */
 public void showMatrix()
 {
  for(int i=0; i<9; i++)
  {
   for(int j=0; j<9; j++)
   {
    System.out.print(matrix[i][j]+" ");
   }
   System.out.println ();
  }
  System.out.println ();
     System.out.println("解题时间: "+printTime()+"毫秒");
  System.out.println ();
 }
 public static void main(String[] args) {
     int matrix[][] = {
       {3,0,6,0,5,7,0,0,0},
       {7,9,0,0,2,4,0,0,0},
       {0,5,0,6,0,0,9,7,4},
       {8,0,1,0,0,9,0,0,0},
       {0,2,0,3,0,8,0,0,7},
       {4,0,0,0,6,0,5,0,0},
       {0,0,4,0,3,6,0,5,0},
       {2,0,3,7,0,5,0,0,1},
       {0,0,7,4,1,0,6,0,0}};

     int ma1[][]={
     {0,3,0,0,0,5,0,6,0},
     {0,1,0,0,0,3,0,8,0},
     {0,4,0,0,0,0,0,0,7},
     {0,0,7,0,2,4,0,0,0},
     {5,0,0,0,9,0,0,0,0},
     {0,8,0,3,0,0,5,0,0},
     {0,0,0,8,0,0,0,0,0},
     {0,0,9,0,0,0,0,7,3},
     {0,5,0,9,0,0,0,0,2}};

     int ma2[][]={
     {0,0,0,0,8,4,0,0,0},//8
     {0,0,0,2,0,3,0,8,0},
     {8,3,0,9,0,0,0,5,0},
     {0,5,3,0,9,0,7,0,0},
     {0,0,0,6,3,7,0,4,5},//7
     {0,7,0,5,0,0,0,0,0},
     {0,0,6,8,0,0,0,0,0},
     {3,0,0,0,2,9,0,0,0},
     {2,0,9,3,0,0,0,0,1}};//3

     // 号称世界上最难数独
     int[][] sudoku = {
     { 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, 6, 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 }};

     Matrix m = new Matrix(sudoku);
     m.backTrack(0, 0);
 }

}

标签:回溯法,数独
0
投稿

猜你喜欢

  • java文件操作之java写文件简单示例

    2023-10-21 14:37:13
  • 详解 c# 克隆

    2021-12-16 16:23:12
  • 自己写的java日志类和方法代码分享

    2021-08-18 19:34:13
  • Java多线程之同步工具类CountDownLatch

    2023-10-23 22:17:51
  • 用C语言实现五子棋小游戏

    2023-01-19 12:56:58
  • C#使用udp如何实现消息的接收和发送

    2022-05-26 20:56:01
  • Spring BeanPostProcessor接口使用详解

    2022-12-18 09:19:36
  • Android实现遮罩层(蒙板)效果

    2023-04-26 18:43:03
  • springcloud-gateway整合jwt+jcasbin实现权限控制的详细过程

    2023-11-20 12:57:09
  • 浅谈MyBatis中@MapKey的妙用

    2023-09-25 10:42:02
  • java 单例模式的实例详解

    2023-10-08 05:14:08
  • C#创建一个Word并打开的方法

    2022-08-04 23:33:13
  • c#之滚动字幕动画窗体的实现详解

    2022-02-14 08:46:19
  • ZooKeeper 实现分布式锁的方法示例

    2023-03-20 07:26:43
  • C#微信公众号开发之消息处理

    2023-11-10 01:10:53
  • 浅谈Java数值类型的转换与强制转换

    2022-07-01 15:24:00
  • Java模拟qq软件的详细过程

    2022-01-27 15:06:19
  • 从java源码分析线程池(池化技术)的实现原理

    2021-12-24 00:01:24
  • Andriod使用多线程实现轮播图片

    2023-07-27 15:47:31
  • Android studio 出现 Unsupported major.minor version 52.0解决办法

    2023-12-01 18:30:53
  • asp之家 软件编程 m.aspxhome.com