-
Notifications
You must be signed in to change notification settings - Fork 30
/
ValidSudoku.cs
executable file
·103 lines (95 loc) · 6.52 KB
/
ValidSudoku.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Source : https://leetcode.com/problems/valid-sudoku/
// Author : codeyu
// Date : Monday, October 17, 2016 7:38:09 PM
/**********************************************************************************
*
* 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.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
public class Solution036
{
public static bool IsValidSudoku(char[,] board)
{
if(board.Length != 81 && board.GetLength(0) != 9)
{
return false;
}
// check Row
for(int i=0; i<9; i++) {
ISet<int> existed = new HashSet<int>();
for(int j=0; j<9; j++) {
if(board[i,j] == '.') continue;
int k = board[i,j]-'0';
if(k==0 || !existed.Add(k)) return false;
}
}
//check Column
for(int j=0; j<9; j++) {
ISet<int> existed = new HashSet<int>();
for(int i=0; i<9; i++) {
if(board[i,j] == '.') continue;
int k = board[i,j]-'0';
if(k==0 || !existed.Add(k)) return false;
}
}
// check Box
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
int row = 3*i;
int col = 3*j;
ISet<int> existed = new HashSet<int>();
for(int m=row; m<row+3; m++) {
for(int n=col; n<col+3; n++) {
if(board[m,n] == '.') continue;
int k = board[m,n]-'0';
if(k==0 || !existed.Add(k)) return false;
}
}
}
}
return true;
}
public static bool IsValidSudoku2(char[,] board)
{
for(int i = 0; i < 9; i++)
{
int[] rowValid =new int[10];
int[] columnValid =new int[10];
int[] boxValid =new int[10];
for(int j = 0; j < 9; j++)
{
var m = 3*(i/3) + j/3;
var n = 3*(i%3) + j%3;
if(!checkValid(rowValid, board[i,j]-'0') ||
!checkValid(columnValid, board[j,i]-'0') ||
!checkValid(boxValid, board[m,n]-'0'))
return false;
}
}
return true;
}
static bool checkValid(int[] existed, int val)
{
if(val < 0) return true;
if(existed[val] == 1)return false;
existed[val] = 1;
return true;
}
}
}