Sudoku: Difference between revisions
Content deleted Content added
→Recursive Backtrack Solution: More fluent syntax |
→Recursive Backtrack Solution: Aded more fluent code in main() |
||
Line 1,829: | Line 1,829: | ||
<!-- By Martin Freedman, 20/11/2021 --> |
<!-- By Martin Freedman, 20/11/2021 --> |
||
<lang csharp>using System.Linq; |
<lang csharp>using System.Linq; |
||
using System.Linq; |
|||
using static System.Linq.Enumerable; |
using static System.Linq.Enumerable; |
||
using static System.Console; |
using static System.Console; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System; |
|||
namespace Sudoku { |
namespace Sudoku { |
||
Line 1,852: | Line 1,854: | ||
while (r < 9 && grid[r][c] != 0) { |
while (r < 9 && grid[r][c] != 0) { |
||
c = ++c % 9; |
c = ++c % 9; |
||
r = c == 0 ? |
r += c == 0 ? 1 : 0; |
||
} |
} |
||
return (r, c); |
return (r, c); |
||
Line 1,872: | Line 1,874: | ||
else |
else |
||
return (true, g); |
return (true, g); |
||
} |
} |
||
grid[r][c] = 0; //backtrack |
grid[r][c] = 0; //backtrack |
||
return (false, grid); |
return (false, grid); |
||
} |
} |
||
private static U Fwd<T, U>(this T t, Func<T, U> func) => func(t); |
|||
static void Main(string[] args) { |
static void Main(string[] args) { |
||
var challenge = new string[]{ "970 340 060", |
var challenge = new string[]{ "970 340 060", |
||
Line 1,893: | Line 1,895: | ||
.ToArray(); |
.ToArray(); |
||
var |
var report = input.Solve(0, 0) switch { |
||
⚫ | |||
Range(0, 9) |
|||
.Select(r => Range(0, 9).Select(c => result[r][c]).Fwd(string.Concat)) |
|||
.Fwd(s => string.Join('\n', s)), |
|||
(_, _) => |
|||
"No Solution\n" |
|||
}; |
|||
Write(report); |
|||
else |
|||
⚫ | |||
ReadLine(); |
ReadLine(); |
||
} |
} |