コード例 #1
0
        private (string way, int k) GetNewK(string way, int k, StairsWithDicks stairsWithDicks, int step)
        {
            int nextStep = step + 1;
            int jumpStep = step + 2;

            if (jumpStep <= stairsWithDicks.Count)
            {
                (string way, int k)next = GetNewK(way, k, stairsWithDicks, nextStep);
                (string way, int k)jump = GetNewK(way, k, stairsWithDicks, jumpStep);
                if (next.k < jump.k)
                {
                    k  += next.k;
                    way = $"{nextStep};{next.way}";
                }
                else
                {
                    k  += jump.k;
                    way = $"{jumpStep};{jump.way}";
                }
            }
            else
            {
                way = $"End;{way}";
            }
            if (stairsWithDicks.HasStep(step))
            {
                int count = stairsWithDicks.GetDicksForStep(step);
                k += count;
            }
            return(way, k);
        }
コード例 #2
0
        public void Test1(int stepCount, int dickCount)
        {
            StairsWithDicks stairsWithDicks = _dickService.DropDicks(stepCount, dickCount);

            (string way, int k)result = _dickService.Find(stairsWithDicks);

            Console.WriteLine("Stairs: " + string.Join(';', Enumerable.Range(1, stepCount).Select(s => stairsWithDicks.GetDicksForStep(s))));
            Console.WriteLine("Way: " + result.way);
            Console.WriteLine("K: " + result.k);
        }
コード例 #3
0
        public StairsWithDicks DropDicks(int stepCount, int dickCount)
        {
            var stairsWithDicks = new StairsWithDicks(stepCount);

            var random = new Random();

            for (int i = 0; i < dickCount; i++)
            {
                int step = random.Next(1, stepCount + 1);
                stairsWithDicks.AddDicksForStep(step);
            }

            return(stairsWithDicks);
        }
コード例 #4
0
 public (string way, int k) Find(StairsWithDicks stairsWithDicks)
 {
     (string way, int k)result = GetNewK(string.Empty, k: 0, stairsWithDicks, step: 0);
     return(result);
 }