leetcode problem 37 -- Sudoku Solver

解决数独

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character ‘.‘.

You may assume that there will be only one unique solution.

A sudoku puzzle...

思路:

  搜索加剪枝。

  首先设立3个辅助二维数组:rows,  columns, grids来维护目前的数独状态,rows[i][k](1<=i, k<=9)表示第i行是否占用了数字k,同理colums[i][k]表示第i列是否占用了数字k,grid[i][k]表示第i个格子是否占用了数字k,

  然后第一次读入二维数组,更新rows, columns和grids.

  第二次读二维数组,对每个为‘.‘的元素根据rows,columns和grid来枚举可能的值。如果没有可以枚举的值,直接剪枝返回。

代码如下:

  Runtime: 5 ms

static int columns[9][10];
static int rows[9][10];
static int grids[9][10];

int search(char *board[9], int startI, int startJ) {

    for(int i = startI; i < 9; ++i) {
        for(int j = i == startI ? startJ : 0; j < 9; ++j) {
            if (board[i][j] == ‘.‘) {
                for (int k = 1; k <= 9; ++k) {
                    if (!rows[i][k] && !columns[j][k] && !grids[i/3 + j/3 *3][k]) {
                        rows[i][k] = 1;
                        columns[j][k] = 1;
                           grids[i/3 + j/3 *3][k] = 1;

                        if (search(board, i, j+1) == 1) {
                            board[i][j] = k + ‘0‘;
                            return 1;
                        }
                        rows[i][k] = 0;
                        columns[j][k] = 0;
                           grids[i/3 + j/3 *3][k] = 0;
                    }
                }
                return 0;
            }
        }
    }
    return 1;
}

void solveSudoku(char * board[9]) {
    memset(columns, 0, sizeof(columns));
    memset(rows, 0, sizeof(rows));
    memset(grids, 0, sizeof(grids));    

    for(int i = 0; i < 9; ++i) {
        for(int j = 0; j < 9; ++j) {
            if (board[i][j] != ‘.‘) {
                rows[i][board[i][j] - ‘0‘] = 1;
                columns[j][board[i][j] - ‘0‘] = 1;

                int tmp = i/3 + j/3 * 3;
                grids[tmp][board[i][j] - ‘0‘] = 1;
            }
        }
    }
    search(board, 0, 0);
}
时间: 2024-08-05 19:21:21

leetcode problem 37 -- Sudoku Solver的相关文章

[Leetcode][Python]37: Sudoku Solver

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 37: Sudoku Solverhttps://oj.leetcode.com/problems/sudoku-solver/ Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assu

37. Sudoku Solver(js)

37. Sudoku Solver Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly on

[LeetCode] 37. Sudoku Solver 求解数独

Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy all of the following rules: Each of the digits 1-9 must occur exactly once in each row. Each of the digits 1-9 must occur exactly once in each column.

[leetcode]算法题目 - Sudoku Solver

最近,新加坡总理李显龙也写了一份代码公布出来,大致瞧了一眼,竟然是解数独题的代码!前几天刚刚写过,数独主要算法当然是使用回溯法.回溯法当时初学的时候在思路上比较拧,不容易写对.写了几个回溯法的算法之后心里总算有了点底.回溯法的代码一般都是长成下面这样子: void backtracking(int[] arr, int boundary, int current, int[] result) { if(current>=boundary) //到达终止条件 { //判断result是否符合要求

LeetCode 37 Sudoku Solver(求解数独)

题目链接: https://leetcode.com/problems/sudoku-solver/?tab=Description Problem : 解决数独问题,给出一个二维数组,将这个数独进行求解. 思路: 嵌套循环,三层循环体,每一行,每一列,填入从1到9的数字.判断填入之后是否合理 判断数独是否合理的函数 参考代码: package leetcode_50; /*** * * @author pengfei_zheng * 求解数独问题 */ public class Solutio

LeetCode 37 Sudoku Solver (C,C++,Java,Python)

Problem: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. So

Java [leetcode 37]Sudoku Solver

题目描述: Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by the character '.'. You may assume that there will be only one unique solution. A sudoku puzzle... ...and its solution numbers marked in red. 解题思路:

[leetcode 37]sudoku solver

1 题目: 根据给出的数独,全部填出来 2 思路: 为了做出来,我自己人工做了一遍题目给的数独.思路是看要填的数字横.竖.子是否已经有1-9的数字,有就剔除一个,最后剩下一个的话,就填上.一遍一遍的循环,直到填完为止. 后来发现,这个思路只能解决部分数独.还有部分数独是需要回溯的,比如,这个位置只能填3或5,那么就需要先填上3,看看能否继续填下去,不能的话,再回过来填5. 想了半天,想不出来,把别人的backtracking看懂了,写出来了.. 3 代码: 自己的: Hashtable<Inte

leetcode 37 Sudoku Solver java

求数独,只要求做出一个答案就可以. 刚开始对题意理解错误,以为答案是唯一的, 所以做了很久并没有做出来,发现答案不唯一之后,使用回溯.(还是借鉴了一下别人) public class Solution { public void solveSudoku(char[][] board) { HashSet[] hashset = new HashSet[27]; for (int i = 0; i < 27; i++) hashset[i] = new HashSet<Character>