BOJ - 2174 - 로봇 시뮬레이션

Updated:

import sys

def solution():
    toMove = ['N', 'E', 'S', 'W']
    dr = [1, 0, -1, 0]
    dc = [0, 1, 0, -1]

    A, B = map(int, sys.stdin.readline().split(' '))
    N, M = map(int, sys.stdin.readline().split(' '))

    Field = [[0 for _ in range(A)] for _ in range(B)]
    Robot = [[0, 0, 0]]

    for i in range(N):
        x, y, to = map(str, sys.stdin.readline().split(' '))
        Field[int(y)-1][int(x)-1] = (i + 1)
        Robot.append([toMove.index(to[0]), int(y)-1, int(x)-1])

    for _ in range(M):
        robot, move, count = map(str, sys.stdin.readline().split(' '))
        robot = int(robot)
        for _ in range(int(count)):
            if move == 'L':
                Robot[robot][0] = (Robot[robot][0] - 1) % 4
            elif move == 'R':
                Robot[robot][0] = (Robot[robot][0] + 1) % 4
            else:
                curX = Robot[robot][1]
                curY = Robot[robot][2]
                nextX = Robot[robot][1] + dr[Robot[robot][0]]
                nextY = Robot[robot][2] + dc[Robot[robot][0]]

                if not 0 <= nextX < B or not 0 <= nextY < A:
                    print("Robot {} crashes into the wall".format(robot))
                    return

                if Field[nextX][nextY]:
                    print("Robot {} crashes into robot {}".format(robot, Field[nextX][nextY]))
                    return

                Field[curX][curY] = 0
                Field[nextX][nextY] = robot
                Robot[robot][1] = nextX
                Robot[robot][2] = nextY
            
    print('OK')
solution()

https://www.acmicpc.net/problem/2174

Categories:

Updated:

Leave a comment