數獨演算法原始碼(未用遞迴)

來源:互聯網
上載者:User
/**
 * 數獨程式
 */
public class ShuDu {
      /**儲存數位數組*/
      static int[][] n = new int[9][9];
      /**產生隨機數位源數組,隨機數字從該數組中產生*/
      static int[] num = {1,2,3,4,5,6,7,8,9};
      public static void main(String[] args) {
             //產生數字
            for(int i = 0;i < 9;i++){
                     //嘗試填充的數字次數
                     int time = 0;
                    //填充數字
                     for(int j = 0;j < 9;j++){
                            //產生數字
                            n[i][j] = generateNum(time);    
                           //如果傳回值為0,則代表卡住,退回處理
                           //退回處理的原則是:如果不是第一列,則先倒退到前一列,否則倒退到前一行的最後一列
                           if(n[i][j] == 0){
                                   //不是第一列,則倒退一列
                                   if(j > 0){
                                               j-=2;
                                               continue;
                                  }else{//是第一列,則倒退到上一行的最後一列
                                              i--;
                                              j = 8;
                                              continue;
                                  }
                            } 
                           //填充成功
                            if(isCorret(i,j)){
                                    //初始化time,為下一次填充做準備
                                  time = 0;
                            }else{ //繼續填充
                                 //次數增加1
                                 time++;
                                //繼續填充當前格
                                 j--;
                           }    
                    }
             }
            //輸出結果
            for(int i = 0;i < 9;i++){
                       for(int j = 0;j < 9;j++){
                                  System.out.print(n[i][j] + "  ");
                       }
                       System.out.println();
             }
      }
  
      /**
        * 是否滿足行、列和3X3地區不重複的要求
        * @param row 行號
        * @param col 列號
        * @return true代表符合要求
       */
       public static boolean isCorret(int row,int col){
              return (checkRow(row) & checkLine(col) & checkNine(row,col));
       }
  
       /**
        * 檢查行是否符合要求
        * @param row 檢查的行號
        * @return true代表符合要求
        */
       public static boolean checkRow(int row){  
               for(int j = 0;j < 8;j++){
                      if(n[row][j] == 0){
                               continue;
                      }
                     for(int k =j + 1;k< 9;k++){
                              if(n[row][j] == n[row][k]){
                                       return false;
                              }
                     }
                }
               return true;
       }
  
      /**
       * 檢查列是否符合要求
       * @param col 檢查的列號
       * @return true代表符合要求
       */
      public static boolean checkLine(int col){
             for(int j = 0;j < 8;j++){
                    if(n[j][col] == 0){
                            continue;
                    }
                    for(int k =j + 1;k< 9;k++){
                            if(n[j][col] == n[k][col]){
                                     return false;
                            }
                    }
            }
           return true;
       }
  
       /**
       * 檢查3X3地區是否符合要求
       * @param row 檢查的行號
       * @param col 檢查的列號
       * @return true代表符合要求
       */
       public static boolean checkNine(int row,int col){
               //獲得左上方的座標
               int j = row / 3 * 3;
               int k = col /3 * 3;
               //迴圈比較
               for(int i = 0;i < 8;i++){
                         if(n[j + i/3][k + i % 3] == 0){
                                     continue;
                         }
                        for(int m = i+ 1;m < 9;m++){
                                    if(n[j + i/3][k + i % 3] == n[j + m/3][k + m % 3]){
                                                 return false;
                                    }
                         }
                 }
                 return true;
        }
  
      /**
       * 產生1-9之間的隨機數字
       * 規則:產生的隨機數字放置在數組8-time下標的位置,隨著time的增加,已經嘗試過的數字將不會在取到
       * 說明:即第一次次是從所有數字中隨機,第二次時從前八個數字中隨機,依次類推,
       *       這樣既保證隨機,也不會再重複取已經不符合要求的數字,提高程式的效率
       * 這個規則是本演算法的核心
       * @param time 填充的次數,0代表第一次填充
       * @return
       */
       public static int generateNum(int time){
               //第一次嘗試時,初始化隨機數字源數組
               if(time == 0){
                     for(int i = 0;i < 9;i++){
                             num[i] = i + 1;
                     }
               }
               //第10次填充,表明該位置已經卡住,則返回0,由主程式處理退回
               if(time == 9){
                        return 0;
               }  
               //不是第一次填充
               //產生隨機數字,該數字是數組的下標,取數組num中該下標對應的數字為隨機數字
              int ranNum = (int)(Math.random() * (9 - time));
              //把數字放置在數組倒數第time個位置,
              int temp = num[8 - time];
              num[8 - time] = num[ranNum];
              num[ranNum] = temp;
             //返回數字
             return num[8 - time];  
       }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.