Coding Game - POWER OF THOR - EPISODE 1

本文介绍了一个编程挑战,玩家需帮助Thor角色通过八个方向移动来找到并达到“光”的位置。文章详细展示了两种不同的实现方案,包括原始代码及重构后的代码,并提到了在执行过程中遇到的超时错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POWER OF THOR - EPISODE 1

问题地址: POWER OF THOR - EPISODE 1

问题大意:

Thor 需要找到光的能量(大古熬成汤?),有 8 个方向可以移动,就是东南西北四个垂直的方向,还有东南、东北、西南、西北四个对角线方向。

初始的时候提供了四个变量:

  • the variable lightX: the X position of the light of power that Thor must reach.
  • the variable lightY: the Y position of the light of power that Thor must reach.
  • the variable initialTX: the starting X position of Thor.
  • the variable initialTY: the starting Y position of Thor.

也就是光的 x 轴和 y 轴地点,以及 Thor 的 x 轴和 y 轴地点,然后玩家需要根据所给的地点找到光,成为光之巨人?

思路

第一反应就是,这题很简单,四个方向先确定了,然后再找对角线就好了,但是发现,没这么简单……

一来是多重判断有点绕,做着做着就做丢了,二来是 solution 有 bug。

第一阶段

这是第一阶段的代码,四个垂直的方向——东南西北——都已经能够顺利跑出来了,但是一到对角线就会发现跑出边界这个问题。

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/

var inputs = readline().split(" ");
const lightX = parseInt(inputs[0]); // the X position of the light of power
const lightY = parseInt(inputs[1]); // the Y position of the light of power
let initialTx = parseInt(inputs[2]); // Thor's starting X position
let initialTy = parseInt(inputs[3]); // Thor's starting Y position

// game loop
while (true) {
  const remainingTurns = parseInt(readline()); // The remaining amount of turns Thor can move. Do not remove this line.

  // Write an action using console.log()
  // To debug: console.error('Debug messages...');
  let diffX = 0,
    diffY = 0;

  if (lightX !== initialTx) {
    diffX = lightX - initialTx;
  }

  if (lightY !== initialTy) {
    diffY = lightY - initialTy;
  }

  if (diffX === 0 && diffY < 0) {
    initialTy++;
    console.log("N");
  } else if (diffX === 0 && diffY > 0) {
    initialTy--;
    console.log("S");
  } else if (diffX > 0 && diffY === 0) {
    initialTx++;
    console.log("E");
  } else if (diffX < 0 && diffY === 0) {
    initialTx--;
    console.log("W");
  } else if (diffX < 0 && diffY > 0) {
    // 从这以后就跑不下去了
    if (lightY !== initialTy) {
      initialTy--;
    }
    if (lightX !== initialTx) {
      initialTx--;
    }
    console.log("SW");
  } else if (diffX < 0 && diffY < 0) {
    if (lightY !== initialTy) {
      initialTy++;
    }
    if (lightX !== initialTx) initialTx--;
    console.log("NW");
  } else if (diffX > 0 && diffY < 0) {
    if (lightY !== initialTy) initialTy++;
    if (lightX !== initialTx) initialTx++;
    console.log("NE");
  } else if (diffX > 0 && diffY > 0) {
    if (lightY !== initialTy) initialTy--;
    if (lightX !== initialTx) initialTx++;
    console.log("SE");
  }

  // A single line providing the move to be made: N NE E SE S SW W or NW
  // console.log('SE');
}

第二阶段

做着做着就把自己绕丢了,于是重构代码,分了两个函数,一个专门处理上下左右,即移动一个方向。另外一个函数负责对角线,这样阅读起来清晰一些。

修改过后的答案:

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/

var inputs = readline().split(" ");
const lightX = parseInt(inputs[0]); // the X position of the light of power
const lightY = parseInt(inputs[1]); // the Y position of the light of power
let initialTx = parseInt(inputs[2]); // Thor's starting X position
let initialTy = parseInt(inputs[3]); // Thor's starting Y position

