自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

基于Java:解荷蘭數(shù)學(xué)家設(shè)計世界最難九宮格

開發(fā) 后端
芬蘭數(shù)學(xué)家因卡拉花費3個月設(shè)計出了世界上迄今難度最大的數(shù)獨游戲,而且它只有一個答案。因卡拉說只有思考能力最快、頭腦最聰明的人才能破解這個游戲。

今日,一則騰訊的新聞稱中國老頭三天破解世界最難九宮格,雖然最后老人是改了一個數(shù)字,但是引起本人一時興趣,想通過計算機程序求解該問題,于是在宿舍呆了一下午,終于成功求解,程序源碼如下。​

  1. package numberGame; 
  2.  
  3.  
  4. public class Point { 
  5.     private int col;// 行號 
  6.     private int row;// 列號 
  7.     private boolean flag;// 真為未設(shè)置。 
  8.     private int value; 
  9.     // 構(gòu)造點 
  10.     public Point(int col, int row, boolean flag, int value) { 
  11.         super(); 
  12.         this.col = col; 
  13.         this.row = row; 
  14.         this.flag = flag; 
  15.         this.value = value; 
  16.     } 
  17.  
  18.     public void changeFlag() { 
  19.         flag = !flag; 
  20.     } 
  21.  
  22.  
  23.     public boolean getFlag() { 
  24.         return flag; 
  25.     } 
  26.  
  27.     public int getValue() { 
  28.         return value; 
  29.     } 
  30.  
  31.     public void setValue(int value) { 
  32.         this.value = value; 
  33.     } 
  34.  
  35.     public boolean canHere(Point[][] pArr) { 
  36.         boolean cb = canCol(pArr); 
  37.         boolean cr = canRow(pArr); 
  38.         boolean cminiArr = canMiniArr(pArr); 
  39.         return cb && cr && cminiArr; 
  40.     } 
  41.     //判斷在小3*3格子里是否有相同元素 
  42.     private boolean canMiniArr(Point[][] pArr) { 
  43.         int coltemp = this.col % 3
  44.         int rowtemp = this.row % 3
  45.  
  46.         for (int i = this.col - coltemp; i < col + (3 - coltemp); i++) { 
  47.             for (int j = this.row - rowtemp; j < row + (3 - rowtemp); j++) { 
  48.                 if(i == this.col && j == this.row){ 
  49.                     continue
  50.                 }else{               
  51.                     if(this.value == pArr[i][j].getValue()){ 
  52.                         return false
  53.                     }                
  54.                 } 
  55.             } 
  56.         } 
  57.         return true
  58.     } 
  59.  
  60.     // 判斷列上是否有相同元素 
  61.     private boolean canRow(Point[][] pArr) { 
  62.         for (int i = 0; i < 9; i++) { 
  63.             if (i == this.col) { 
  64.                 continue
  65.             } else { 
  66.                 if (this.value == pArr[i][this.row].value) {// 行變,列不變 
  67.                     return false
  68.                 } 
  69.             } 
  70.         } 
  71.         return true
  72.     } 
  73.  
  74.     // 判斷行上是否有相同元素 
  75.     private boolean canCol(Point[][] pArr) { 
  76.         for (int i = 0; i < 9; i++) { 
  77.             if (i == this.row) { 
  78.                 continue
  79.             } else { 
  80.                 if (this.value == pArr[this.col][i].value) {// 列邊,行不變 
  81.                     return false
  82.                 } 
  83.             } 
  84.         } 
  85.         return true
  86.     } 
  87. }

