CodinGame 雷神的力量 2 级无法工作

CodeinGame Power of Thor level 2 wont work

我在 CodeinGame 中解决了一个问题,我必须将 Thor 带到灯光处 lightx & lightY 是灯光位置 & thorx & thory 是雷神的位置。& initiaTX,initiaTX 是雷神的起始位置,但它失败了一些网站上的测试用例,例如: 如果初始位置是 (5,4) 它通过 Log 但是如果初始位置是 (31,17) 它会失败 Log

我的代码

        string[] inputs = Console.ReadLine().Split(' ');
    int lightX = int.Parse(inputs[0]); // the X position of the light of power
    int lightY = int.Parse(inputs[1]); // the Y position of the light of power
    int initialTX = int.Parse(inputs[2]); // Thor's starting X position
    int initialTY = int.Parse(inputs[3]); // Thor's starting Y position

    // game loop

    int thorx=initialTX;
    int thory=initialTY;

    string directionX, directionY;

    while (true)
    {
        int remainingTurns = int.Parse(Console.ReadLine()); // The remaining amount of turns Thor can move. Do not remove this line.

        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        if (thorx>lightX)
        {
            directionX="W";
            thorx=-1;
            Console.WriteLine("W");
        }
        else if (thorx<lightX)
        {
            directionX="E";
            thorx=+1;
            Console.WriteLine("E");
        }
        else
        {
            if (thory>lightY)
            {
                directionY="N";    
                thory=-1;
                Console.WriteLine("N");
            }
            else if (thory<lightY)
            {
                directionY="S";
                thorx=+1;
                Console.WriteLine("S");
            }
        }

CodeinGame Link第二题雷神之力

不是递增或递减位置变量,而是将它们重置为 +/-1。这里:

    if (thorx>lightX)
    {
        directionX="W";
        thorx=-1;
        Console.WriteLine("W");
    }

应该是:

    if (thorx>lightX)
    {
        directionX="W";
        thorx -= 1;
        Console.WriteLine("W");
    }

或者更好,因为您对那些 directionXdirectionY 值完全没有用处:

    if (thorx>lightX)
    {
        thorx -= 1;
        Console.WriteLine("W");
    }

下一个问题(正如 vernerik 所指出的)是您在向南移动时正在调整 thorx。南方应该增加 thory.

最后,此代码移动 west/east 直到与目标垂直对齐,然后移动 north/south,这是低效的。它将通过前三个测试,但无法通过第四个测试 - 最佳角度。要通过该测试,您还必须使用对角线移动:NW、NE、SW、SE。