示例#1
0
        private bool AfterShipStatus(BothShore shore, uint shipMissionaryNumber, uint shipCannibalNumber)
        {
            if (shipMissionaryNumber + shipCannibalNumber > 2)
            {
                throw new Exception("The boat can load 2 guys at most!");
            }

            if (StartSide == SideEnum.Left)
            {
                if (shore.LeftShore.MissionaryNumber < shipMissionaryNumber || shore.LeftShore.CannibalNumber < shipCannibalNumber)
                {
                    return(false);
                }

                shore.LeftShore.MissionaryNumber -= shipMissionaryNumber;
                shore.LeftShore.CannibalNumber   -= shipCannibalNumber;
                return(true);
            }

            if (StartSide == SideEnum.Right)
            {
                if (shore.RightShore.MissionaryNumber < shipMissionaryNumber || shore.RightShore.CannibalNumber < shipCannibalNumber)
                {
                    return(false);
                }

                shore.RightShore.MissionaryNumber -= shipMissionaryNumber;
                shore.RightShore.CannibalNumber   -= shipCannibalNumber;
                return(true);
            }

            return(false);
        }
示例#2
0
        static void Main(string[] args)
        {
            var status = new BothShore(0, 0, 3, 3);
            var boat   = new BoatStrategy(); //{ StartSide = SideEnum.Right };

            var statusStack = new Stack <BothShore>();

            statusStack.Push(statusStack);

            var strategyStack = new Stack <BoatStrategy>();

            var currentStatus = status;

            /*
             * //L 0 0
             * L 0 1
             * L 0 2
             * L 1 0
             * L 2 0
             * L 1 1
             * //R 0 0
             * R 0 1
             * R 0 2
             * R 1 0
             * R 2 0
             * R 1 1
             *
             * //* L 0 0
             * //* R 0 0
             * //* 不能无限循环
             */

            while (!currentStatus.IsPassRiverSuccessful())
            {
                uint formerStrategyIndex = 1;
                for (uint index = formerStrategyIndex, index <= 10, index++)
                {
                    boat.AfterShipStatus(BoatStrategy.Strategies[index], status);
                    statusStack.Push(status);
                    strategyStack.Push(BoatStrategy.Strategies[index]);
                    currentStatus = status;

                    if (!status.ValidateWholeStatus())
                    {
                        currentStatus = statusStack.Pop();
                        strategyStack.Pop();
                    }

                    //if(currentStatus.IsPassRiverSuccessful())
                    //{
                    //    break;
                    //}
                }
            }

            Console.ReadKey();
        }
示例#3
0
        public bool AfterShipStatus(StrategyEnum strategy, BothShore shore)
        {
            /*
             * L 0 1
             * L 0 2
             * L 1 0
             * L 2 0
             * L 1 1
             * R 0 1
             * R 0 2
             * R 1 0
             * R 2 0
             * R 1 1
             */
            switch (strategy)
            {
            case StrategyEnum.StrategyEnum1:
                return(AfterShipStatus(shore, SideEnum.Left, 0, 1));

            case StrategyEnum.StrategyEnum2:
                return(AfterShipStatus(shore, SideEnum.Left, 0, 2));

            case StrategyEnum.StrategyEnum3:
                return(AfterShipStatus(shore, SideEnum.Left, 1, 0));

            case StrategyEnum.StrategyEnum4:
                return(AfterShipStatus(shore, SideEnum.Left, 2, 0));

            case StrategyEnum.StrategyEnum5:
                return(AfterShipStatus(shore, SideEnum.Left, 1, 1));

            case StrategyEnum.StrategyEnum6:
                return(AfterShipStatus(shore, SideEnum.Right, 0, 1));

            case StrategyEnum.StrategyEnum7:
                return(AfterShipStatus(shore, SideEnum.Right, 0, 2));

            case StrategyEnum.StrategyEnum8:
                return(AfterShipStatus(shore, SideEnum.Right, 1, 0));

            case StrategyEnum.StrategyEnum9:
                return(AfterShipStatus(shore, SideEnum.Right, 2, 0));

            case StrategyEnum.StrategyEnum10:
                return(AfterShipStatus(shore, SideEnum.Right, 1, 1));
            }
        }