Exemplo n.º 1
0
        private bool FindPath(Board board, CellPath path, Cell origin)
        {
            var availableCells = board.GetAvailableCells(origin);

            foreach (var cell in availableCells)
            {
                path.AddCell(cell);

                //Console.WriteLine(_printingService.ToString(board, path));

                if (board.IsSolved())
                {
                    return(true);
                }

                if (!BoardHasStillSolution(board, cell))
                {
                    //Console.WriteLine("Impossible solution, rolling back.");
                }
                else
                {
                    if (FindPath(board, path, cell))
                    {
                        return(true);
                    }
                }

                path.Undo(1);
            }

            return(false);
        }
Exemplo n.º 2
0
        internal CellPath Solve(Board board)
        {
            var path = new CellPath();

            path.AddCell(board.Start);

            FindPath(board, path, board.Start);

            if (board.IsSolved())
            {
                return(path);
            }

            throw new Exception("Impossible problem");
        }