1. 程式人生 > 其它 >LeetCode-中等-36-有效的數獨(C)

LeetCode-中等-36-有效的數獨(C)

技術標籤:Cleetcodec語言

文章首發及後續更新:https://mwhls.top/1604.html
新的更新內容請到mwhls.top檢視。
無圖/無目錄/格式錯誤/更多相關請到上方的文章首發頁面檢視。

題目

判斷一個9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。

數字1-9在每一行只能出現一次。
數字1-9在每一列只能出現一次。
數字1-9在每一個以粗實線分隔的3x3宮內只能出現一次。

上圖是一個部分填充的有效的數獨。

數獨部分空格內已填入了數字,空白格用'.'表示。

示例1:

輸入:
[
["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"]
]
輸出: true
示例2:

輸入:
[
["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"]
]
輸出: false
解釋: 除了第一行的第一個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:

一個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字1-9和字元'.'。
給定數獨永遠是9x9形式的。

來源:力扣(LeetCode)
連結:https://leetcode-cn.com/problems/valid-sudoku
著作權歸領釦網路所有。商業轉載請聯絡官方授權,非商業轉載請註明出處。

思路

  • 這次這個好做啊,只要按一般的思路去做,bug都沒有多少,好舒服。
  • 只需要按行遍歷一下,按列遍歷一下,按塊遍歷一下,對比一下每次是否有出現重複的數字就好了。
  • 我這裡提供的比較方法,是用一個compareList[9]的全1陣列來作為基準,每次比較中,出現一個數字,就將對應元素減1。
    • 如果這個數字出現了兩次甚至更多,那這個元素就會變成負數,就說明這個數獨陣列是錯誤的。
  • 至於行、列、塊的比較,就很簡單了,只需要玩過,或者僅懂這個規則就知道了,就是非常普通的按順序對比即可。
  • 此外,這題的C語言版本還傳參了兩個用不上的boardSize與boardColSize。
    • 按慣例應該是為了彌補C語言不能測出**陣列大小的不足,但數獨好像都是9*9的,
    • 像16*16這種雖然可以做成數獨,不過想象上去就有點醜,沒見過,這題也沒出這種,所以這兩個引數用不上。

程式碼

bool isValidRow(char **board, int row);
bool isValidCol(char **board, int col);
bool isValidBlock(char **board, int block);

bool isValidSudoku(char** board, int boardSize, int* boardColSize){
    int comparePos=0;
    for(comparePos=0;   comparePos<9;   comparePos++){
        if(!isValidRow(board, comparePos)) return false;
    }
    for(comparePos=0;   comparePos<9;   comparePos++){
        if(!isValidCol(board, comparePos)) return false;
    }
    for(comparePos=0;   comparePos<9;   comparePos++){
        if(!isValidBlock(board, comparePos)) return false;
    }
    return true;

}

bool isValidRow(char **board, int row){
    int col, num;
    int compareList[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
    for(col=0;  col<9;  col++){
        num = board[row][col]-'1';
        if(num == '.'-'1') continue;
        if(compareList[num] == 1) compareList[num] -= 1;
        else return false;
    }
    return true;
}

bool isValidCol(char **board, int col){
    int row, num;
    int compareList[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
    for(row=0;  row<9;  row++){
        num = board[row][col]-'1';
        if(num == '.'-'1') continue;
        if(compareList[num] == 1) compareList[num] -= 1;
        else return false;
    }
    return true;
}

bool isValidBlock(char **board, int block){
    int compareBlock[9][2] = { {0,0}, {0,3}, {0,6}, {3,0}, {3,3}, {3,6}, {6,0}, {6,3}, {6,6} };
    int compareList[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
    int num, row, col;
    for(row=0;    row<3;   row++){
        for(col=0;  col<3;  col++){
            num = board[ compareBlock[block][0] + row ][ compareBlock[block][1] + col ] - '1';
            if(num == '.'-'1') continue;
            if(compareList[num] == 1) compareList[num] -= 1;
            else return false;
        }
    }
    return true;
}