你一定听说过“数独”游戏。
如【图1.png】,玩家需要根据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
请严格按要求输出,不要画蛇添足地打印类似:“请您输入...”的多余内容。
所有代码放在同一个源文件中,调试通过后,拷贝提交该源码。
注意:不要使用package语句。不要使用jdk1.7及以上版本的特性。
注意:主类的名字必须是:Main,否则按无效代码处理。
思路:
遍历整个数组,若当前位置为0,则填入1,判断行列上和九宫格内有无数字与1重复,若无重复,则进入下一个位置,若有重复,则填入2,3,4,...9.以此类推
import java.util.Scanner;
public class Sudoku {
long end;
long start;
static int[][] map = new int[9][9];
public Sudoku() {
super();
}
/**
* 判断行列是否有重复
*
* @param row
* @param column
* @param value
* @return
*/
boolean rowColumnIsNotRepeat(int row, int column, int value) {
// 判断列
for (int i = 0; i < 9; i++) {
if (map[i][column] == value) {
return false;
}
}
// 判断行
for (int i = 0; i < 9; i++) {
if (map[row][i] == value) {
return false;
}
}
return true;
}
/**
* 判断同一九宫格内是否有重复
*/
boolean sectionIsNotRepeat(int row, int column, int value) {
int sectionRow;
int sectionColumm;
if (row <= 2) {
sectionRow = 0;
} else if (row <= 5) {
sectionRow = 3;
} else {
sectionRow = 6;
}
if (column <= 2) {
sectionColumm = 0;
} else if (column <= 5) {
sectionColumm = 3;
} else {
sectionColumm = 6;
}
for (int i = sectionRow; i < sectionRow + 3; i++) {
for (int j = sectionColumm; j < sectionColumm + 3; j++) {
if (map[i][j] == value) {
return false;
}
}
}
return true;
}
void dfs(int row, int column) {
//填完后显示结果和时间(毫秒)
if(row >= 9) {
print();
end = System.currentTimeMillis();
System.out.println(end - start + "ms");
System.exit(0);
}
if (map[row][column] == 0) {
//从1到9挨个试
for (int i = 1; i <= 9; i++) {
if (rowColumnIsNotRepeat(row, column, i) && sectionIsNotRepeat(row, column, i)) {
map[row][column] = i;
dfs(row + (column + 1) / 9, (column + 1) % 9);
}
}
//试完后都不行,返回上一层
map[row][column] = 0;
return;
} else {
//如果当前位置有数字,进入下一个位置
dfs(row + (column + 1) / 9, (column + 1) % 9);
}
}
void print() {
for(int i = 0; i < 9; i++) {
for(int j = 0; j < 9; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
public static void main(String[] args) {
Sudoku sudoku = new Sudoku();
Scanner scanner = new Scanner(System.in);
String str;
char[] a;
System.out.println("Please input");
for(int i = 0; i < 9; i++) {
str = scanner.nextLine();
a = str.toCharArray();
for(int j = 0; j < 9; j++) {
map[i][j] = Integer.parseInt(a[j] + "");
}
}
if(scanner != null) {
try {
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
sudoku.start = System.currentTimeMillis();
sudoku.dfs(0, 0);
}
}