你一定听说过“数独”游戏。
如【图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();
}
}
}
---补充2025年10月18日,面试官要求手撕判断一个二维数组是否满足数独的条件:
import java.util.*;
/**
合法1:
2 4 6 1 3 5 7 8 9
5 8 3 2 7 9 1 6 4
9 1 7 6 8 4 5 2 3
1 3 2 5 6 8 4 9 7
4 5 8 7 9 1 6 3 2
6 7 9 4 2 3 8 1 5
8 2 4 9 5 6 3 7 1
3 9 1 8 4 7 2 5 6
7 6 5 3 1 2 9 4 8
合法2:
2 5 7 1 3 6 4 8 9
3 9 6 4 8 2 5 1 7
8 4 1 5 7 9 2 3 6
4 3 9 2 1 7 8 6 5
6 1 2 3 5 8 7 9 4
5 7 8 6 9 4 3 2 1
1 2 3 7 6 5 9 4 8
9 6 5 8 4 3 1 7 2
7 8 4 9 2 1 6 5 3
合法3:
7 2 4 1 3 5 9 6 8
8 1 3 2 9 6 4 5 7
6 9 5 7 4 8 1 2 3
1 3 8 5 6 2 7 4 9
2 5 7 9 8 4 3 1 6
4 6 9 3 7 1 2 8 5
5 7 1 8 2 3 6 9 4
9 4 2 6 5 7 8 3 1
3 8 6 4 1 9 5 7 2
不合法1:
2 2 6 1 3 5 7 8 9
5 8 3 2 7 9 1 6 4
9 1 7 6 8 4 5 2 3
1 3 2 5 6 8 4 9 7
4 5 8 7 9 1 6 3 2
6 7 9 4 2 3 8 1 5
8 2 4 9 5 6 3 7 1
3 9 1 8 4 7 2 5 6
7 6 5 3 1 2 9 4 8
不合法2:
2 5 7 1 3 6 4 8 9
2 9 6 4 8 2 5 1 7
8 4 1 5 7 9 2 3 6
4 3 9 2 1 7 8 6 5
6 1 2 3 5 8 7 9 4
5 7 8 6 9 4 3 2 1
1 2 3 7 6 5 9 4 8
9 6 5 8 4 3 1 7 2
7 8 4 9 2 1 6 5 3
不合法3:
7 2 4 1 3 5 9 6 8
8 7 3 2 9 6 4 5 7
6 9 5 7 4 8 1 2 3
1 3 8 5 6 2 7 4 9
2 5 7 9 8 4 3 1 6
4 6 9 3 7 1 2 8 5
5 7 1 8 2 3 6 9 4
9 4 2 6 5 7 8 3 1
3 8 6 4 1 9 5 7 2
**/
public class Test {
static boolean fun(int[][]arr){
//1. 行
for (int i = 0; i < arr.length; i++) {
boolean[] flag = new boolean[10];
for (int j = 0; j < arr[i].length; j++) {
int cur = arr[i][j];
if (flag[cur]) return false;
else flag[cur] = true;
}
}
//2. 列
for (int i = 0; i < arr.length; i++) {
boolean[] flag = new boolean[10];
for (int j = 0; j < arr[i].length; j++) {
int cur = arr[j][i];
if (flag[cur]) return false;
else flag[cur] = true;
}
}
//3. 九宫格
for (int t = 0; t < 9; t++) {
int x = (t / 3) * 3;
int y = (t * 3) % 9;
boolean[] flag = new boolean[10];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int cur = arr[x + i][y + j];
if (flag[cur]) return false;
else flag[cur] = true;
}
}
}
return true;
}
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();
}
}
System.out.println(fun(arr));
}
}
3989

被折叠的 条评论
为什么被折叠?



