Exemplo n.º 1
0
        private PuzzleState MakeMove(PuzzleState puzzleState, int i, int j)
        {
            char[] stateArr  = puzzleState.Position.ToCharArray();
            char   tempValue = stateArr[i];

            stateArr[i] = stateArr[j];
            stateArr[j] = tempValue;
            return(new PuzzleState(new string(stateArr)));
        }
Exemplo n.º 2
0
        private List <PuzzleState> GenerateNextStates(PuzzleState puzzleState)
        {
            var nextStates = new List <PuzzleState>();

            int zeroIndex = puzzleState.Position.IndexOf('0');
            int zeroX     = zeroIndex / this.size;
            int zeroY     = zeroIndex % this.size;

            if (zeroX > 0)
            {
                int         targetIndex = zeroIndex - this.size;
                PuzzleState nextState   = this.MakeMove(puzzleState, zeroIndex, targetIndex);
                nextState.Direction = DirectionDown;

                nextStates.Add(nextState);
            }

            if (zeroX < this.size - 1)
            {
                int         targetIndex = zeroIndex + this.size;
                PuzzleState nextState   = this.MakeMove(puzzleState, zeroIndex, targetIndex);
                nextState.Direction = DirectionUp;

                nextStates.Add(nextState);
            }

            if (zeroY > 0)
            {
                int         targetIndex = zeroIndex - 1;
                PuzzleState nextState   = this.MakeMove(puzzleState, zeroIndex, targetIndex);
                nextState.Direction = DirectionRight;

                nextStates.Add(nextState);
            }

            if (zeroY < this.size - 1)
            {
                int         targetIndex = zeroIndex + 1;
                PuzzleState nextState   = this.MakeMove(puzzleState, zeroIndex, targetIndex);
                nextState.Direction = DirectionLeft;

                nextStates.Add(nextState);
            }

            return(nextStates);
        }
Exemplo n.º 3
0
        private int Heuristic(PuzzleState state)
        {
            int sum = 0;

            for (int i = 0; i < this.targetState.Position.Length; i++)
            {
                char block   = this.targetState.Position[i];
                int  x       = state.Position.IndexOf(block) / this.size;
                int  y       = state.Position.IndexOf(block) % this.size;
                int  targetX = this.targetState.Position.IndexOf(block) / this.size;
                int  targetY = this.targetState.Position.IndexOf(block) % this.size;

                sum += this.ManhattanDistance(x, y, targetX, targetY);
            }

            return(sum);
        }
Exemplo n.º 4
0
        private void PrintSolution(PuzzleState puzzleState)
        {
            Stack <PuzzleState> result = new Stack <PuzzleState>();

            result.Push(puzzleState);
            while (puzzleState.Parent != null)
            {
                puzzleState = puzzleState.Parent;
                result.Push(puzzleState);
            }

            int steps = 0;

            result.Pop();
            while (result.Count != 0)
            {
                Console.WriteLine(result.Pop().Direction);
                steps++;
            }

            Console.WriteLine("Total steps: {0}", steps);
        }
Exemplo n.º 5
0
        public void Solve()
        {
            Console.WriteLine("Started solving the puzzle...");

            HashSet <PuzzleState>       exploredStates        = new HashSet <PuzzleState>();
            PriorityQueue <PuzzleState> unexploredStatesQueue = new PriorityQueue <PuzzleState>();

            this.initialState.G = 0;
            this.initialState.H = this.Heuristic(this.initialState);
            this.initialState.F = this.initialState.H;
            unexploredStatesQueue.Enqueue(this.initialState);

            while (unexploredStatesQueue.Count != 0)
            {
                PuzzleState currentState = unexploredStatesQueue.Dequeue();

                if (currentState.Position == this.targetState.Position)
                {
                    this.PrintSolution(currentState);
                    break;
                }

                exploredStates.Add(currentState);
                var nextStates = this.GenerateNextStates(currentState);
                foreach (var childState in nextStates)
                {
                    if (exploredStates.Contains(childState))
                    {
                        continue;
                    }

                    childState.G      = currentState.G + 1;
                    childState.H      = this.Heuristic(childState);
                    childState.F      = childState.G + childState.H;
                    childState.Parent = currentState;
                    unexploredStatesQueue.Enqueue(childState);
                }
            }
        }
Exemplo n.º 6
0
 public SlidingBlocksPuzzleSolver(int n, string initialPosition)
 {
     this.initialState = new PuzzleState(initialPosition);
     this.targetState  = new PuzzleState(this.InitializeTargetState(n));
     this.size         = (int)Math.Sqrt(n + 1);
 }