open:power-of-thor

[codingame] Power of Thor

SOLVE IT
토르의 망치, Mjöllnir 는 모든 힘을 잃었다…. 망치의 힘을 되찾기 위해 힘의 빛으로 안내해 줄 수 있겠는가?

주제: 조건문 (if…).

이 퍼즐은 과거 라그나로크 컨테스트에 제안되었던 시리즈중의 첫번째이다. 망치의 힘을 되찾게 되면, 다음 퍼즐로 건너가서 “토르 대 거인”에서 거인을 물리쳐라“. 더욱 어려울것이다.

프로그램

가로 40, 세로 18의 맵에서 움직인다. 토르는 지도위 임의의 지점에서 시작할 것이고, 최대한 빨리 힘의 빛에 도착해야 한다.

각 턴에서, 어느 방향으로 이동할지 다음 경우중에서 결정해야 한다.

  • N (North)
  • NE (North-East)
  • E (East)
  • SE (South-East)
  • S (South)
  • SW (South-West)
  • W (West)
  • NW (North-West)

힘의 빛에 도달하면 당신이 승리한다.

  • 토르가 맵 밖으로 나간다.
  • 힘의 빛에 도달하기 위한 에너지가 더 이상 남아있지 않다

기본 코드

<sxh perl>
select(STDOUT); $| = 1; # DO NOT REMOVE

Auto-generated code below aims at helping you parse

the standard input according to the problem statement.

LX: the X position of the light of power

LY: the Y position of the light of power

TX: Thor's starting X position

TY: Thor's starting Y position

chomp($tokens=<STDIN>);
($LX, $LY, $TX, $TY) = split(/ /,$tokens);

game loop

while (1) {

  chomp($E = <STDIN>); # The level of Thor's remaining energy, representing the number of moves he can still make.
  
  # Write an action using print
  # To debug: print STDERR "Debug messages...\n";
  print "SE\n"; # A single line providing the move to be made: N NE E SE S SW W or NW

}
</sxh>

코드작성

<sxh perl>
select(STDOUT); $| = 1; # DO NOT REMOVE

Auto-generated code below aims at helping you parse

the standard input according to the problem statement.

LX: the X position of the light of power

LY: the Y position of the light of power

TX: Thor's starting X position

TY: Thor's starting Y position

chomp($tokens=<STDIN>);
($LX, $LY, $TX, $TY) = split(/ /,$tokens);

$x = $LX - $TX;
$y = $LY - $TY;

# game loop
while (1) {

  chomp($E = <STDIN>); # The level of Thor's remaining energy, representing the number of moves he can still make.
  
  # Write an action using print
  # To debug: print STDERR "Debug messages...\n";
  print STDERR "energy:",$E,"\n";
  print STDERR "X:",$x,"\tY:",$y,"\n";
  
  if ($x > 0) {
      if ($y > 0) {
          $d = "SE";
          $x--;
          $y--;
      }
      elsif ($y < 0) {
          $d = "NE";
          $x--;
          $y++;
      }
      else{
          $d = "E";
          $x--;
      }
  }
  elsif ($x < 0) {
      if ($y > 0) {
          $d = "SW";
          $x++;
          $y--;
      }
      elsif ($y < 0) {
          $d = "NW";
          $x++;
          $y++;
      }
      else{
          $d = "W";
          $x++;
      }
  }
  else{
      if ($y > 0) {
          $d = "S";
          $y--;
      }
      elsif ($y < 0) {
          $d = "N";
          $y++;
      }
  }
  
  
  
  
  print $d,"\n"; # A single line providing the move to be made: N NE E SE S SW W or NW

}
</sxh>

if문 줄이기

<sxh perl>
select(STDOUT); $| = 1; # DO NOT REMOVE

chomp($tokens=<STDIN>);
($LX, $LY, $TX, $TY) = split(/ /,$tokens);

$x = $LX - $TX;
$y = $LY - $TY;

# game loop
while (1) {

  chomp($E = <STDIN>); # The level of Thor's remaining energy, representing the number of moves he can still make.
  
 
  $d ="";
  
  if ($y > 0) {$d.="S";$y--}
  elsif ($y < 0) {$d.="N";$y++}
  
  if ($x > 0) {$d.="E";$x--}
  elsif ($x < 0) {$d.="W";$x++};
      
 print $d,"\n"; # A single line providing the move to be made: N NE E SE S SW W or NW

}
</sxh>


  • open/power-of-thor.txt
  • 마지막으로 수정됨: 2020/06/02 09:25
  • 저자 127.0.0.1