你一定听说过“数独”游戏。
如【图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
解题思路: 数据量小,回溯法的效率是可观的。
import java.util.Scanner;
/**
*
*
* 数独游戏
*
测试数据1:
0 0 5 3 0 0 0 0 0
8 0 0 0 0 0 0 2 0
0 7 0 0 1 0 5 0 0
4 0 0 0 0 5 3 0 0
0 1 0 0 7 0 0 0 6
0 0 3 2 0 0 0 8 0
0 6 0 5 0 0 0 0 9
0 0 4 0 0 0 0 3 0
0 0 0 0 0 9 7 0 0
测试数据2:
8 0 0 0 0 0 0 0 0
0 0 3 6 0 0 0 0 0
0 7 0 0 9 0 2 0 0
0 5 0 0 0 7 0 0 0
0 0 0 0 4 5 7 0 0
0 0 0 1 0 0 0 3 0
0 0 1 0 0 0 0 6 8
0 0 8 5 0 0 0 1 0
0 9 0 0 0 0 4 0 0
*
*/
public class Main
{
public static boolean flag = false; // 标记是否已找到
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int arr[][] = new int [10][10];
for (int i = 1; i < arr.length; i++)
for (int j = 1; j < arr[i].length; j++)
arr[i][j] = sc.nextInt();
sc.close();
dfs(1, 1, arr);
}
private static void dfs(int x, int y, int[][] arr)
{
if (flag) return ;
if (x > 9)
{
output(arr); flag = true;
return ;
}
if (y > 9)
{
dfs(x+1, 1, arr);
}
else if (arr[x][y] != 0) dfs(x,y+1,arr);
else
{
for (int i = 1; i < 10; i++)
{
if (check(x, y, i, arr))
{
arr[x][y] = i;
dfs(x, y+1, arr);
arr[x][y] = 0;
}
}
}
}
private static boolean check(int x, int y, int num, int[][] arr)
{
// 检查x轴
for (int i = 1; i < 10; i++)
{
if (arr[x][i] == num) return false;
}
// 检查y轴
for (int i = 1; i < 10; i++)
{
if (arr[i][y] == num) return false;
}
// 检查九宫格
for (int i = (x-1)/3*3+1; i <= (x-1)/3*3+3; i++)
{
for (int j = (y-1)/3*3+1; j <= (y-1)/3*3+3; j++)
{
if (arr[i][j] == num) return false;
}
}
return true;
}
private static void output(int[][] arr)
{
for (int i = 1; i < arr.length; i++)
{
for (int j = 1; j < arr[i].length; j++)
{
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
import java.util.Scanner;
/**
*
* @author muyichun
0 0 5 3 0 0 0 0 0
8 0 0 0 0 0 0 2 0
0 7 0 0 1 0 5 0 0
4 0 0 0 0 5 3 0 0
0 1 0 0 7 0 0 0 6
0 0 3 2 0 0 0 8 0
0 6 0 5 0 0 0 0 9
0 0 4 0 0 0 0 3 0
0 0 0 0 0 9 7 0 0
1 2 3 4 5 6 7 8 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
*/
public class Main
{
public static boolean flag = false; // 标记是否已找到
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int arr[][] = new int [9][9];
for (int i = 0; i < arr.length; i++)
for (int j = 0; j < arr[i].length; j++)
arr[i][j] = sc.nextInt();
sc.close();
dfs(0, 0, arr);
}
private static void dfs(int x, int y, int[][] arr) {
if (flag) return; // 找到一组即返回
if (x > 8) { //找到
output(arr);
flag = true;
}else if (y > 8) {
dfs(x+1,0, arr);
}else if (arr[x][y] != 0) {
dfs(x, y+1, arr);
}else {
for (int i = 1; i < 10; i++) {
if (check(x, y, arr, i)) {
arr[x][y] = i;
dfs(x, y+1, arr);
arr[x][y] = 0;
}
}
}
}
private static boolean check(int x, int y, int[][]arr, int value) {
//行
for (int i = 0; i < 9; i++) {
if (arr[i][y] == value) return false;
}
//列
for (int i = 0; i < 9; i++) {
if (arr[x][i] == value) return false;
}
//九宫格3行检查
for (int i = 0; i < 3; i++) {
for (int j = 0 ; j < 3; j++) {
if ( arr[x/3*3 + i][y/3*3 + j] == value )
return false;
}
}
return true;
}
private static void output (int[][]arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}