-----主程序

  1. package numberGame; 
  2.  
  3. import java.io.BufferedReader; 
  4. import java.io.IOException; 
  5. import java.io.InputStreamReader; 
  6. import java.util.ArrayList; 
  7.  
  8.  
  9. public class Number99 { 
  10.  
  11.     public static void main(String[] args) throws IOException{ 
  12.         Point[][] numMat = new Point[9][9]; 
  13.         ArrayList<Point> al = new ArrayList<Point>(); 
  14.          
  15.         initNumMat(numMat,al); 
  16.          
  17.          
  18.         setNum(numMat,al); 
  19.         printMat(numMat); 
  20.     } 
  21.  
  22.     private static void setNum(Point[][] numMat,ArrayList<Point> al) { 
  23.         int i = 0
  24.         int j = 0
  25.         do
  26.             if (numMat[i][j].getFlag()) { 
  27.                 for (int v = numMat[i][j].getValue()+1; v <= 9; v++) {//給回退到的位置的值加一。 
  28.                     numMat[i][j].setValue(v); 
  29.                     if (numMat[i][j].canHere(numMat)) {//滿足條件,不沖突。 
  30.                         numMat[i][j].changeFlag();//改變標(biāo)記為假。表示已設(shè)置過。 
  31.                         break
  32.                     }else{//滿足不條件,沖突。value值自加一次 
  33.                     }    
  34.                      
  35.                     while(v == 9){//如果1-9都不能滿足要求,則先將本位重置為0,并回退一格,給回退到的位置的值加一(當(dāng)回退位置的值不為9時,并且保證回退到的位置不是九宮格原本的點)。 
  36.                         numMat[i][j].setValue(0); 
  37.                         j--; 
  38.                         if(j==-1){ 
  39.                             i--;j=8
  40.                         } 
  41.                         while(al.contains(numMat[i][j])){//如果回退到的位置為九宮格本來的點時,繼續(xù)回退,直到不是本身的點時跳出while。 
  42.                             j--; 
  43.                             if(j==-1){ 
  44.                                 i--;j=8
  45.                             } 
  46.                         } 
  47.                         numMat[i][j].changeFlag();//將標(biāo)記 
  48.                         v = numMat[i][j].getValue(); 
  49.                     } 
  50.                 }            
  51.             } 
  52.             j++; 
  53.             if(j==9){ 
  54.                 j=0;i++;//此處i++ 可能使i自加為9,故下面需要i!=9判斷 
  55.             } 
  56.             if(i!=9){            
  57.                 while(al.contains(numMat[i][j])){ 
  58.                     j++; 
  59.                     if(j==9){ 
  60.                         j=0;i++; 
  61.                     } 
  62.                 } 
  63.             } 
  64.         }while(i!=9); 
  65.  
  66.     } 
  67.  
  68.     public static void initNumMat(Point[][] numMat,ArrayList<Point> al) throws IOException { 
  69.         for (int i = 0; i < numMat.length; i++) { 
  70.             for (int j = 0; j < numMat[i].length; j++) { 
  71.                 numMat[i][j] = new Point(i, j, true0); 
  72.             } 
  73.         } 
  74.         initNumMat2(numMat, al); 
  75.  
  76.     } 
  77.  
  78.     public static void initNumMat2(Point[][] numMat, ArrayList<Point> al) throws IOException { 
  79.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
  80.         String[] p = new String[3]; 
  81.         String line=null
  82.         System.out.println("請按格式輸入點信息(i行號, j列號 v值),輸入結(jié)束輸入over: i j v "); 
  83.          
  84.         while((line = br.readLine())!=null){ 
  85.             if(line.equals("over")) 
  86.                 break
  87.             p = line.trim().split(" +"); 
  88.             numMat[Integer.parseInt(p[0])][Integer.parseInt(p[1])].setValue(Integer.parseInt(p[2])); 
  89.             numMat[Integer.parseInt(p[0])][Integer.parseInt(p[1])].changeFlag(); 
  90.             al.add(numMat[Integer.parseInt(p[0])][Integer.parseInt(p[1])]); 
  91.         } 
  92.     } 
  93.  
  94.     public static void printMat(Point[][] numMat) { 
  95.         System.out.println("--------┬---------┬---------┐"); 
  96.  
  97.         for (int i = 0; i < numMat.length; i++) { 
  98.             for (int j = 0; j < numMat[i].length; j++) { 
  99.                 if ((j + 1) % 3 == 0
  100.                     System.out.print(numMat[i][j].getValue() + " | "); 
  101.                 else 
  102.                     System.out.print(numMat[i][j].getValue() + "  "); 
  103.             } 
  104.             if ((i + 1) % 3 == 0
  105.                 System.out.println("\r\n--------┼---------┼---------┤"); 
  106.             else 
  107.                 System.out.println(); 
  108.         } 
  109.     } 
  110.  

#p#

-------運行程序

請按格式輸入點信息(i行號, j列號 v值),輸入結(jié)束輸入over: i j v
0 0 8
1 2 3
1 3 6
2 1 7
2 4 9
2 6 2
3 1 5
3 5 7
4 4 4
4 5 5
4 6 7
5 3 1
5 7 3
6 2 1
6 7 6
6 8 8
7 2 8
7 3 5
7 7 1
8 1 9
8 6 4
over
--------┬---------┬---------┐
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 |
--------┼---------┼---------┤

原文鏈接:http://blog.csdn.net/liuhenghui5201/article/details/8987311

 

責(zé)任編輯:陳四芳 來源: csdn博客
相關(guān)推薦

2011-08-01 13:13:19

iPhone開發(fā) 圖片

2015-01-26 13:22:55

密碼鎖

2019-03-24 15:05:23

手機安全解鎖

2018-08-14 13:34:30

商業(yè)模式

2015-03-05 15:27:22

2021-12-31 08:12:05

微信微信支付云閃付

2013-07-30 11:16:33

iOS 7 Beta 九宮格輸入法

2022-04-21 21:49:57

元宇宙

2011-09-16 10:35:13

Android應(yīng)用數(shù)獨經(jīng)典游戲

2023-04-20 08:26:17

九宮格布局客戶端

2020-09-16 12:10:19

九宮格視頻Python文件

2017-03-29 15:41:45

互聯(lián)網(wǎng)

2013-10-31 11:19:09

微軟語音翻譯手語翻譯

2022-03-03 18:49:56

HarmonyOS鴻蒙操作系統(tǒng)

2022-05-16 15:23:46

人工智能工具科學(xué)計算

2011-05-25 19:35:25

2021-10-12 16:39:39

支付寶口碑移動應(yīng)用

2012-04-13 16:35:02

傲游手機瀏覽器發(fā)布

2023-05-31 10:08:51

2024-05-20 15:40:00

AI數(shù)學(xué)
點贊
收藏

51CTO技術(shù)棧公眾號