백준
1. Python
정해
참고
import sys
from collections import deque
n,m,t = map(int,input().split())
board = [deque(int(x) for x in input().split()) for _ in range(n)]
for _ in range(t):
x, d, k = map(int, input().split())
result = 0
for i in range(n):
result += sum(board[i])
if (i+1)%x == 0:
if d == 0:
board[i].rotate(k)
else:
board[i].rotate(-k)
if result != 0:
have_to_remove = []
for i in range(n):
for j in range(m-1):
if board[i][j] != 0 and board[i][j+1] != 0 and board[i][j] == board[i][j+1]:
have_to_remove.append((i,j))
have_to_remove.append((i,j+1))
if board[i][0] != 0 and board[i][-1]!=0 and board[i][0] == board[i][-1]:
have_to_remove.append((i,0))
have_to_remove.append((i,m-1))
for j in range(m):
for i in range(n-1):
if board[i][j] != 0 and board[i+1][j] != 0 and board[i][j] == board[i+1][j]:
have_to_remove.append((i,j))
have_to_remove.append((i+1,j))
have_to_remove = list(set(have_to_remove))
for i in range(len(have_to_remove)):
x,y = have_to_remove[i]
board[x][y] = 0
if len(have_to_remove) == 0:
avg_sum = 0
zero_cnt = 0
for i in range(n):
avg_sum += sum(board[i])
zero_cnt += board[i].count(0)
avg = avg_sum / (n*m-zero_cnt)
for i in range(n):
for j in range(m):
if board[i][j] != 0 and board[i][j] > avg:
board[i][j] -= 1
elif board[i][j] !=0 and board[i][j] < avg:
board[i][j] += 1
else:
break
ans = 0
for i in range(n):
ans += sum(board[i])
print(ans)
틀렸습니다.
import sys
input = sys.stdin.readline
n, m, t = map(int, input().split())
disc = [[]]
for _ in range(n):
disc.append(list(map(int, input().split())))
for _ in range(t):
x, d, k = map(int, input().split())
for i in range(x, n + 1, x):
if d == 0:
disc[i] = disc[i][-k:] + disc[i][:-k]
else:
disc[i] = disc[i][k:] + disc[i][:k]
for i in range(1, n + 1):
for j in range(m):
if disc[i][j] != 0:
if i == 1 or j == 0:
if disc[1][j] == disc[2][j]:
disc[1][j], disc[2][j] = 0, 0
if disc[i][0] == disc[i][1]:
disc[i][0], disc[i][1] = 0, 0
if disc[i][0] == disc[i][m - 1]:
disc[i][0], disc[i][m - 1] = 0, 0
elif i == n or j == m - 1:
if disc[n][j] == disc[n - 1][j]:
disc[n][j], disc[n - 1][j] = 0, 0
if disc[i][m - 1] == disc[i][m - 2]:
disc[i][m - 1], disc[i][m - 2] = 0, 0
if disc[i][m - 1] == disc[i][0]:
disc[i][m - 1], disc[i][0] = 0, 0
elif 2 <= i <= n - 1 and 1 <= j <= m - 2:
if disc[i][j] == disc[i][j - 1]:
disc[i][j],disc[i][j - 1] = 0, 0
if disc[i][j] == disc[i][j + 1]:
disc[i][j], disc[i][j + 1] = 0, 0
if disc[i][j] == disc[i - 1][j]:
disc[i][j], disc[i - 1][j] = 0, 0
if disc[i][j] == disc[i + 1][j]:
disc[i][j], disc[i + 1][j] = 0, 0
else:
average = sum(disc[i]) / (m - disc[i].count(0))
if disc[i][j] > average:
disc[i][j] -= 1
elif disc[i][j] < average:
disc[i][j] += 1
print(sum(sum(disc, [])))