본문 바로가기

Problem Solving

[Simulation] 교차로-소프티어(별3)

문제 제목 : 교차로

문제 출처 : 소프티어

알고리즘 : simulation

문제원본링크

https://softeer.ai/practice/info.do?idx=1&eid=803 

 

Softeer

자율주행차가 아래와 같은 교차로를 통과하는 상황을 생각하여 보자. 이 문제에서 다루는 교차로에서는 직진만 가능하기 때문에, 아래 그림과 같은 네 가지 방법으로만 교차로

softeer.ai

import sys; reader = sys.stdin.readline
from collections import defaultdict,deque
from sys import setrecursionlimit
setrecursionlimit(5000)
INF = int(2e9)

aord = ord('A'); tonum = defaultdict(int); tonum[(1<<0)] = 0; tonum[(1<<1)] = 1; tonum[(1<<2)] = 2; tonum[(1<<3)] = 3
block = defaultdict(int)
block[0] = 3; block[1] = 0; block[2] = 1; block[3] = 2

n = int(reader().rstrip())
INF = 0
car = [deque([]) for _ in range(4)]

all = 0
for nn in range(n):
    time, w = input().split()
    time = int(time)
    car[ord(w) - aord].append( [time,nn] )
    all +=1
    INF = max( INF, time+1)


ans = [-1] * n

pre = -1
for _ in range(all):
    nowbit, nowtime,cnt = 0, INF, 0 
    flag = False 
    for i in range(4):
        if car[i] :
            if pre+1 >= car[i][0][0]: 
                if flag: 
                    nowbit |= (1<<i)
                    cnt +=1
                else: 
                    nowbit = (1<<i)
                    cnt = 1; 
                    nowtime = pre+1
                    flag = True
            else: 
                if flag: continue
                if nowtime > car[i][0][0]:
                    nowbit = (1<<i)
                    nowtime = car[i][0][0]
                    cnt = 1
                elif nowtime == car[i][0][0]: 
                    nowbit |= (1<<i)
                    cnt += 1 
    if cnt == 0: break
    if cnt == 4:break 
    bit = nowbit
    while bit:
        nextt = bit&-bit
        bit &= (bit-1)
        num = tonum[nextt]
        if nowbit & (1<<block[num]): continue
        ans[ car[num][0][1] ] = nowtime
        car[num].popleft()     
        
    pre = nowtime
    

print(*ans, sep="\n")