Paste Search Dynamic
Recent pastes
setrecursionlimit
  1. import sys
  2. sys.setrecursionlimit(10 ** 5)
  3. dir = [(1, 0), (0, 1), (-1, 0), (0, -1)]
  4.  
  5. n, q = map(int, input().split())
  6. n = 2 ** n
  7. ice = [list(map(int, input().split())) for _ in range(n)]
  8.  
  9. for L in list(map(int, input().split())):
  10.     # 회전
  11.     k = 2 ** L
  12.     for x in range(0, n, k):
  13.         for y in range(0, n, k):
  14.             tmp = [ice[i][y:y + k] for i in range(x, x + k)]
  15.             for i in range(k):
  16.                 for j in range(k):
  17.                     ice[x + j][y + k - 1 - i] = tmp[i][j]
  18.  
  19.     # 인접한 얼음 카운팅
  20.     cnt = {}
  21.     for x in range(0, n):
  22.         for y in range(0, n):
  23.             count=0
  24.             if ice[x][y]==0:
  25.                 continue
  26.             for d in dir:
  27.                 nx, ny = x + d[0], y + d[1]
  28.                 if 0 <= nx < n and 0 <= ny < n and ice[nx][ny]>0:
  29.                     count=count+1
  30.             if count < 3:
  31.                 cnt[(x,y)]=1
  32.     # 얼음 제거
  33.     for pair in cnt:
  34.         ice[pair[0]][pair[1]] -= 1
  35.  
  36. print(ice)
  37. # 남아있는 얼음의 합
  38. print(sum(sum(i) for i in ice))
  39.  
  40. # (x,y)가 속한 덩어리의 크기
  41. def dfs(x, y):
  42.     ret = 1
  43.     ice[x][y] = 0
  44.     for d in dir:
  45.         nx, ny = x + d[0], y + d[1]
  46.         if 0 <= nx < n and 0 <= ny < n and ice[nx][ny]:
  47.             ret += dfs(nx, ny)
  48.     return ret
  49.  
  50. # 제일 큰 덩어리
  51. ans = 0
  52. for x in range(n):
  53.     for y in range(n):
  54.         if ice[x][y] > 0:
  55.             ans = max(ans, dfs(x, y))
  56. print(ans)
Parsed in 0.007 seconds