leetcode 有效的数独

该博客内容涉及一个C++程序,用于检查数独矩阵是否有效。程序通过检查行、列和宫格来验证每个数字1到9是否恰当地出现且不重复。 isValidSudoku 函数调用三个辅助函数 judge_rows、judge_cols 和 judge_palaces,分别检查这些条件。如果所有检查都通过,则数独有效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <string.h>
#include <iostream>
#include <vector>
using namespace std;
bool array[10] = { 0 };
class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        return judge_rows(board) && judge_cols(board) && judge_palaces(board);
    }
    bool judge_rows(const vector<vector<char>>& board) {
        int size = board.size();
        for (int row = 0;row < size;row++) {
            const vector<char>&row_board = board[row];
            memset(array, 0, sizeof(array));
            for (auto &ch : row_board) {
                if ('.' == ch) {
                    continue;
                }
                int index = ch - '0';
                if (!array[index]) {
                    array[index] = true;
                }
                else {
                    return false;
                }
            }
        }
        return true;
    }
    bool judge_cols(const vector<vector<char>>& board) {
        int size = board.size();
        for (int col = 0;col < size;col++) {
            memset(array, 0, sizeof(array));
            for (int i = 0;i < size;i++) {
                char ch = board[i][col];
                if ('.' == ch) {
                    continue;
                }
                int index = ch - '0';
                if (!array[index]) {
                    array[index] = true;
                }
                else {
                    return false;
                }
            }
        }
        return true;
    }
    bool judge_palaces(const vector<vector<char>>& board) {
        int size = board.size();
        for (int row = 0;row < size;row +=3) {
            for (int col = 0;col < size;col +=3) {
                memset(array, 0, sizeof(array));
                for (int i = row;i < row + 3;i++) {
                    for (int j = col;j < col + 3;j++) {
                        char ch = board[i][j];
                        if ('.' == ch) {
                            continue;
                        }
                        int index = ch - '0';
                        if (!array[index]) {
                            array[index] = true;
                        }
                        else {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
};
int main() {
 /*   vector<vector<char>>board = { {'5','3','.','.','7','.','.','.','.'},
                                  {'6','.','.','1','9','5','.','.','.'},
                                  {'.','9','8','.','.','.','.','6','.'},
                                  {'8','.','.','.','6','.','.','.','3'},
                                  {'4','.','.','8','.','3','.','.','1'},
                                  {'7','.','.','.','2','.','.','.','6'},
                                  {'.','6','.','.','.','.','2','8','.'},
                                  {'.','.','.','4','1','9','.','.','5'},
                                  {'.','.','.','.','8','.','.','7','9'}
                                };*/
   vector<vector<char>>board = { {'8','3','.','.','7','.','.','.','.'},
                                 {'6','.','.','1','9','5','.','.','.'},
                                 {'.','9','8','.','.','.','.','6','.'},
                                 {'8','.','.','.','6','.','.','.','3'},
                                 {'4','.','.','8','.','3','.','.','1'},
                                 {'7','.','.','.','2','.','.','.','6'},
                                 {'.','6','.','.','.','.','2','8','.'},
                                 {'.','.','.','4','1','9','.','.','5'},
                                 {'.','.','.','.','8','.','.','7','9'}
                              };

    Solution ss;
    std::cout << ss.isValidSudoku(board) << std::endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值