数独sukudo

问题的类别是回溯。

注意边界、剪枝的问题。

具体的问题描述看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);
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • LeetCode 刷题随手记 - 第一部分 前 256 题(非会员),仅算法题,的吐槽 https://leetc...
    蕾娜漢默阅读 17,985评论 2 36
  • 一、正则表达式的用途(搜索和替换) 1.1.正则表达式(regular expression,简称regex)是一...
    IIronMan阅读 10,161评论 0 14
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • 用到的组件 1、通过CocoaPods安装 2、第三方类库安装 3、第三方服务 友盟社会化分享组件 友盟用户反馈 ...
    SunnyLeong阅读 14,738评论 1 180