파이썬/문제

[코드트리] 좌우로 움직이는 로봇

낑깡H 2022. 10. 3. 17:57

🍒 문제 링크 

https://www.codetree.ai/missions/5/problems/robot-moving-from-side-to-side/submissions

 

🍒 문제 분석

 

 

🍒 내 코드

코드

 

🍓 내 해결 과정

 

 

🌽 다른 사람 코드

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


# time에 따른 a,b의 위치 기록할 수 있는 배열 생성
pos_a, pos_b = [0] * 100000, [0] * 100000  
time_a, time_b = 1, 1


#t만큼의 변화동안 pos_a, pos_b
for _ in range(N):
    t, d = tuple(input().split())
    for _ in range(int(t)):
        pos_a[time_a] = pos_a[time_a - 1] + (1 if d == "R" else -1)
        time_a += 1


for _ in range(M):
    t, d = tuple(input().split())
    for _ in range(int(t)):
        pos_b[time_b] = pos_b[time_b - 1] + (1 if d == "R" else -1)
        time_b += 1

#time_a, time_b가 다른 경우, 차이만큼 동안의 위치
if time_a < time_b:
	for i in range(time_a, time_b):
		pos_a[i] = pos_a[i - 1]
elif time_a > time_b:
	for i in range(time_b, time_a):
		pos_b[i] = pos_b[i - 1]


cnt = 0
time_max = max(time_a, time_b)
for i in range(1, time_max):
    if pos_a[i] == pos_b[i] and pos_a[i - 1] != pos_b[i - 1]:
        cnt  += 1


print(cnt)

 

 

 

🍉 깨달은 점 및 정리

 

19행 ~ : time_a랑 time_b 가 다르면 a가 먼저 끝난 동안은 pos_a[i] = pos_a[i-1] 로 입력해 줘야함