본문 바로가기
Programming/Contest

[codingame] Power of Thor

by NAMP 2014. 12. 13.
[codingame] Power of Thor

Power of Thor


해결하기

토르의 망치, 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)

승리조건

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

패배 조건:

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

기본 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
}

코드작성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
}

if문 줄이기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
}











'Programming > Contest' 카테고리의 다른 글

[checkio] Non-unique Elements  (0) 2016.04.05
[codingame] The Descent  (0) 2014.12.13
[codingame] Onboarding  (0) 2014.12.13
=dovelet 블럭 색칠하기/paintblock  (0) 2014.08.19
=dovelet 퓨 즈/fuse  (0) 2014.08.19

댓글