【问题标题】:CodeinGame Power of Thor level 2 wont work雷神 2 级的 CodinGame Power 不起作用
【发布时间】:2015-11-07 19:35:23
【问题描述】:

我在 CodeinGame 中解决了一个问题,我必须将 Thor 带到灯光处在网站上例如: 如果初始 pos 是 (5,4) 它通过 Log 但如果初始 pos 是 (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这是第二题雷神之力

【问题讨论】:

  • 好的,你有问题吗?
  • @Steve 编辑了问题
  • 检查你的雷神南下代码。

标签: c# if-statement logic


【解决方案1】:

您将位置变量重置为 +/-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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-10
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 2017-05-03
    • 2021-07-06
    相关资源
    最近更新 更多