你一定听说过“数独”游戏。
如图,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。
数独的答案都是唯一的,所以,多个解也称为无解。
本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。
本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。
格式要求,输入9行,每行9个字符,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。
例如:
输入(即图中题目):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700
程序应该输出:
145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764
再例如,输入:
800000000
003600000
070090200
050007000
000045700
000100030
001000068
008500010
090000400
程序应该输出:
812753649
943682175
675491283
154237896
369845721
287169534
521974368
438526917
796318452
资源约定:
峰值内存消耗(含虚拟机) < 256M
如图,玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行、每一列、每一个同色九宫内的数字均含1-9,不重复。
数独的答案都是唯一的,所以,多个解也称为无解。
本图的数字据说是芬兰数学家花了3个月的时间设计出来的较难的题目。但对会使用计算机编程的你来说,恐怕易如反掌了。
本题的要求就是输入数独题目,程序输出数独的唯一解。我们保证所有已知数据的格式都是合法的,并且题目有唯一的解。
格式要求,输入9行,每行9个字符,0代表未知,其它数字为已知。
输出9行,每行9个数字表示数独的解。
例如:
输入(即图中题目):
005300000
800000020
070010500
400005300
010070006
003200080
060500009
004000030
000009700
程序应该输出:
145327698
839654127
672918543
496185372
218473956
753296481
367542819
984761235
521839764
再例如,输入:
800000000
003600000
070090200
050007000
000045700
000100030
001000068
008500010
090000400
程序应该输出:
812753649
943682175
675491283
154237896
369845721
287169534
521974368
438526917
796318452
资源约定:
峰值内存消耗(含虚拟机) < 256M
CPU消耗 < 2000ms
import java.util.Scanner;
public class Main1 {
static int[][] d = new int[10][10];// 保存输入数独的数组1-9行,1-9列
long start;
long end;
int count = 0;
// 判断行列是否重复
boolean noLcAgain(int l, int c, int num) {
int i;
// 判断行是否重复
for (i = 1; i <= 9; i++) {
if (d[l][i] == num)// 重复返回false
return false;
}
// 判断列是否重复
for (i = 1; i <= 9; i++) {
if (d[i][c] == num)// 重复返回false
return false;
}
return true;// 不重复时,返回true
}
// 判断同色九宫格是否重复
boolean noColorAgain(int l, int c, int num) {
int i, j;
int i1,j1;
if (l>=1&&l<=3) {
i1 = 1;
} else if(l>=4&&l<=6) {
i1 = 4;
} else {
i1 = 7;
}
if (c>=1&&c<=3) {
j1 = 1;
} else if(c>=4&&c<=6) {
j1 = 4;
} else {
j1 = 7;
}
for (i = i1; i <= i1+2; i++)
for (j = j1; j <= j1+2; j++)
if (d[i][j] == num)
return false;// 重复返回false
return true;// 不重复时,返回true
}
// 打印
void print() {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++)
System.out.print(d[i][j] + "");
System.out.println("\n");
}
}
// 深度优先搜索
void dfs(int l, int c) {
if (l >= 10) {// 填完数时,打印出来
end = System.nanoTime();
System.out.println("运行时间:" + (end - start) / Math.pow(10, 9) + "s");
print();
System.exit(0);
}
int i;
if (d[l][c] == 0) {// 该位置未填数字
for (i=1;i<=9;i++) {
if(noLcAgain(l, c, i)&&noColorAgain(l, c, i)) {// 要填的i与其它数字不重复
d[l][c] = i;
if(count<30) {
System.out.println("l:"+l+" c:"+c+" i:"+i);// 打印填写过程
count++;
}
dfs(l+(c+1)/10, (c+1)%10);
}
}
d[l][c] = 0;// 重新置0
} else {
dfs(l+(c+1)/10, (c+1)%10);
}
}
public static void main(String[] args) {
Main1 test = new Main1();
Scanner scanner = new Scanner(System.in);
for (int i = 1; i <= 9; i++) {
String s = scanner.nextLine();
for (int j = 0; j < 9; j++) {
String s1 = s.charAt(j) + "";
d[i][j + 1] = Integer.parseInt(s1);
}
}
test.start = System.nanoTime();
test.dfs(1,1);
}
}