728x90
2174번: 로봇 시뮬레이션
첫째 줄에 두 정수 A, B가 주어진다. 다음 줄에는 두 정수 N, M이 주어진다. 다음 N개의 줄에는 각 로봇의 초기 위치(x, y좌표 순) 및 방향이 주어진다. 다음 M개의 줄에는 각 명령이 명령을 내리는 순
www.acmicpc.net
🤔 문제분석
간단한 구현문제로, 주어진 명령의 조건에따라서 수행하면된다. 특이한점은 y좌표의 그래프가 거꾸로 되어있기때문에 y좌표가 거꾸로 되어있는것만 잘 맞추어준다면 쉽게 문제를 해결 할 수 있다. 또한 방향을 결정할때에 4바퀴를 돌면 원점임으로 4를 나눈 나머지의 로테이션을 하게되면 시간복잡도를 줄일 수 있다.
💻 코드
import sys
input = sys.stdin.readline
A, B = map(int, input().split())
N, M = map(int, input().split())
robots = [(0, 0, 0)]
command = []
def L(direciton):
if direciton == 'W':
return 'S'
if direciton == 'S':
return 'E'
if direciton == 'E':
return 'N'
if direciton == 'N':
return 'W'
def R(direciton):
if direciton == 'W':
return 'N'
if direciton == 'N':
return 'E'
if direciton == 'E':
return 'S'
if direciton == 'S':
return 'W'
def F(y, x, direction):
if direction == 'W':
return y, x-1
if direction == 'S':
return y+1, x
if direction == 'E':
return y, x+1
if direction == 'N':
return y-1, x
def check_crash(robot, y, x):
for i in range(1, len(robots)):
if robots[i] != robot:
r_y, r_x, _ = robots[i]
if r_y == y and r_x == x:
return i
return 0
def is_range(y, x):
return B > y >= 0 and A > x >=0
for _ in range(N):
x, y, dir = map(str, input().split())
robots.append((B-int(y), int(x)-1, dir))
for _ in range(M):
robot, cmd, cnt = map(str, input().split())
command.append((int(robot), cmd, int(cnt)))
is_crashed = False
for robot, cmd, cnt in command:
y, x, dir = robots[robot]
if cmd == 'L':
cnt %= 4
for _ in range(cnt):
dir = L(dir)
elif cmd == 'R':
cnt %= 4
for _ in range(cnt):
dir = R(dir)
elif cmd == 'F':
for _ in range(cnt):
if is_crashed:
break
y, x = F(y, x, dir)
crash_robot = check_crash(robot, y, x)
if crash_robot:
is_crashed = True
print("Robot %d crashes into robot %d" % (robot, crash_robot))
if not is_range(y, x):
is_crashed = True
print("Robot %d crashes into the wall" % robot)
robots[robot] = (y, x, dir)
if not is_crashed:
print("OK")
🎯 피드백 및 개선사항
파이썬의 포맷 출력할때에는 print문의 % (변수, 변수)를 넣으면 C++ 스타일의 format을 출력할 수 있다.
728x90
'알고리즘' 카테고리의 다른 글
[백준] 17822번 : 원판 돌리기 (0) | 2024.03.16 |
---|---|
[이것이코딩테스트다] 1로 만들기 (0) | 2023.11.19 |
[알고리즘 - 이론] 분기한정법 (0) | 2023.07.25 |
파이썬 공간복잡도 (0) | 2023.06.24 |
파이썬 시간복잡도 (0) | 2023.06.24 |