-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution37.cs
80 lines (68 loc) · 2.06 KB
/
Solution37.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
namespace LeetCode.Solutions;
public class Solution37
{
/// <summary>
/// 37. Sudoku Solver - Hard
/// <a href="https://leetcode.com/problems/sudoku-solver">See the problem</a>
/// </summary>
public void SolveSudoku(char[][] board)
{
Solve(board);
}
private bool Solve(char[][] board)
{
for (var row = 0; row < board.Length; row++)
{
for (var col = 0; col < board[0].Length; col++)
{
// Skip filled cells
if (board[row][col] != '.')
{
continue;
}
for (var num = '1'; num <= '9'; num++)
{
if (!IsValid(board, row, col, num))
{
continue;
}
board[row][col] = num;
if (Solve(board))
{
return true; // If it's the solution, return true
}
board[row][col] = '.'; // Otherwise, backtrack
}
// If no valid number can be placed in this cell, return false
return false;
}
}
// If all cells are filled correctly, we've found a solution
return true;
}
private bool IsValid(char[][] board, int row, int col, char num)
{
// Check the row and column
for (var i = 0; i < 9; i++)
{
if (board[row][i] == num || board[i][col] == num)
{
return false;
}
}
// Check the 3x3 sub-grid
var startRow = row / 3 * 3;
var startCol = col / 3 * 3;
for (var i = startRow; i < startRow + 3; i++)
{
for (var j = startCol; j < startCol + 3; j++)
{
if (board[i][j] == num)
{
return false;
}
}
}
return true; // The number can be placed in this cell
}
}