로봇 시뮬레이션
포스트
취소

로봇 시뮬레이션

로봇 시뮬레이션

접근방법

딕셔너리를 활용하여 방향을 매핑하였다

그리고 단순 구현? 으로

F라면 1칸 전진 하며 갈 수 있는 장소인지 체크

L, R 이라면 4로나눈 몫을 구하여 방향을 저장해주었다

코드

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
74
75
76
77
78
79
80
# 2174 / 로봇 시뮬레이션
# https://www.acmicpc.net/problem/2174

# E, N, W, S
d = {
    0 : [1, 0],
    1 : [0, 1],
    2 : [-1, 0],
    3 : [0, -1],
}

news = {
    'E' : 0,    
    'N' : 1,
    'W' : 2,
    'S' : 3,
}

def move(robot, m, t):

    if m == 'F':        
        cx, cy, s = robots[robot]
        dx, dy = d[s]
        arr[cx][cy] = 0

        while t:
            nx = cx + dx
            ny = cy + dy

            if 0 <= nx < A and 0 <= ny < B and arr[nx][ny] == 0:
                cx = nx
                cy = ny
            else:
                if 0 <= nx < A and 0 <= ny < B:
                    print('Robot %d crashes into robot %d' % (robot, arr[nx][ny]))
                    quit()
                else:
                    print('Robot %d crashes into the wall' % (robot))
                    quit()
            t -= 1
        
        robots[robot] = [cx, cy, s]
        arr[cx][cy] = 1

        return

    elif m == 'L':
        cx, cy, s = robots[robot]
        s = (s + t) % 4
        robots[robot][2] = s
        return
    elif m == 'R':
        cx, cy, s = robots[robot]
        s = (s - t) % 4
        robots[robot][2] = s
        return

A, B = map(int, input().split())
N, M = map(int, input().split())

arr = [[0] * B for _ in range(A)]
robots = [[] for _ in range(N + 1)]

for idx in range(1, N + 1):
    x, y, s = input().split()
    x = int(x) - 1
    y = int(y) - 1

    arr[x][y] = idx
    robots[idx] = [x, y, news[s]]

for _ in range(M):
    robot, m, t = input().split()
    robot = int(robot)
    t = int(t)

    move(robot, m, t)

print('OK')

이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.