Пример #1
0
        private static IImmutableList<Tuple<int, int, int, bool>> BuildInternalRowsForGrid(Grid grid)
        {
            var rowsByCols =
                from row in Rows
                from col in Cols
                let value = grid.ValueAt(row, col)
                select BuildInternalRowsForCell(row, col, value);

            return rowsByCols.SelectMany(cols => cols).ToImmutableList();
        }
Пример #2
0
        private static void Main()
        {
            // http://puzzles.telegraph.co.uk/site/search_puzzle_number?id=27744
            var grid = new Grid(ImmutableList.Create(
                "6 4 9 7 3",
                "  3    6 ",
                "       18",
                "   18   9",
                "     43  ",
                "7   39   ",
                " 7       ",
                " 4    8  ",
                "9 8 6 4 5"));

            grid.Draw();

            var internalRows = BuildInternalRowsForGrid(grid);
            var dlxRows = BuildDlxRows(internalRows);
            var solutions = new Dlx()
                .Solve(dlxRows, d => d, r => r)
                .Where(solution => VerifySolution(internalRows, solution))
                .ToImmutableList();

            Console.WriteLine();

            if (solutions.Any())
            {
                Console.WriteLine($"First solution (of {solutions.Count}):");
                Console.WriteLine();
                DrawSolution(internalRows, solutions.First());
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No solutions found!");
            }
        }