-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
66 lines (65 loc) · 2.67 KB
/
Program.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
//遍历行,列个九宫格,按照要求检查是否有重复的数字出现,查出重复数字返回false,否则遍历结束后返回true。
using System;
namespace ValidSudoku
{
class Program
{
static void Main(string[] args)
{
char[][] board = new char[9][];
board[0] = new char[9] { '8', '3', '.', '.', '7', '.', '.', '.', '.' };
board[1] = new char[9] { '6', '.', '.', '1', '9', '5', '.', '.', '.' };
board[2] = new char[9] { '.', '9', '8', '.', '.', '.', '.', '6', '.' };
board[3] = new char[9] { '8', '.', '.', '.', '6', '.', '.', '.', '3' };
board[4] = new char[9] { '4', '.', '.', '8', '.', '3', '.', '.', '1' };
board[5] = new char[9] { '7', '.', '.', '.', '2', '.', '.', '.', '6' };
board[6] = new char[9] { '.', '6', '.', '.', '.', '.', '2', '8', '.' };
board[7] = new char[9] { '.', '.', '.', '4', '1', '9', '.', '.', '.' };
board[8] = new char[9] { '.', '.', '.', '.', '.', '8', '.', '7', '9' };
Console.WriteLine(IsValidSudoku(board));
}
static bool IsValidSudoku(char[][] board)
{
for (int i = 0; i < 9; i++)
{
int[] recordRow = new int[10];
int[] recordCol = new int[10];
for (int j = 0; j < 9; j++)
{
if (board[i][j] != '.')
{
recordRow[board[i][j] - '0']++;
if (recordRow[board[i][j] - '0'] > 1)
return false;
}
if (board[j][i] != '.')
{
recordCol[board[j][i] - '0']++;
if (recordCol[board[j][i] - '0'] > 1)
return false;
}
}
}
for (int i = 0; i < 9; i+=3)
{
for (int j = 0; j < 9; j+=3)
{
int[] record = new int[10];
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (board[x + i][y + j] != '.')
{
record[board[x + i][y + j] - '0']++;
if (record[board[x + i][y + j] - '0'] > 1)
return false;
}
}
}
}
}
return true;
}
}
}