Esempio n. 1
0
        public void ProcessFloorStatePossibilities(FloorState floorState)
        {
            List <int> possibleElevatorStops = floorState.ListOfPossibleFloors();

            foreach (int targetedFloor in possibleElevatorStops)
            {
                // Now need to handle all valid pairs
                List <string> objectFloorForPairs = floorState.CurrentObjectFloor().ToList();
                var           combinations        = CombinationUtility.GetAllPermutationPairsOrderDoesNotMatter(objectFloorForPairs);
                foreach (var combo in combinations)
                {
                    // If the two items are of different types, need to make sure valid

                    /*
                     * string element = combo.Item1.Substring(0, 1);
                     * if ((combo.Item1.Contains("M") && combo.Item2.Contains("M")) ||
                     *  (combo.Item1.Contains("G") && combo.Item2.Contains("G")) ||
                     *   combo.Item2.Contains(element))
                     * {
                     */
                    List <List <string> > copyOfObjectFloors = floorState.DeepCloneObjectFloors();
                    copyOfObjectFloors[targetedFloor].Add(combo.Item1);
                    copyOfObjectFloors[targetedFloor].Add(combo.Item2);
                    copyOfObjectFloors[floorState.Elevator].Remove(combo.Item1);
                    copyOfObjectFloors[floorState.Elevator].Remove(combo.Item2);

                    // Make new FloorState and if valid, add to queue
                    FloorState newFloorState = new FloorState(floorState.Step + 1, copyOfObjectFloors, targetedFloor);
                    if (!newFloorState.IsInvalidSituation() && floorState.PreviousStates.Contains(newFloorState, new FloorStateEqualityComparer()) == false)
                    {
                        var prevStates = floorState.DeepClonePreviousStates();
                        prevStates.Add(floorState);
                        newFloorState.PreviousStates = prevStates;
                        _queue.Add(newFloorState);
                    }
                }

                // Can move one element, but only if there was not a valid pair movement
                List <string> objectFloor = floorState.CurrentObjectFloor().ToList();
                foreach (string obj in objectFloor)
                {
                    // Make a copy so we can modify original
                    List <List <string> > copyOfObjectFloors = floorState.DeepCloneObjectFloors();
                    copyOfObjectFloors[targetedFloor].Add(obj);
                    copyOfObjectFloors[floorState.Elevator].Remove(obj);

                    // Make new FloorState and if valid, add to queue
                    FloorState newFloorState = new FloorState(floorState.Step + 1, copyOfObjectFloors, targetedFloor);
                    if (!newFloorState.IsInvalidSituation() && floorState.PreviousStates.Contains(newFloorState, new FloorStateEqualityComparer()) == false)
                    {
                        var prevStates = floorState.DeepClonePreviousStates();
                        prevStates.Add(floorState);
                        newFloorState.PreviousStates = prevStates;
                        _queue.Add(newFloorState);
                    }
                }
            }
        }
        public int CountOfMinimumStepsNeededForGoals(string input, bool includeStartingPoint)
        {
            _maze = InitializeMaze(input);
            List <Coordinate> goalCells = new List <Coordinate>();

            foreach (List <Coordinate> row in _maze)
            {
                foreach (Coordinate cell in row)
                {
                    if (cell.GoalNumber >= 0)
                    {
                        goalCells.Add(cell);
                    }
                }
            }

            List <PairDistance> distances = new List <PairDistance>();

            var allPairs = CombinationUtility.GetAllPermutationPairsOrderDoesNotMatter(goalCells);

            foreach (var pair in allPairs)
            {
                PairDistance pairDistance = new PairDistance();
                Coordinate   from         = pair.Item1;
                Coordinate   to           = pair.Item2;
                int          distance     = NumberOfStepsToTarget(from, to);

                pairDistance.From  = from.GoalNumber;
                pairDistance.To    = to.GoalNumber;
                pairDistance.Steps = distance;
                distances.Add(pairDistance);
            }

            int smallestDistance = GetMinimumDistanceAsTravelingSalesmanProblem(distances, goalCells, includeStartingPoint);

            return(smallestDistance);
        }