zoukankan      html  css  js  c++  java
  • Valid Sudoku

    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.

    Note:
    A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

    注意:其中没有填的也代表有效的,用'.'表示。为了是每行每列以及每个9方格中没有重复的元素,使用了set来存放,本来想用map的,但是map每次要存放两个元素,多余。

    当每行或每列填满之后,可以直接判断set的长度是否为9,否则就是有重复的元素。

    C++代码如下:

    #include<iostream>
    #include<set>
    #include<vector>
    using namespace std;
    
    class Solution
    {
    public:
        bool isValidSudoku(vector<vector<char> > &board)
        {
            set<char> st;
            size_t dot=0;
            int i,j;
            for(i=0; i<9; i++)
            {
                st.clear();
                dot=0;
                for(j=0; j<9; j++)
                {
                    if(board[i][j]=='.')
                    {
                        dot++;
                        continue;
                    }
                    st.insert(board[i][j]);
                }
                if(st.size()<9-dot)
                    return false;
            }
            for(i=0; i<9; i++)
            {
                st.clear();
                dot=0;
                for(j=0; j<9; j++)
                {
                    if(board[j][i]=='.')
                    {
                        dot++;
                        continue;
                    }
                    st.insert(board[j][i]);
                }
                if(st.size()<9-dot)
                    return false;
            }
            int block=0;
            for(block=0; block<9; block++)
            {
                dot=0;
                st.clear();
                for(i=block/3*3; i<block/3*3+3; i++)
                {
                    for(j=block%3*3; j<block%3*3+3; j++)
                    {
                        if(board[i][j]=='.')
                        {
                            dot++;
                            continue;
                        }
                        st.insert(board[i][j]);
                    }
                }
                if(st.size()<9-dot)
                    return false;
            }
            return true;
        }
    };
    
    int main()
    {
        Solution s;
        /*vector<vector<char> > vec= {{'8','1','4','6','5','2','7','3','9'},
            {'9','5','7','1','8','3','6','2','4'},
            {'2','3','6','9','7','4','1','5','8'},
            {'6','9','5','4','3','7','2','8','1'},
            {'4','8','1','5','2','6','3','9','7'},
            {'3','7','2','8','9','1','5','4','6'},
            {'5','6','9','3','1','8','4','7','2'},
            {'7','4','8','2','6','5','9','1','3'},
            {'1','2','3','7','4','9','8','6','5'}
        };*/
        vector<vector<char> >vec= {{'.','.','5','.','.','.','.','.','6'},
            {'.','.','.','.','1','4','.','.','.'},
            {'2','.','.','.','.','.','.','.','.'},
            {'1','.','.','.','.','.','.','.','.'},
            {'5','.','.','.','.','.','.','.','.'},
            {'6','.','.','.','.','.','.','.','.'},
            {'7','.','.','.','.','.','.','.','.'},
            {'8','.','.','.','.','.','.','.','.'},
            {'9','.','.','.','.','.','.','.','.'},
        };
        cout<<s.isValidSudoku(vec)<<endl;
    }
  • 相关阅读:
    python3 urllib 类
    python 金角大王博客园学习地址
    配置文件一mapper.xml
    配置文件一mybatis-config.xml
    配置文件一applicationContext.xml
    类文件路径一classpath
    配置文件一web.xml
    日志框架一logback配置和使用
    SpringBoot + redis实现分布式session共享
    SpringBoot集成Redis 一 分布式锁 与 缓存
  • 原文地址:https://www.cnblogs.com/wuchanming/p/4106736.html
Copyright © 2011-2022 走看看