[力扣c++实现] 36.有效的数独

[力扣c++实现] 36.有效的数独,第1张

36.有效的数独

请你判断一个 9x9 的数独是否有效。


只需要 根据以下规则 ,验证已经填入的数字是否有效即可。


数字 1-9 在每一行只能出现一次。



数字 1-9 在每一列只能出现一次。



数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。


(请参考示例图)
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。


注意:

一个有效的数独(部分已被填充)不一定是可解的。



只需要根据以上规则,验证已经填入的数字是否有效即可。


示例 1:

输入: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”]]
输出:true
示例 2:

输入: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”]]
输出:false
解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。


但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。


提示:

board.length == 9
board[i].length == 9
board[i][j] 是一位数字或者 ‘.’

1.c++
/*
* 2021-09-08 22:41
*/
#include 
#include 
#include 

using namespace std;

class Solution {
  vector<int> pos_1 = {0,0};
  vector<vector<int>> mod_1 = {{1,1},{2,2},{1,2},{2,1}};

  vector<int> pos_2 = {0,1};
  vector<vector<int>> mod_2 = {{1,-1},{1,1},{2,-1},{2,1}};

  vector<int> pos_3 = {0,2};
  vector<vector<int>> mod_3 = {{1,-2},{1,-1},{2,-2},{2,-1}};

  vector<int> pos_4 = {1,0};
  vector<vector<int>> mod_4 = {{-1,1},{1,1},{-1,2},{1,2}};

  vector<int> pos_5 = {1,1};
  vector<vector<int>> mod_5 = {{-1,-1},{1,1},{-1,1},{1,-1}};

  vector<int> pos_6 = {1,2};
  vector<vector<int>> mod_6 = {{-1,-2},{-1,-1},{1,-2},{1,-1}};

  vector<int> pos_7 = {2,0};
  vector<vector<int>> mod_7 = {{-2,1},{-1,1},{-2,2},{-1,2}};

  vector<int> pos_8 = {2,1};
  vector<vector<int>> mod_8 = {{-2,-1},{-1,-1},{-1,1},{-2,1}};

  vector<int> pos_9 = {2,2};
  vector<vector<int>> mod_9 = {{-2,-2},{-2,-1},{-1,-2},{-1,-1}};

  map<vector<int>,vector<vector<int>>> pos_map = {
    {pos_1,mod_1},
    {pos_2,mod_2},
    {pos_3,mod_3},
    {pos_4,mod_4},
    {pos_5,mod_5},
    {pos_6,mod_6},
    {pos_7,mod_7},
    {pos_8,mod_8},
    {pos_9,mod_9}
  };
    
public:
    bool isNineRight(const int x,const int y,const vector<vector<char>>& board)
    {
       int tempx = x%3;
       int tempy = y%3;
        
       cout<<"x: "<<x<<" y: "<<y<<endl;
       cout<<"tempx: "<<tempx<<" tempy: "<<tempy<<endl;

       vector<int> tmp_pos = {tempx,tempy};
       auto tmp_vvec = pos_map[tmp_pos];
       
       for (int i = 0; i < tmp_vvec.size(); ++i)
       {
          if (board[x][y] == board[x+tmp_vvec[i][0]][y+tmp_vvec[i][1]])
          {
             return false;
          }
       }
        
       return true;
    }

    bool isValid(const int x,const int y,const vector<vector<char>>& board)
    {
        //1.行冲突判断
        for (int i =0; i < 9;++i)
        {
            if (i == x)
            {
                continue;
            }

            if (board[i][y] == board[x][y])
            {
                return false;
            }
        }

        //2.列冲突判断
        for (int j =0; j < 9;++j)
        {
            if (j == y)
            {
                continue;
            }

            if (board[x][j] == board[x][y])
            {
                return false;
            }
        }

        //3.九宫格判断
        return isNineRight(x,y,board);
    }
public:
    bool isValidSudoku(vector<vector<char>>& board) {

        for (int i = 0 ; i < 9;++i)
        {
            for (int j = 0 ; j < 9;++j)
            {
                if (board[i][j] == '.')
                {
                    continue;
                }

                if (isValid(i,j,board) == false)
                {
                    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'}
    };

    Solution s_obj;
    cout<<s_obj.isValidSudoku(board)<<endl;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/564106.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-03
下一篇 2022-04-03

发表评论

登录后才能评论

评论列表(0条)

保存