数独

  1 你一定听说过“数独”游戏。
  2 如【图1.png】,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。
  3 
  4 数独的答案都是唯一的,所以,多个解也称为无解。
  5 
  6 本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。
  7 
  8 本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。
  9 
 10 格式要求,输入9行,每行9个字符,0代表未知,其它数字为已知。
 11 输出9行,每行9个数字表示数独的解。
 12 
 13 例如:
 14 输入(即图中题目):
 15 005300000
 16 800000020
 17 070010500
 18 400005300
 19 010070006
 20 003200080
 21 060500009
 22 004000030
 23 000009700
 24 
 25 程序应该输出:
 26 145327698
 27 839654127
 28 672918543
 29 496185372
 30 218473956
 31 753296481
 32 367542819
 33 984761235
 34 521839764
 35 
 36 再例如,输入:
 37 800000000
 38 003600000
 39 070090200
 40 050007000
 41 000045700
 42 000100030
 43 001000068
 44 008500010
 45 090000400
 46 
 47 程序应该输出:
 48 812753649
 49 943682175
 50 675491283
 51 154237896
 52 369845721
 53 287169534
 54 521974368
 55 438526917
 56 796318452
 57 
 58 
 59 import java.util.Scanner;  
 60 public class Main {
 61     static int[][] d = new int[10][10];// 保存输入数独的数组1-9行,1-9列  
 62     long start;  
 63     long end;  
 64     int count = 0;  
 65   
 66     // 判断行列是否重复  
 67     boolean noLcAgain(int l, int c, int num) {  
 68         int i;  
 69         // 判断行是否重复  
 70         for (i = 1; i <= 9; i++) {  
 71             if (d[l][i] == num)// 重复返回false  
 72                 return false;  
 73         }  
 74         // 判断列是否重复  
 75         for (i = 1; i <= 9; i++) {  
 76             if (d[i][c] == num)// 重复返回false  
 77                 return false;  
 78         }  
 79         return true;
 80     }// 不重复时,返回true
 81         boolean noColorAgain(int l, int c, int num) {  
 82             int i, j;  
 83             int i1,j1;  
 84             if (l>=1&&l<=3) {  
 85                 i1 = 1;  
 86             } else if(l>=4&&l<=6) {  
 87                 i1 = 4;  
 88             } else {  
 89                 i1 = 7;  
 90             }  
 91               
 92             if (c>=1&&c<=3) {  
 93                 j1 = 1;  
 94             } else if(c>=4&&c<=6) {  
 95                 j1 = 4;  
 96             } else {  
 97                 j1 = 7;  
 98             }  
 99               
100             for (i = i1; i <= i1+2; i++)  
101                 for (j = j1; j <= j1+2; j++)  
102                     if (d[i][j] == num)  
103                         return false;// 重复返回false  
104               
105             return true;// 不重复时,返回true  
106         }
107         void print() {  
108             for (int i = 1; i <= 9; i++) {  
109                 for (int j = 1; j <= 9; j++)  
110                     System.out.print(d[i][j] + "");  
111                 System.out.println("\n");  
112             }  
113         }  
114       
115         // 深度优先搜索  
116         void dfs(int l, int c) {  
117             if (l >= 10) {// 填完数时,打印出来  
118                 end = System.nanoTime();  
119                 System.out.println("运行时间:" + (end - start) / Math.pow(10, 9) + "s");  
120                 print();  
121                 System.exit(0);  
122             }  
123       
124             int i;  
125             if (d[l][c] == 0) {// 该位置未填数字  
126                 for (i=1;i<=9;i++) {  
127                     if(noLcAgain(l, c, i)&&noColorAgain(l, c, i)) {// 要填的i与其它数字不重复  
128                         d[l][c] = i;  
129                         if(count<30) {  
130                             System.out.println("l:"+l+" c:"+c+" i:"+i);// 打印填写过程  
131                             count++;  
132                         }  
133                         dfs(l+(c+1)/10, (c+1)%10);  
134                     }  
135                 }  
136                 d[l][c] = 0;// 重新置0  
137             } else {  
138                 dfs(l+(c+1)/10, (c+1)%10);  
139             }  
140         }  
141       
142         public static void main(String[] args) {  
143             Main test = new Main();  
144             Scanner scanner = new Scanner(System.in);  
145             for (int i = 1; i <= 9; i++) {  
146                 String s = scanner.nextLine();  
147                 for (int j = 0; j < 9; j++) {  
148                     String s1 = s.charAt(j) + "";  
149                     d[i][j + 1] = Integer.parseInt(s1);  
150                 }  
151             }  
152             test.start = System.nanoTime();  
153             test.dfs(1,1);  
154         }
155 }

 

posted on 2017-11-05 13:39  jun俊  阅读(673)  评论(0)    收藏  举报

导航