代码之家  ›  专栏  ›  技术社区  ›  Enoy

二进制搜索陷入无限循环

c#
  •  -2
  • Enoy  · 技术社区  · 6 年前

    你好,谢谢你的时间。

    我正在努力提高我解决问题的能力,我正在做以下练习: https://www.codingame.com/ide/puzzle/shadows-of-the-knight-episode-1

    每转一圈你都会得到一个方向,例如:“U”代表“向上”,“Dr”,代表右下,等等。

    你需要在n回合前将角色移动到目标。

    我们得到w,数组的宽度,h,数组的高度。

    我一直在考虑计算角色需要移动的方向的中间点。

    我目前的代码如下:

    using System;
    using System.Linq;
    using System.IO;
    using System.Text;
    using System.Collections;
    using System.Collections.Generic;
    
    /**
     * Auto-generated code below aims at helping you parse
     * the standard input according to the problem statement.
     **/
    class Player
    {
        static string lastMoveDirection = "";
        static int increasedMoveUnit = 0;
        static int W;
        static int H;
        static int X0;
        static int Y0;
    
        static void Main(string[] args)
        {
            string[] inputs;
            inputs = Console.ReadLine().Split(' ');
            W = int.Parse(inputs[0]); // width of the building.
            H = int.Parse(inputs[1]); // height of the building.
            int N = int.Parse(Console.ReadLine()); // maximum number of turns before game over.
            inputs = Console.ReadLine().Split(' ');
             X0 = int.Parse(inputs[0]);
             Y0 = int.Parse(inputs[1]);
    
    
            // game loop
            while (true)
            {
                N--;
                string bombDir = Console.ReadLine(); // the direction of the bombs from batman's current location (U, UR, R, DR, D, DL, L or UL)
    
                // Write an action using Console.WriteLine()
                // To debug: Console.Error.WriteLine("Debug messages...");
    
                Console.Error.WriteLine("Batman pos: " + X0 + " " + Y0); 
                Console.Error.WriteLine("Bomb direction: " + bombDir);
                Console.Error.WriteLine("Width of the array: " + W);
                Console.Error.WriteLine("Height of the array: " + H);
    
                if(lastMoveDirection == bombDir)
                    increasedMoveUnit++;
                else
                    increasedMoveUnit = 0;
    
                switch(bombDir)
                {
                case "DR":
                    X0=CalcMoveDistance("X");
                    Y0=CalcMoveDistance("Y");
                    break;
                case "DL":
                    X0=CalcMoveDistance("X", -1);
                    Y0=CalcMoveDistance("Y");
                    break;
                case "D":
                     Y0=CalcMoveDistance("Y");
                     break;
                case "UR":
                    X0=CalcMoveDistance("X");
                    Y0=CalcMoveDistance("Y", -1);
                    break;
                case "UL":
                    X0=CalcMoveDistance("X", -1);
                    Y0=CalcMoveDistance("Y", -1);
                    break;
                  case "U":
                    Y0=CalcMoveDistance("Y", -1);      
                    break;
                case "L":
                    X0=CalcMoveDistance("X", -1); 
                    break;
                case "R":
                    X0=CalcMoveDistance("X");  
                    break;
                }
    
    
                // the location of the next window Batman should jump to.
                Console.WriteLine(X0.ToString() + " " + Y0.ToString());
                lastMoveDirection = bombDir;
            }
        }
    
        private static int CalcMoveDistance(string axis, int direction = 1) 
        {
            int result = 0;
            switch(axis)
            {
            case "X":
                result = (W+X0*direction)/2;
                break;
            case "Y":
                result = (H+Y0*direction)/2;
                break;
            }
    
    
            if(axis == "Y" && result + increasedMoveUnit >= H)
                result = H-1;
    
            else if(axis == "X" && result + increasedMoveUnit >= W)
                result = W-1;
            else
                result += increasedMoveUnit * direction;
    
            return result;
        }
    }
    

    我面临以下困难:

    字符开始于(6,6) 它向右移动到(23,33)

    对的。

    字符开始于(23,33) 它向右移动到(32,47)

    对的。

    字符起始于(32,47) 它向右移动到(36,6)

    对的。

    字符起始于(36,6) 它向右移动到(38,33)

    对的。

    字符起始于(38,33) 向下移动到(38,46)

    正确(非常接近目标)。

    字符起始于(38,46) 上升到(38,7) 很不客观…

    字符起始于(38,7) 向下移动到(38,33)

    比以前更接近目标…

    字符起始于(38,7) 向下移动到(38,47)

    它非常接近目标…

    字符起始于(38,47) 移动到(38,6) 在Y轴上离目标非常远…

    我们继续循环: 降到(38,33) 降到(3847) 高达(38,6)

    1 回复  |  直到 6 年前
        1
  •  0
  •   eye_am_groot    6 年前

    我猜是因为你有:

    while (true)
    

    而在 for 打破它的循环。

    好像你想要:

    while (N >= 0)  // Or just N > 0, depending on your desired use
    

    自从你减刑 N 在循环开始时