程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> LeetCode之Valid Sudoku

LeetCode之Valid Sudoku

編輯:C++入門知識

【題目】

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

\

A partially filled sudoku which is valid.<喎?http://www.BkJia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPGg0PqG+zOLS4qG/PC9oND4KPHA+vNnJ6NK7uPbK/bbAysfT0NCntcSjrNTyy/y3+7rPyv22wNPOz7cguebU8qOoteO797Tyv6rBtL3To6mhozxicj4KPC9wPgo8cD7K/bbA087PtyC55tTyo7o8YnI+CjwvcD4KPHA+o6gxo6nDv9K70NC2vLHY0OvU2jEtObXEt7bOp8Tao6zH0ta7s/bP1tK7tM6hozwvcD4KPHA+PGltZyBzcmM9"http://www.BkJia.com/uploadfile/Collfiles/20140120/20140120134659466.jpg" alt="\">

(2)每一列都必須在1-9的范圍內,且只出現一次。

\

(3)數字1-9在每個子宮格中只出現一次。

\

數獨宮格可以部分填充,其中空單元格都充滿了字符".'。

例如:

\

部分填充的有效數獨

【分析】

細節分析題

(1)檢查行

(2)檢查列

(3)檢查9個子宮格

【代碼】

/*********************************
*   日期:2014-01-20
*   作者:SJF0115
*   題號: Valid Sudoku
*   來源:http://oj.leetcode.com/problems/valid-sudoku/
*   結果:AC
*   來源:LeetCode
*   總結:
**********************************/
#include 
#include 
#include 
#include 
#include 
using namespace std;

class Solution {
public:
    bool isValidSudoku(vector > &board) {
        int i,j,k;
        //判斷1-9是否已用
        bool used[9];
        for(i = 0;i < 9;i++){
            //檢查行
            memset(used,false,9);
            for(j = 0;j < 9;j++){
                //不符合規則
                if(!check(board[i][j],used)){
                    return false;
                }//if
            }//for
            //檢查列
            memset(used,false,9);
            for(j = 0;j < 9;j++){
                //不符合規則
                if(!check(board[j][i],used)){
                    return false;
                }//if
            }//for
        }//for
        //檢查9個子宮格
        for(k = 0;k < 3;k++){
            memset(used,false,9);
            for(i = k*3;i < 3*k + 3;i++){
                for(j = 0;j < 3;j++){
                    //不符合規則
                    if(!check(board[i][j],used)){
                        return false;
                    }//if
                }//for
            }//for
            memset(used,false,9);
            for(i = k*3;i < 3*k + 3;i++){
                for(j = 3;j < 6;j++){
                    //不符合規則
                    if(!check(board[i][j],used)){
                        return false;
                    }//if
                }//for
            }//for
            memset(used,false,9);
            for(i = k*3;i < 3*k + 3;i++){
                for(j = 6;j < 9;j++){
                    //不符合規則
                    if(!check(board[i][j],used)){
                        return false;
                    }//if
                }//for
            }//for
        }//for
        return true;
    }
private:
    bool check(char c,bool used[9]){
        if(c == '.'){
            return true;
        }
        //字符c 已用
        if(used[c - '1']){
            return false;
        }
        //字符c 未用
        else{
            used[c - '1'] = true;
            return true;
        }
    }
};
int main() {
    Solution solution;
    bool result;
    vector> board;

    char value[9] = {'5','3','.','.','7','.','.','.','.'};
    vector subBoard(value,value + 9);
    board.push_back(subBoard);

    value = {'6','.','.','1','9','5','.','.','.'};
    vector subBoard1(value,value + 9);
    board.push_back(subBoard1);

    value = {'.','9','8','.','.','.','.','6','.'};
    vector subBoard2(value,value + 9);
    board.push_back(subBoard2);

    value = {'8','.','.','.','6','.','.','.','3'};
    vector subBoard3(value,value + 9);
    board.push_back(subBoard3);

    value = {'4','.','.','8','.','3','.','.','1'};
    vector subBoard4(value,value + 9);
    board.push_back(subBoard4);

    value = {'7','.','.','.','2','.','.','.','6'};
    vector subBoard5(value,value + 9);
    board.push_back(subBoard5);

    value = {'.','6','.','.','.','.','2','8','.'};
    vector subBoard6(value,value + 9);
    board.push_back(subBoard6);

    value = {'.','.','.','4','1','9','.','.','5'};
    vector subBoard7(value,value + 9);
    board.push_back(subBoard7);

    value = {'.','.','.','.','8','.','.','7','9'};
    vector subBoard8(value,value + 9);
    board.push_back(subBoard8);

    result = solution.isValidSudoku(board);
    cout<

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved