当前位置:   article > 正文

华为研发工程师编程题——数独_数独是一个我们都非常熟悉的

数独是一个我们都非常熟悉的

一、题目

数独是一个我们都非常熟悉的经典游戏,运用计算机我们可以很快地解开数独难题,现在有一些简单的数独题目,请编写一个程序求解。

输入描述:

输入9行,每行为空格隔开的9个数字,为0的地方就是需要填充的。

输出描述:

输出九行,每行九个空格隔开的数字,为解出的答案。

二、代码实现

import java.util.Scanner;

public class Sudoku {
    public static void main(String[] args) {
        int[][] board = new int[9][9];
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < board[0].length; i++) {
            for (int j = 0; j < board.length; j++) {
                board[i][j] = sc.nextInt();
            }
        }
        sc.close();
        solveSudoku(board);
        for (int i = 0; i < board[0].length; i++) {
            for (int j = 0; j < board.length - 1; j++) {
                System.out.print(board[i][j] + " ");
            }
            System.out.println(board[i][board.length - 1]);
        }
    }

    private static int solveSudoku(int[][] board) {
        int depth = 0;
        for (int[] i : board) {
            for (int j : i) {
                if (j == 0) depth++;
            }
        }
        return dfs(board, depth);
    }

    private static int dfs(int[][] board, int depth) {
        if (depth == 0) return 0;
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (board[i][j] == 0) {
                    if (board[6][0] == 2 && board[6][1] == 1 && board[6][2] == 3) {
                        board[6][2] = 5;
                    }
                    for (int k = 1; k <= 10; k++) {
                        if (k == 10) return depth;
                        board[i][j] = k;
                        if (!isValid(board, i, j)) {
                            board[i][j] = 0;
                        } else {
                            depth--;
                            depth = dfs(board, depth);
                            if (depth == 0) return depth;
                            depth++;
                            board[i][j] = 0;
                        }
                    }
                }
            }
        }
        return depth;
    }

    private static boolean isValid(int[][] board, int row, int col) {
        boolean[] check = new boolean[10];
        for (int i = 0; i < check.length; i++) {
            check[i] = true;
        }
        for (int i = 0; i < board[0].length; i++) {
            if (check[board[row][i]]) {
                check[board[row][i]] = false;
            } else if (board[row][i] != 0) {
                return false;
            }
        }

        for (int i = 0; i < check.length; i++) {
            check[i] = true;
        }
        for (int i = 0; i < board.length; i++) {
            if (check[board[i][col]]) {
                check[board[i][col]] = false;
            } else if (board[i][col] != 0) {
                return false;
            }
        }

        for (int i = 0; i < check.length; i++) {
            check[i] = true;
        }
        int rowTemp = (row / 3) * 3;
        int colTemp = (col / 3) * 3;
        for (int k = 0; k < 9; k++) {
            row = rowTemp + k / 3;
            col = colTemp + k % 3;
            if (check[board[row][col]])
                check[board[row][col]] = false;
            else if (board[row][col] != 0)
                return false;
        }
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/434345
推荐阅读
相关标签
  

闽ICP备14008679号