Пример #1
0
 public Node(Board endState)
 {
     this.EndState = endState;
 }
Пример #2
0
 public Node(Board endState, Node parent, int heuristicCost, Action action)
     : this(endState)
 {
     this.parent = parent;
     this.cost = heuristicCost;
     this.Action = action;
 }
Пример #3
0
        public void Solve()
        {
            var stopWatch = new Stopwatch();
            stopWatch.Start();
            const int size = 3;
            var nodestoExplore = new PriorityQueueDemo.PriorityQueue<int, Node>();
            var exploredStates = new Dictionary<string, Board>();
            var initialBoard = new Board(initialstate, size);
            var initialCost = initialBoard.GetNumberMisplacedBlocksDistanceSum();
            var intialPath = new Node(
                                                    initialBoard,
                                                    null,
                                                    initialCost,
                                                    null
                                                    );

            nodestoExplore.Enqueue(initialCost, intialPath, intialPath.EndState.StateAsString);

            var currentPath = new Node(null);

            while (true)
            {
                currentPath = nodestoExplore.DequeueValue();
                nodestoExplore.DeleteFromStateDictionary(currentPath.EndState.StateAsString);
                var currentState = currentPath.EndState;

                exploredStates.Add(currentState.StateAsString, currentState);
                if (currentState.IsGoal())
                {
                    break;
                }
                foreach (Action action in currentState.GetActionResults())
                {
                    if (
                            !exploredStates.ContainsKey(action.ResultState)
                            &&
                            !nodestoExplore.ContainsState(action.ResultState)

                        )
                    {
                        int cost = currentPath.Cost + action.Cost + 1;
                        Node pathToAdd = new Node(
                                                    new Board(action.ResultState, size),
                                                    currentPath,
                                                    cost,
                                                    action
                                                    );

                        nodestoExplore.Enqueue(cost, pathToAdd, pathToAdd.EndState.StateAsString);

                    }

                }
            }
            stopWatch.Stop();
            //currentPath.Print();
            this.Swaps = currentPath.GetSwapsList();
            //var hmm = "386571240";
            /*foreach (var t in test.Reverse())
            {

                hmm = Board.SwapCharacters(hmm, t.Item1, t.Item2);
                Console.WriteLine(hmm);
            }

            Console.ReadKey();*/
        }