Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3bd387d

Browse files
committed
Update0037.解数独,添加C#
1 parent 69103f6 commit 3bd387d

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

‎problems/0037.解数独.md‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,59 @@ object Solution {
756756
}
757757
}
758758
```
759+
### C#
760+
```csharp
761+
public class Solution
762+
{
763+
public void SolveSudoku(char[][] board)
764+
{
765+
BackTracking(board);
766+
}
767+
public bool BackTracking(char[][] board)
768+
{
769+
for (int i = 0; i < board.Length; i++)
770+
{
771+
for (int j = 0; j < board[0].Length; j++)
772+
{
773+
if (board[i][j] != '.') continue;
774+
for (char k = '1'; k <= '9'; k++)
775+
{
776+
if (IsValid(board, i, j, k))
777+
{
778+
board[i][j] = k;
779+
if (BackTracking(board)) return true;
780+
board[i][j] = '.';
781+
}
782+
}
783+
return false;
784+
}
785+
786+
}
787+
return true;
788+
}
789+
public bool IsValid(char[][] board, int row, int col, char val)
790+
{
791+
for (int i = 0; i < 9; i++)
792+
{
793+
if (board[i][col] == val) return false;
794+
}
795+
for (int i = 0; i < 9; i++)
796+
{
797+
if (board[row][i] == val) return false;
798+
}
799+
int startRow = (row / 3) * 3;
800+
int startCol = (col / 3) * 3;
801+
for (int i = startRow; i < startRow + 3; i++)
802+
{
803+
for (int j = startCol; j < startCol + 3; j++)
804+
{
805+
if (board[i][j] == val) return false;
806+
}
807+
}
808+
return true;
809+
}
810+
}
811+
```
759812

760813
<p align="center">
761814
<a href="https://programmercarl.com/other/kstar.html" target="_blank">

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /