美文网首页
数独sukudo

数独sukudo

作者: sunblog | 来源:发表于2018-03-14 01:17 被阅读0次

问题的类别是回溯。

注意边界、剪枝的问题。

具体的问题描述看leetcode这里

需要特别注意的是,leetcode这里的输入是按照第一行、第二行、第三行来进行的,而不是,第一个九宫格,第二个九宫格。

// .h
//
// Created on 3/14/18.
//

#ifndef AGORITHM_SUDOKU2_H
#define AGORITHM_SUDOKU2_H

#include <vector>

class Sudoku2 {
public:
    using VIS_RECORD=std::vector<std::vector<int>>;

    static void test();

    static void solveSudoku(std::vector<std::vector<char>> &board);

    static void visit(VIS_RECORD &row, VIS_RECORD &col, VIS_RECORD &mat, int r, int c, int num,
                      std::vector<std::vector<char>> &board, bool &found);

private:
    static void initVis(VIS_RECORD &row, VIS_RECORD &col, VIS_RECORD &mat, const std::vector<std::vector<char>> &board);

    static int getOrderForMat(int r, int c);

    static int getPosOfCell(int r, int c);

    template<typename T>
    static void printBoard(const std::vector<std::vector<T>> &board);
};


#endif //AGORITHM_SUDOKU2_H

// .cpp
//
// Created on 3/14/18.
//

#include <iostream>
#include "Sudoku2.h"

using namespace std;

void Sudoku2::solveSudoku(std::vector<std::vector<char>> &board) {
    const int N = board.size();

    vector<vector<int>> visRow(N, vector<int>(N, 0));   // visRow[i][j] == 1 if row i has number j
    auto visCol = visRow;                               // visCol[i][j] == 1 if col i has number j
    auto visMat = visRow;                               // visMat[i][j] == 1 if mat i has number j

    initVis(visRow, visCol, visMat, board);

    bool found = false;
    for (int i = 0; i < N; i++) {
        visit(visRow, visCol, visMat, 0, 0, i, board, found);
    }
}

void Sudoku2::visit(Sudoku2::VIS_RECORD &row, Sudoku2::VIS_RECORD &col, Sudoku2::VIS_RECORD &mat, const int r,
                    const int c, int num, std::vector<std::vector<char>> &board, bool &found) {
    if (!found) {
        int o = getOrderForMat(r, c);
        if ((!row[r][num] && !col[c][num] && !mat[o][num] && (board[r][c] == '.')) || (board[r][c] - '1' == num)) {
            const int N = board.size();

            bool diff = (board[r][c] - '1' != num);
            if (diff) {
                row[r][num] = col[c][num] = mat[o][num] = 1;
                board[r][c] = num + '1';
            }


            if (r == N - 1 && c == N - 1) {
                found = true;
                return;
            }

            if (c < N - 1) {
                for (int i = 0; (i < N) && !found; i++) {   // visit next cell of same unit with possible values
                    if (i != num) {
                        visit(row, col, mat, r, c + 1, i, board, found);
                    }
                }
            }

            if (c == N - 1) {   // the last cell of k'th unit
                for (int i = 0; (i < N) && !found; i++) {   // visit first cell of next unit with possible values
                    visit(row, col, mat, r + 1, 0, i, board, found);
                }
            }

            if (diff && !found) {
                row[r][num] = col[c][num] = mat[o][num] = 0;
                board[r][c] = '.';
            }
        }
    }
}

int Sudoku2::getOrderForMat(int r, int c) {
    return r / 3 * 3 + c / 3;
}

int Sudoku2::getPosOfCell(int r, int c) {
    return c % 3 + r % 3 * 3;
}

void Sudoku2::initVis(Sudoku2::VIS_RECORD &row, Sudoku2::VIS_RECORD &col, Sudoku2::VIS_RECORD &mat,
                      const std::vector<std::vector<char>> &board) {
    const int N = board.size();
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (board[i][j] != '.') {
                int x = board[i][j] - '1';

                int o = getOrderForMat(i, j);
                mat[o][x] = 1;

                row[i][x] = 1;
                col[j][x] = 1;
            }
        }
    }
}

template<typename T>
void Sudoku2::printBoard(const std::vector<std::vector<T>> &board) {
    for (auto &v: board) {
        for (auto ch: v) {
            cout << ch << " ";
        }
        cout << endl;
    }
    cout << endl;
}

void Sudoku2::test() {
    vector<vector<char>> board1 = {
            {'.', '.', '9', '7', '4', '8', '.', '.', '.'},
            {'7', '.', '.', '.', '.', '.', '.', '.', '.'},
            {'.', '2', '.', '1', '.', '9', '.', '.', '.'},
            {'.', '.', '7', '.', '.', '.', '2', '4', '.'},
            {'.', '6', '4', '.', '1', '.', '5', '9', '.'},
            {'.', '9', '8', '.', '.', '.', '3', '.', '.'},
            {'.', '.', '.', '8', '.', '3', '.', '2', '.'},
            {'.', '.', '.', '.', '.', '.', '.', '.', '6'},
            {'.', '.', '.', '2', '7', '5', '9', '.', '.'}
    };

    auto fn = [](vector<vector<char>> &board) {
        solveSudoku(board);
        printBoard(board);
    };
    fn(board1);
}

相关文章

  • 数独sukudo

    问题的类别是回溯。 注意边界、剪枝的问题。 具体的问题描述看leetcode这里 需要特别注意的是,leetcod...

  • 数独数独

    想找人一起玩数独,你在哪?

  • 数独数独我爱你

    好久没玩数独了,昨天在图书馆借书时突发奇想查了有关数独的书,有好多!借了一本非常数独,回家就迫不及待地翻阅,哇塞,...

  • 数独

    一款拥有几十年历史的数字益智游戏,5种宫格,3种难度,适合各种水平,最接地气的数独游戏。 游戏特性: -- 每种难...

  • 数独

    现在,我每周天几乎都上的数独,直到上周天我全部学完了数独的六堂课。 在课上,我们学习了四宫格与六宫...

  • 数独

    学习目标 1、经历填数游戏活动,初步提高分析推理能力。 2、在探索、尝试、交流等活动中,体会填数游...

  • 数独

    这是一款集合了数独、方块、圈圈消消乐以及疯狂球球的合集游戏,画面精美,操作简单,经典数独与球球考验思维、方块与圈圈...

  • 数独

    全新数独游戏,无限个数独关卡,简单操作下的无穷乐趣! 玩好数独游戏首先要了解它的特点、要求,然后要运用一些原则和技...

  • 数独

    最近我参加了一个数独班,虽然我之前有学过一点点的基础,但是应该用什么规律来解,我并不是很清楚。而刚好通过...

  • 数独

    数独游戏是对智慧和毅力的考验。在这看似简单的小小一方九宫格上,用自己所有的想象力、逻辑推理和创新思维,去感悟游走在...

网友评论

      本文标题:数独sukudo

      本文链接:https://www.haomeiwen.com/subject/hadxqftx.html