-
Notifications
You must be signed in to change notification settings - Fork 1
/
37SudokuSolver6.cs
143 lines (127 loc) · 4.67 KB
/
37SudokuSolver6.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SudokuSolver6
{
class SudokuSolver6
{
static void Main(string[] args)
{
char[][] board = new char[9][];
board[0] = new char[] { '.', '.', '9', '7', '4', '8', '.', '.', '.' };
board[1] = new char[] { '7', '.', '.', '.', '.', '.', '.', '.', '.' };
board[2] = new char[] { '.', '2', '.', '1', '.', '9', '.', '.', '.' };
board[3] = new char[] { '.', '.', '7', '.', '.', '.', '2', '4', '.' };
board[4] = new char[] { '.', '6', '4', '.', '1', '.', '5', '9', '.' };
board[5] = new char[] { '.', '9', '8', '.', '.', '.', '3', '.', '.' };
board[6] = new char[] { '.', '.', '.', '8', '.', '3', '.', '2', '.' };
board[7] = new char[] { '.', '.', '.', '.', '.', '.', '.', '.', '6' };
board[8] = new char[] { '.', '.', '.', '2', '7', '5', '9', '.', '.' };
solveSudoku(board);
}
/*
* source code from blog:
* http://shanjiaxin.blogspot.ca/2014/04/sudoku-solver-leetcode.html
* convert the java code to C#
*
*/
public static void solveSudoku(char[][] board)
{
if (board == null || board.Length == 0 || board[0].Length == 0)
{
return;
}
solve(board);
}
/*
* source code from blog:
* http://shanjiaxin.blogspot.ca/2014/04/sudoku-solver-leetcode.html
* convert the java code to C#
* Julia's comment:
* 1. 3 loops, question about line 61: return false - debug the code and then try to figure out?
*
*/
protected static bool solve(char[][] board)
{
for (int i = 0; i < board.Length; i++)
{
for (int j = 0; j < board.Length; j++)
{
if (board[i][j] != '.')
{
continue;
}
for (int k = 1; k <= 9; k++)
{
board[i][j] = (char)(k + '0');
if (isValid(board, i, j) && solve(board))
{
return true; // last step return,
// it will go back with true until the first one, return final result
}
// julia's comment: back tracking if there is no solution on this number k
board[i][j] = '.';
}
return false; // tried 1-9, just return false and don't try next, return to previous.
}
}
return true; // all points are filled --> true. the last step
}
/*
* source code from blog:
* http://shanjiaxin.blogspot.ca/2014/04/sudoku-solver-leetcode.html
* convert the java code to C#
* Julia's comment:
* 1. great idea to use HashSet, and then, check if there is any duplicate char in the hash set
*/
private static bool isValid(char[][] board, int a, int b)
{
HashSet<Char> contained = new HashSet<Char>();
// Check a row
for (int i = 0; i < 9; i++)
{
if (contained.Contains(board[a][i]))
{
return false;
}
if (board[a][i] > '0' && board[a][i] <= '9')
{
contained.Add(board[a][i]);
}
}
// Check b column
contained.Clear();
for (int i = 0; i < 9; i++)
{
if (contained.Contains(board[i][b]))
{
return false;
}
if (board[i][b] > '0' && board[i][b] <= '9')
{
contained.Add(board[i][b]);
}
}
// Check sub-box board[a][b] located.
contained.Clear();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
int x = a / 3 * 3 + i, y = b / 3 * 3 + j;
if (contained.Contains(board[x][y]))
{
return false;
}
if (board[x][y] > '0' && board[x][y] <= '9')
{
contained.Add(board[x][y]);
}
}
}
return true;
}
}
}