Anonymous user
Sudoku: Difference between revisions
m
→"Smart" Recursive Backtrack Solution
Line 1,839:
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int RowCol(int rc) => rc <= 2 ? 0 : rc <= 5 ? 3 : 6;
private static
▲ from r in Range(RowCol(row), 3)
▲ from c in Range(RowCol(col), 3)
▲ select (r,c);
▲ private static IEnumerable<int> GetBox(this int[][] grid, int row, int col) =>
private static readonly int[] Domain = Range(1, 9).ToArray();▼
private static bool Solve(this int[][] grid, List<(int, int)> cells, int idx) {
if (idx == 81)
Line 1,861 ⟶ 1,850:
var (r, c) = cells[idx];
foreach (var i in
grid[r][c] = i;
if (grid.Solve(cells, idx + 1))
Line 1,871 ⟶ 1,860:
private static readonly int[] Unmarked = new[] { 0 };
private static IEnumerable<int> Hints(this int[][] grid, int row, int col) =>
grid[row]
.Union(
.Union(
.Except(Unmarked);
private static IEnumerable<(int, int)> SortedCells(this int[][] grid) =>
from row in
from col in
let cell = (count: grid[row][col] > 0 ? 10 : Hints(grid, row, col).Count(), cell: (row, col))
orderby cell.count descending
|