1. 문제
2. 정답 코드
# @Date : 2024-10-07 17:31:26 # @Author : ecodev # @Link : http://ecodev-blog.vercel.app import sys from collections import deque input = sys.stdin.readline ############ ---- Input Functions ---- ############ def inp(): return(int(input())) def inlt(): return(list(map(int, input().split()))) def insr(): s = input() return(list(s[:len(s) - 1])) def invr(): return(map(int, input().split())) n, q = invr() graph = [inlt() for _ in range(2 ** n)] magic = inlt() # 배열 돌리기 def rotate(graph, l): new_graph = [[0] * 2 ** n for _ in range(2 ** n)] for i in range(0, 2 ** n, 2 ** l): for j in range(0, 2 ** n, 2 ** l): for x in range(2 ** l): for y in range(2 ** l): new_graph[i + y][j + (2 ** l - 1 - x) ] = graph[i + x][j + y] return new_graph path = [(1, 0), (-1, 0), (0, 1), (0, -1)] def melt(graph): now = [] for x in range(2 ** n): for y in range(2 ** n): cnt = 0 if graph[x][y] > 0: for i in range(4): nx = x + path[i][0] ny = y + path[i][1] if 0 <= nx < 2 ** n and 0 <= ny < 2 ** n and graph[nx][ny] > 0: cnt += 1 # 동시성 조심 if cnt < 3: now.append((x, y)) for x, y in now: graph[x][y] -= 1 def BFS(x, y): global ice q = deque() q.append((x, y)) visited[x][y] = True ice += 1 while q: x, y = q.popleft() for i in range(4): nx = x + path[i][0] ny = y + path[i][1] if 0 <= nx < 2 ** n and 0 <= ny < 2 ** n and not visited[nx][ny] and graph[nx][ny] > 0: ice += 1 visited[nx][ny] = True q.append((nx, ny)) for i in range(q): graph = rotate(graph, magic[i]) melt(graph) ans = 0 visited = [[False] * (2 ** n) for _ in range(2 ** n)] # 제곱할 때 곱하기와 혼동하여 연산순서가 바뀔 수 있기 때문에 반드시 괄호를 쓰자. for i in range(2 ** n): for j in range(2 ** n): if graph[i][j] > 0 and not visited[i][j]: ice = 0 # 할 때마다 ice를 새로 비교해주어야 한다. BFS(i, j) ans = max(ans, ice) print(sum(map(sum, graph))) print(ans)
3. 생각한 방향 및 설명
시계 방향으로 90도 회전 | new[j][n - 1 - i] = graph[i][j] |
반시계 방향으로 90도 회전 | new[n - 1 - j][i] = graph[i][j] |
n : 회전시킬 그래프의 크기
i, j : 행, 열
일단 이 로직을 사용하기 전에 너무 복잡하게 코드를 작성했다.
이 부분은 블로그를 참고해서 작성하였다.
그리고 계속 제출해도 틀리는 상황이 발생하였다.
여기서 내가 실수했던 부분은 다음과 같다.
ice = 0
처음에
for i in range(2 ** n): ice = 0 for j in range(2 ** n): if graph[i][j] > 0 and not visited[i][j]: BFS(i, j) ans = max(ans, ice)
이런식으로 바보같이 작성했다.

당연히 각 칸마다 얼음이 있을 수 있기 때문에 이중for문 가장 안쪽에 ice = 0으로 초기화 시켜주어야 한다. 이 부분이 치명적으로 작용했다.
항상 코드를 작성할 때 지금 내가 어떤 부분을 구현하고 있는지를 생각해야할 것.