腾讯的新闻称中国老头三天破解世界最难九宫格

来源:互联网 发布:民乐队服装大全淘宝 编辑:程序博客网 时间:2024/04/29 07:59

今日,一则腾讯的新闻称中国老头三天破解世界最难九宫格,虽然最后老人是改了一个数字,但是引起本人一时兴趣,想通过计算机程序求解该问题,于是在宿舍呆了一下午,终于成功求解,程序源码如下。​

  1. package numberGame; 
  2.  
  3.  
  4. public class Point { 
  5.     private int col;// 行号 
  6.     private int row;// 列号 
  7.     private boolean flag;// 真为未设置。 
  8.     private int value; 
  9.     // 构造点 
  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();//改变标记为假。表示已设置过。 
  31.                         break
  32.                     }else{//满足不条件,冲突。value值自加一次 
  33.                     }    
  34.                      
  35.                     while(v == 9){//如果1-9都不能满足要求,则先将本位重置为0,并回退一格,给回退到的位置的值加一(当回退位置的值不为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])){//如果回退到的位置为九宫格本来的点时,继续回退,直到不是本身的点时跳出while。 
  42.                             j--; 
  43.                             if(j==-1){ 
  44.                                 i--;j=8
  45.                             } 
  46.                         } 
  47.                         numMat[i][j].changeFlag();//将标记 
  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值),输入结束输入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.  
0 0
原创粉丝点击