-
Notifications
You must be signed in to change notification settings - Fork 1
/
37sudokuSolver3.cs
196 lines (164 loc) · 7.27 KB
/
37sudokuSolver3.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace sudokuSolver3
{
class sudokuSolver3
{
static void Main(string[] args)
{
/**
* Latest update: July 20, 2015
* Test case:
*
*/
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', '.', '.' };
solveSudokuRe(board, 0, 0);
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 9; j++)
{
System.Console.Write(board[i][j] + ", ");
}
System.Console.WriteLine();
}
/*
* debug through the code
*/
char[][] board2 = new char[9][];
board2[0] = new char[] { '.', '.', '9', '7', '4', '8', '.', '.', '.' };
board2[1] = new char[] { '7', '.', '.', '.', '.', '.', '.', '.', '.' };
board2[2] = new char[] { '.', '2', '.', '1', '.', '9', '.', '.', '.' };
board2[3] = new char[] { '.', '.', '7', '.', '.', '.', '2', '4', '.' };
board2[4] = new char[] { '.', '6', '4', '.', '1', '.', '5', '9', '.' };
board2[5] = new char[] { '.', '9', '8', '.', '.', '.', '3', '.', '.' };
board2[6] = new char[] { '.', '.', '.', '8', '.', '3', '.', '2', '.' };
board2[7] = new char[] { '.', '.', '.', '.', '.', '.', '.', '.', '6' };
board2[8] = new char[] { '.', '.', '.', '2', '7', '5', '9', '.', '.' };
solveSudoku(board2);
}
public static void solveSudoku(char[][] board) {
KeyValuePair<int, int> kvP = new KeyValuePair<int, int>(0, 0);
KeyValuePair<int, int> next =
(board[0][0] == '.') ? kvP : getNextMissing(board, 0, 0);
solveSudokuRe(board, next.Key, next.Value);
}
/*
* source code from blog:
* https://github.com/yinlinglin/LeetCode/blob/master/SudokuSolver.h
* convert C++ code to C# code
*
*/
public static bool solveSudokuRe(char[][]board, int row, int col) {
if (row == 9) return true;
KeyValuePair<int, int> next = getNextMissing(board, row, col);
ArrayList possible = new ArrayList();
getPossibleValues(board, row, col, possible);
for (int i = 0; i < possible.Count; ++i)
{
object o = possible[i];
int val = Convert.ToInt16(o);
board[row][col] = (char)val;
if (solveSudokuRe(board, (int)next.Key, (int)next.Value))
return true;
// back tracking
board[row][col] = '.';
}
return false;
}
/**
* source code from blog:
* https://github.com/yinlinglin/LeetCode/blob/master/SudokuSolver.h
*
*
* convert it from c++ to C#
* Julia comment:
* 1. C++ pair<int, int> <-> KeyValuePair<string, string>
* checked the webpage:
* http://stackoverflow.com/questions/166089/what-is-c-sharp-analog-of-c-stdpair
* 2. C++ make_pair <-> C# KeyValuePair class
* 3. C++ make_pair(row, col) <-> C# new KeyValuePair<int, int>(row, col)
* http://stackoverflow.com/questions/15495165/how-to-initialize-keyvaluepair-object-the-proper-way
learn template syntax again.
*/
private static KeyValuePair<int, int> getNextMissing(char[][] board, int row, int col)
{
while (true)
{
// julia's comment: formula is very concise, excellent!
row = (col == 8) ? row + 1 : row;
col = (col + 1) % 9;
if (row == 9 || board[row][col] == '.')
return new KeyValuePair<int, int>(row, col);
}
}
/**
* source code from blog:
* https://github.com/yinlinglin/LeetCode/blob/master/SudokuSolver.h
* convert it from c++ to C#
* julia comment:
* 1. Learn difference from C++ and C#
* 2. this code is fun to play with -
* C++ bool value[9] = {false}; <-> C# Enumerable.Repeat(false, 9).ToArray()
* 3. retrieve the possible chars for row, col position
*
*/
private static void getPossibleValues(char[][] board, int row, int col, ArrayList possible)
{
//http://stackoverflow.com/questions/1014005/how-to-populate-instantiate-a-c-sharp-array-with-a-single-value
bool[] value = Enumerable.Repeat(false, 9).ToArray();
for (int i = 0; i < 9; ++i)
{
char charInRows = board[i][col];
if (charInRows != '.')
value[charInRows - '1'] = true;
char charInCols = board[row][i];
if (charInCols != '.')
value[charInCols - '1'] = true;
// julia's comment:
// spend 5 minutes to work out this formula
// (row, col) (5,6)
// newRow = 5/3*3 + 5/3 = 4
// newCol = 6/3 * 3 +6%3 = 6+0 = 6
// still confused !
// small 3 x 3 matrix, left top corner: (row/3, col/3)
// relative position to the left top corner: (i/3, i%3)
// row shift: i/3
// column shift: i%3 - make sense
// so let julia rewrite this formula to make more readable
/* code from blog
int newRow = row / 3 * 3 + i / 3;
int newCol = col / 3 * 3 + i % 3;
*/
/* julia comment: add some explanation variable */
int leftTop_small3x3_x = row / 3 * 3;
int leftTop_small3x3_y = col / 3 * 3;
int rowShift = i / 3;
int colShift = i % 3;
int newRow = leftTop_small3x3_x + rowShift;
int newCol = leftTop_small3x3_y + colShift;
/* end of change by julia */
char c = board[newRow][newCol];
if (c != '.') value[c-'1'] = true;
}
for (int i = 0; i < 9; ++i)
if (!value[i])
{
int val = i + '1';
possible.Add(val);
}
}
}
}