Leetcode notes: Valid Sudoku

Source: Internet
Author: User

I. Title Description

Determine if a Sudoku is valid, according To:sudoku puzzles-the Rules:
Http://sudoku.com.au/TheRules.aspx.
The Sudoku board could be partially filled, where empty cells is filled with the character '.

The following figure:a partially filled Sudoku which is valid.

Two. Topic analysis

The nature of the Sudoku matrix can be summarized simply as: whether there is a unique permutation combination for each row in the matrix, each column, and each 3×3 of the nine Gongge regions 0~9 , and if the same element exists, the Sudoku matrix is illegal.

This problem is relatively simple, is to traverse all the elements of the Sudoku matrix, check whether the elements meet the nature of Sudoku. For judging whether an element is in a row, a column, or a small area, I define a keyword to hold the number of occurrences of a 0~9 number map , used as a keyword as long as the ‘1‘,‘2‘,...,‘9‘ stored object of a keyword is greater than 1, indicating that there is a duplicate in the row, column, or 3×3 nine Gongge area , traversing in this way can determine whether the matrix is legitimate.

Three. Sample code

#include <iostream>#include <vector>#include <map>using namespace STD;classsolution{ Public:BOOLIsvalidsudoku ( vector<vector<char> >& Board) { Map<char, int>Sudokumap; for(inti =0; I <9; i++) {sudokumap.clear (); for(intj =0; J <9; J + +) {sudokumap[board[i][j]]++;if((board[i][j]! ='. ') && (Sudokumap[board[i][j]] >1))return false; }        } for(inti =0; I <9; i++) {sudokumap.clear (); for(intj =0; J <9; J + +) {sudokumap[board[j][i]]++;if((board[j][i]! ='. ') && (Sudokumap[board[j][i]] >1))return false; }        } for(inti =0; I <9; i + =3)        { for(intj =0; J <9; J + =3) {sudokumap.clear (); for(intK = i; K < i +3; k++) { for(intL = j; L < J +3; l++) {sudokumap[board[k][l]]++;if((board[k][l]! ='. ') && (Sudokumap[board[k][l]] >1))return false; }                }            }        }return true; }};

The results of the operation are as follows:

1. A correct Sudoku matrix:

2. A wrong Sudoku matrix:

Four. Summary

This problem does not require the implementation of any algorithm, just design a matrix operation, use map for each row, each column or each nine in each 1~9 number of times, as long as the discovery of more than one, you can immediately negate the Sudoku, only to traverse the entire Sudoku matrix and meet the requirements, Only to be judged as valid Sudoku.

Related Topics: Sudoku solver

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Leetcode notes: Valid Sudoku

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.