2025년, 코딩은 선택이 아닌 필수!

2025년 모든 학교에서 코딩이 시작 됩니다. 먼저 준비하는 사람만이 기술을 선도해 갑니다~

정보/게임으로 배우는 알고리즘

[게임으로 배우는 알고리즘]POWER OF THOR - EPISODE 1

파아란기쁨1 2023. 3. 2. 18:34
반응형

문제) https://www.codingame.com/training/easy/power-of-thor-episode-1

 

Practice Conditions with the exercise "Power of Thor - Episode 1"

Want to practice coding? Try to solve this easy puzzle "Power of Thor - Episode 1" (25+ languages supported).

www.codingame.com

 

이 퍼즐로 배울 수 있는 것 : 조건문과 변수의 사용법을 익힌다.

문제) 토르가 light of power 에 도착하여 힘을 되찾아 주는 문제

문제해결)

더보기
import sys
import math

# 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.

# light_x: the X position of the light of power
# light_y: the Y position of the light of power
# initial_tx: Thor's starting X position
# initial_ty: Thor's starting Y position
light_x, light_y, initial_tx, initial_ty = [int(i) for i in input().split()]
cur_y=initial_ty
cur_x=initial_tx

# game loop
while True:
    remaining_turns = int(input())  # The remaining amount of turns Thor can move. Do not remove this line.

    # Write an action using print
    # To debug: print("Debug messages...", file=sys.stderr, flush=True)
    if(light_x==cur_x and light_y < cur_y ) : 
        print("N")
        cur_y-=1
    elif(light_x>cur_x and light_y < cur_y ) : 
        print("NE")
        cur_y-=1
        cur_x+=1
    elif(light_x>cur_x and light_y == cur_y ) : 
        print("E")
        cur_x+=1
    elif(light_x>cur_x and light_y > cur_y ) : 
        print("SE")
        cur_x+=1
        cur_y+=1
    elif(light_x==cur_x and light_y > cur_y ) : 
        print("S")
        cur_y+=1
    elif(light_x<cur_x and light_y > cur_y ) : 
        print("SW")
        cur_y+=1
        cur_x-=1
    elif(light_x<cur_x and light_y == cur_y ) : 
        print("W")
        cur_x-=1
    elif(light_x<cur_x and light_y < cur_y ) : 
        print("NW")
        cur_x-=1
        cur_y-=1
    print(light_y,initial_ty, file=sys.stderr, flush=True)
       


    # A single line providing the move to be made: N NE E SE S SW W or NW
    #print("SE")

처음 자신의 위치에서 자신의 위치를 변경하면서 동서남북을 체크하여 해당 위치로 이동하도록 출력합니다.

또한 해당 위치로 이동하면 자신의 위치를 변경하여 현재 위치를 가지고 있으면서 이동을 합니다.

이 문제를 통해 x,y 축에 대한 개념과 방향에 대한 개념을 이해 합니다.

 

2단계) SUBMIT 버튼을 클릭하여 제출 합니다.

 

 

반응형