const moveSingleDirection = (diffX, diffY) => {
  if (diffX === 0) {
    // 只能沿着 y 轴垂直移动
    if (diffY < 0) {
      // 光 - Thor < 0,光 在 Thor 的上边,也就是北方
      console.log("N");
      initialTy--;
    } else {
      console.log("S");
      initialTy++;
    }
  } else {
    // 只能沿着 x 轴水平移动
    if (diffX > 0) {
      // 光 - Thor > 0,光在 Thor 的右边,也就是东方
      initialTx++;
      console.log("E");
    } else {
      initialTx--;
      console.log("W");
    }
  }
};

const moveDioganal = (diffX, diffY) => {
  if (diffY < 0) {
    // 光 - Thor < 0,光 在 Thor 的上边,也就是北方
    // 也就是说只能移动 NW, NE
    if (diffX > 0) {
      console.log("NE");
      initialTx++;
    } else {
      console.log("NW");
      initialTx--;
    }
    initialTy--;
  } else {
    // 反之只能移动 SW, SE
    if (diffX > 0) {
      console.log("SE");
      initialTx++;
    } else {
      console.log("SW");
      initialTx--;
    }
    initialTy++;
  }
};

// game loop
while (true) {
  const remainingTurns = parseInt(readline()); // The remaining amount of turns Thor can move. Do not remove this line.

  // Write an action using console.log()
  // To debug: console.error('Debug messages...');
  //   This means the most top left cell has the coordinates "X=0,Y=0"
  const diffX = lightX - initialTx,
    diffY = lightY - initialTy;

  console.error(diffX, diffY);

  if (diffX === 0 || diffY === 0) {
    moveSingleDirection(diffX, diffY);
  } else {
    moveDioganal(diffX, diffY);
  }

  // A single line providing the move to be made: N NE E SE S SW W or NW
  // console.log('SE');

  // 本地调试用的,因为下面这个BUG发现怎么都跑不通,后来发现是答案有问题……
  if (lightX === initialTx && initialTx === initialTy) {
    break;
  }
}

代码重构之后能够跑通 75% 的程序,但是碰到了一个奇怪的 bug:

Timeout: your program did not provide an input in due time. Earth was destroyed!
Thor position = (5,5). Light position = (5,17). Energy = 12

本地跑倒是没有任何问题,令人迷惑:

0 12
S
0 11
S
0 10
S
0 9
S
0 8
S
0 7
S
0 6
S
0 5
S
0 4
S
0 3
S
0 2
S
0 1
S
0 0
S

PS,在讨论版上发现这好像是一个 bug,很多人都碰到了,包括有人用 python3 写的。

官方答案

挺令人失望的????

居然没有做任何的优化啊……

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 * ---
 * Hint: You can use the debug stream to print initialTX and initialTY, if Thor seems not follow your orders.
 **/

var inputs = readline().split(" ");
var lightX = parseInt(inputs[0]); // the X position of the light of power
var lightY = parseInt(inputs[1]); // the Y position of the light of power
var initialTX = parseInt(inputs[2]); // Thor's starting X position
var initialTY = parseInt(inputs[3]); // Thor's starting Y position

var thorX = initialTX;
var thorY = initialTY;

// game loop
while (true) {
  var remainingTurns = parseInt(readline());

  var directionX = "";
  var directionY = "";

  if (thorX > lightX) {
    directionX = "W";
    thorX--;
  } else if (thorX < lightX) {
    directionX = "E";
    thorX++;
  }

  if (thorY > lightY) {
    directionY = "N";
    thorY--;
  } else if (thorY < lightY) {
    directionY = "S";
    thorY++;
  }

  // Write an action using print()
  // To debug: printErr('Debug messages...');

  print(directionY + directionX); // A single line providing the move to be made: N NE E SE S SW W or NW
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值