예제 #1
0
 /// <summary>
 /// Enqueue current solution path
 /// </summary>
 /// <param name="ruleNumber">The number of applied rule</param>
 void EnqueuePath(int ruleNumber)
 {
     _path.Push(_currentState);
     _visitedStates.Add(_currentState);
     _currentState = new JugState(ruleNumber, J1, J2);
     Console.WriteLine($"({J1.Current}, {J2.Current}) => {_rulesInfo[ruleNumber]}");
 }
예제 #2
0
        public JugProblem(int m, int n)
        {
            J1 = new Jug(m);
            J2 = new Jug(n);

            SetSort(SortType.Asc);

            _rules = Jug.GetTransferRules();

            _currentState = new JugState(0, J1, J2);
        }
예제 #3
0
        /// <summary>
        /// Backtrack for last state to try apply another rule.
        /// </summary>
        /// <returns>A <see cref="bool"/> denoting if backtracking was sucedeed</returns>
        bool BacktrackRule()
        {
            if (_path.Count == 0)
            {
                return(false);
            }

            _visitedStates.Add(_currentState);
            _currentState           = _path.Pop();
            J1                      = (Jug)_currentState.j1.Clone();
            J2                      = (Jug)_currentState.j2.Clone();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"({_currentState.j1.Current}, {_currentState.j2.Current}) => BT - Backtracking");
            Console.ResetColor();

            return(true);
        }
예제 #4
0
        public JugProblem(int m, int n)
        {
            J1 = new Jug(m);
            J2 = new Jug(n);

            SetSort(SortType.Asc);

            _rules = new Dictionary <int, Func <Jug, Jug, bool> >
            {
                { 1, (current, destination) => current.FillJug() },
                { 2, (current, destination) => destination.FillJug() },
                { 3, (current, destination) => current.EmptyJug() },
                { 4, (current, destination) => destination.EmptyJug() },
                { 5, (current, destination) => TransferWater(current, destination) },
                { 6, (current, destination) => TransferWater(destination, current) },
            };

            _currentState = new JugState(0, J1, J2);
        }