백준 구현 로봇 시뮬레이션

yjkim·2023년 8월 13일
0

알고리즘

목록 보기
40/59

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

코드

movedict={
  'N':0,
  'E':1,
  'S':2,
  'W':3
}
movelist=[[1,0], [0,1], [-1,0], [0,-1]]

a,b=map(int , input().split())
graph=[[0 for i in range(a)] for j in range(b)]
n,m=map(int ,input().split())
robot_place=[]
for i in range(n):
  x,y,d=input().split()
  x=int(x)
  y=int(y)
  d=movedict[d]
  robot_place.append([y-1,x-1,d])
  graph[y-1][x-1]=i+1

orderlist=[]
for i in range(m):
  robot,order, repeat=input().split()
  orderlist.append([robot,order,repeat])

for i in range(m):
  robot,order,repeat=orderlist[i]
  count=0
  robot=int(robot)
  repeat=int(repeat)
  ci,cj,cd=robot_place[robot-1][0],robot_place[robot-1][1],robot_place[robot-1][2]
  while count<repeat:
    if order=='L':
      robot_place[robot-1][2]=(robot_place[robot-1][2]+3)%4
    elif order=='R':
      robot_place[robot-1][2]=(robot_place[robot-1][2]+1)%4
    else:
      ni,nj=ci+movelist[cd][0],cj+movelist[cd][1]
      if not (0<=ni<b and 0<=nj<a):
        print('Robot {} crashes into the wall'.format(robot))
        exit()
        # 어쩌구 저쩌구
      elif graph[ni][nj]!=0:
        print('Robot {} crashes into robot {}'.format(robot, graph[ni][nj]))
        exit()
      else:
        graph[ci][cj]=0
        graph[ni][nj]=robot
        robot_place[robot-1][0],robot_place[robot-1][1]=ni,nj
        ci,cj=ni,nj
    count+=1

print("OK")

실수

  1. 좌표계 설정을 잘못함, 문제에서 북쪽(N)으로 향할 경우 좌표계가 늘어남, 즉 idx가 늘어나는 방향을 N으로 설정해야 했으나 초기에 N을 idx가 줄어드는 방향으로 설정하여 N값의 방향에 [-1,0]의 방향을 설정해버림,,, 그래서 에러가 났던것

  2. 인덱스와 실제 로봇의 순서를 헷갈림 i번째 로봇의 graph상 숫자는 i로 표시되지만 robot_place의 idx값으로는 i-1에 해당함. 이를 고려하여 문제를 풀어야함

  3. 데이터 갱신은 마지막에

배운거

포맷팅을 활용한 출력

print("ㅎㅎㅎㅎㅎ{} {} {}".format(변수1, 변수2)) 하면 원하는 변수 끼워넣어서 출력 가능
a=input().split() 으로 하면 문자열로 반환됨
a=["입력","된","값이요"]
이런 식으로
profile
컴퓨터 공부 / 백엔드 개발

0개의 댓글