Created
May 5, 2020 04:28
-
-
Save inspirit941/9bdb4675ca384ceaf1cbe15c6505e92e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# deque rotate 양수 = 시계방향, 음수 = 반시계방향 | |
from collections import deque | |
import sys | |
from itertools import chain | |
def bfs(maps, y, x): | |
dirs = [(0,1),(0,-1),(1,0),(-1,0)] | |
queue = deque() | |
value = maps[y][x] | |
queue.append((y, x)) | |
visited = set() | |
while queue: | |
y, x = queue.popleft() | |
visited.add((y, x)) | |
for dy, dx in dirs: | |
ny, nx = y+dy, x+dx | |
if nx == len(maps[0]): | |
nx = 0 | |
if nx < 0: | |
nx = len(maps[0]) - 1 | |
if 0 < ny < len(maps) and maps[ny][nx] == value and (ny, nx) not in visited: | |
queue.append((ny, nx)) | |
visited.add((ny, nx)) | |
return visited | |
# 1. 원판 회전시키기 | |
def deque_rotate(maps, p, k): | |
# 배수인 원판만 작업하기 | |
for idx in range(1, len(maps)): | |
if idx % p == 0: | |
maps[idx].rotate(k) | |
return | |
# 2. 원판 숫자 연산하기 | |
def deque_calculate(maps): | |
is_adj = False | |
for y in range(1, len(maps)): | |
for x in range(len(maps[0])): | |
current = maps[y][x] | |
if current == 0: | |
continue | |
# 인접하면서 같은 숫자 찾기 | |
adj = bfs(maps, y, x) | |
# 인접하면서 같은 수가 존재하는 경우 = 좌표가 2개 이상 | |
if len(adj) > 1: | |
# 인접한 수가 있으므로 flag True로 변경 | |
is_adj = True | |
for ny, nx in adj: | |
# 모든 좌표의 숫자 0으로 변경 | |
maps[ny][nx] = 0 | |
# 전체 탐색했는데 인접한 수가 없는 경우 | |
# 모든 수가 전부 0일 경우 avg 계산 불가능 | |
if not is_adj and [i for i in chain(*maps) if i != 0]: | |
avg = sum(chain(*maps)) / len([i for i in chain(*maps) if i != 0]) | |
for y in range(1, len(maps)): | |
for x in range(len(maps[0])): | |
if maps[y][x] != 0: | |
if maps[y][x] > avg: | |
maps[y][x] -=1 | |
elif maps[y][x] < avg: | |
maps[y][x] += 1 | |
n, m, t = map(int, sys.stdin.readline().split()) | |
maps = [[0 for _ in range(m)]] | |
for _ in range(n): | |
arr = deque(map(int,sys.stdin.readline().split())) | |
maps.append(arr) | |
for _ in range(t): | |
p, d, k = map(int,sys.stdin.readline().split()) | |
k = k if d == 0 else -k | |
deque_rotate(maps, p, k) | |
deque_calculate(maps) | |
print(sum(chain(*maps))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment