Algorithm with Python

김소은·2025년 6월 3일

블록 게임

n = int(input())
directions = input() # String, N==n
scores = list(map(int, input().split())) # List of Strings, N==n

D = {
	'R': (1, 0), 
	'L': (-1, 0), 
	'U': (0, 1), 
	'D': (0, -1)
}

stack = [(0, 0, 1)]
loc_info = {(0,0)} # info about locations of blocks
cur_sum = 1

for i in range(n):
	dx, dy = D[directions[i]] # get delta x & delta y
	
	x, y, _ = stack[-1] # get last location info
	nx, ny = x + dx, y + dy # create new location info
    
	# if there is same location
	# pop until I fiund it
	while (nx, ny) in loc_info:
		px, py, pscore = stack.pop()
		loc_info.remove((px, py))
		cur_sum -= pscore
        
	# push new one
	stack.append((nx, ny, scores[i]))
	loc_info.add((nx, ny))
	cur_sum += scores[i]

print(cur_sum)
profile
개발자

0개의 댓글