백준
1. 시뮬레이션
정해
참고
import sys
input = sys.stdin.readline
def dir(d):
if d == 1: return 2
elif d == 2: return 1
elif d == 3: return 4
else: return 3
r, c, m = map(int, input().split())
a = [[0]*c for _ in range(r)]
shark = []
for _ in range(m):
x, y, s, d, z = map(int, input().split())
a[x-1][y-1] = [s, d, z]
shark.append([x-1, y-1])
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
ans, rx, ry = 0, -1, -1
for j in range(c):
for i in range(r):
if a[i][j]:
ans += a[i][j][2]
a[i][j] = 0
rx, ry = i, j
break
length = len(shark)
temp = [[0]*c for _ in range(r)]
keep = []
for i in range(length):
x, y = shark[i]
if x == rx and y == ry:
continue
s, d, z = a[x][y][0], a[x][y][1], a[x][y][2]
if d == 1 or d == 2:
nx, ny = x + s * dx[d-1], y
if not 0 <= nx < r:
temp_s = s
if d == 1:
s -= x
x = 0
else:
s -= r-1-x
x = r-1
d = dir(d)
f, g = s // (r-1), s % (r-1)
if f % 2 == 0:
if x == 0:
nx = g
else:
nx = r-1-g
else:
if x == 0:
nx = r-1-g
else:
nx = g
d = dir(d)
s = temp_s
else:
nx, ny = x, y + s * dy[d-1]
if not 0 <= ny < c:
temp_s = s
if d == 3:
s -= c-1-y
y = c-1
else:
s -= y
y = 0
d = dir(d)
f, g = s // (c-1), s % (c-1)
if f % 2 == 0:
if y == 0:
ny = g
else:
ny = c-1-g
else:
if y == 0:
ny = c-1-g
else:
ny = g
d = dir(d)
s = temp_s
if temp[nx][ny]:
if z > temp[nx][ny][2]:
temp[nx][ny] = [s, d, z]
else:
temp[nx][ny] = [s, d, z]
keep.append([nx, ny])
a = temp
shark = keep
print(ans)
틀린 코드
import sys
import copy
input = sys.stdin.readline
r, c, m = map(int, input().split())
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
board = [[[] for _ in range(c + 1)] for _ in range(r + 1)]
shark = []
for _ in range(m):
a, b, s, d, z = map(int, input().split())
shark.append([a, b, s, d - 1, z])
board[a][b].append([[a, b, s, d, z]])
def dir(d):
if d == 1: return 2
elif d == 2: return 1
elif d == 3: return 4
else: return 3
count = 0
for i in range(1, c + 1):
near = []
min_distance = sys.maxsize
if shark:
for a, b, s, d, z in shark:
if b == i:
now = abs(a - 0)
if now < min_distance:
min_distance = now
near.append((a, b, s, d, z))
a, b, s, d, z = near.pop()
shark.remove((a, b, s, d, z))
board[a][b].clear()
count += 1
else:
break
keep = []
while shark:
a, b, s, d, z = shark.pop()
nx = a + (dx[d] * s)
ny = b + (dy[d] * s)
if (1 > nx or nx > r) or (1 > ny or ny > c):
nd = dir(d)
else:
nd = d
nx = (a + (dx[d] * s)) % r
ny = (b + (dy[d] * s)) % c
keep.append((nx, ny, s, nd, z))
board[a][b].remove((a, b, s, d, z))
board[nx][ny].append((nx, ny, s, nd, z))
shark = copy.deepcopy(keep)
for k in range(r):
for j in range(c):
if len(board[k][j]) >= 2:
biggest = []
for a, b, s, d, z in board[k][j]:
if biggest:
if biggest[-1][-1] < z:
biggest.pop()
biggest.append((a, b, s, d, z))
board[k][j].clear()
board[k][j].append((a, b, s, d, z))
print(count)