1780번 – 종이의 개수

문제

https://www.acmicpc.net/problem/1780

풀이

코드

n = int(input())
B = [list(map(int, input().split())) for _ in range(n)]
def answer():
  global board
  counts = [0, 0, 0]
  boards = [B]
  while(len(boards) > 0):
    board = boards.pop()
    num, result = 모두같은숫자인지체크(board)
    if result is True: counts[num] += 1
    elif (len(board) >= 3): boards.extend(종이자르기(board))
  print(counts[-1])
  print(counts[0])
  print(counts[1])

def 종이자르기(board):
  s1, s2 = (len(board)//3), (len(board)//3 * 2)
  lines = [board[0:s1], board[s1:s2], board[s2:]]
  boards = []
  for line in lines:
    b1, b2, b3 = [], [], []
    for i in range(len(line)):
      b1.append(line[i][0:s1])
      b2.append(line[i][s1:s2])
      b3.append(line[i][s2:])
    boards.extend([b1, b2, b3])
  return boards
        
def 모두같은숫자인지체크(board):
  num = board[0][0]
  for y in range(len(board)):
    for x in range(len(board[0])):
      if board[y][x] != num:
        return (num, False)
  return (num, True)
        
answer()

Leave a Reply

Your email address will not be published.