Anonymous user
Sudoku: Difference between revisions
→Recursive Backtrack Solution: More fluent syntax
(Changed my →“Amb” Solution: to better and shorter →Recursive Backtrack Solution) |
(→Recursive Backtrack Solution: More fluent syntax) |
||
Line 1,835:
namespace Sudoku {
static class Program {
private static bool Unique(this IEnumerable<int> values) => values.Distinct().Count() == values.Count();
private static bool IsUnequal(this IEnumerable<int> values) =>
private static int RowCol(int rc) => rc <= 2 ? 0 : rc <= 5 ? 3 : 6;
Line 1,844:
select grid[r][c];
private static bool Constraints(this int[][] grid, int row, int col) =>
private static (int, int) NextEmptyCell(this int[][] grid, int r, int c) {
while (r < 9 && grid[r][c] != 0) {
c = ++c % 9;
Line 1,857:
}
private static (bool, int[][]) Solve(this int[][] grid, int row, int col) {
var (r, c) = grid.NextEmptyCell(
if (r == 9) //success
Line 1,865:
for (var i = 1; i <= 9; i++) {
grid[r][c] = i;
if (!grid.Constraints(
continue;
var (success, g) = grid.Solve(
if (!success)
continue;
Line 1,893:
.ToArray();
var (success, result) = input.Solve(
if (success)
Line 1,907:
}
}
}
Output
<pre>975342861
|