728x90
문제 링크: https://www.acmicpc.net/problem/2174
2174번: 로봇 시뮬레이션
첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순
www.acmicpc.net
문제 풀이
전형적인 시뮬레이션 문제. 이름부터 시뮬레이션이라 나와있다.
일반적인 문제였으면 쉽게 풀렸을거 같지만 평소와 다른 x, y 좌표여서 다르게 처리해줘야 했다. 로봇을 불러오는 경우는 로봇 리스트를 만들어서
맵을 만들때 빈 공간인 경우는 0으로, 로봇이 있는 경우는 i번 로봇이라면 i로 해줬다.
코드
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a, b = map(int, input().split()) | |
maps = [[0 for _ in range(a)] for _ in range(b)] | |
n, m = map(int, input().split()) | |
robot = [0] | |
direct = {'N': 0, 'E': 1, 'S': 2, 'W': 3} | |
dx = [-1, 0, 1, 0] | |
dy = [0, 1, 0, -1] | |
for i in range(1, n+1): | |
x, y, d = input().split() | |
x, y = int(x)-1, int(y) | |
maps[b-y][x] = i | |
robot.append((b-y, x, direct[d])) | |
orders = [] | |
for _ in range(m): | |
rb, o, repeat = input().split() | |
orders.append((int(rb), o, int(repeat))) | |
for order in orders: | |
rb, o, repeat = order | |
if o == 'F': | |
for _ in range(repeat): | |
x, y, d = robot[rb] | |
nx = x + dx[d] | |
ny = y + dy[d] | |
if nx < 0 or nx >= b or ny < 0 or ny >= a: | |
print('Robot '+str(rb)+' crashes into the wall') | |
exit() | |
if maps[nx][ny] != 0: | |
print('Robot '+str(rb)+' crashes into robot '+str(maps[nx][ny])) | |
exit() | |
maps[x][y], maps[nx][ny] = 0, rb | |
robot[rb] = (nx, ny, d) | |
elif o == 'L': | |
x, y, d = robot[rb] | |
for _ in range(repeat): | |
d = (d+3)%4 | |
robot[rb] = (x, y, d) | |
elif o == 'R': | |
x, y, d = robot[rb] | |
for _ in range(repeat): | |
d = (d+1)%4 | |
robot[rb] = (x, y, d) | |
print('OK') |
728x90
'BOJ' 카테고리의 다른 글
[BOJ][Python] 백준 1766번 - 문제집 (0) | 2021.09.25 |
---|---|
[BOJ][Python] 백준 14502번 - 연구소 (0) | 2021.09.24 |
[BOJ][Python] 백준 4766번 - 일반 화학 실험 (0) | 2021.09.24 |
[BOJ][Python] 백준 18301번 - Rats (2) | 2021.09.23 |
[BOJ][Python] 백준 21300번 - Bottle Return (0) | 2021.09.23 |