728x90
https://www.acmicpc.net/problem/2667
# 틀렸습니다 조합 + 한줄
import itertools
n = int(input())
data = [[] for _ in range(n)]
data_empty = [[0 for _ in range(n)] for _ in range(n)]
for i in range(n):
inp = input()
for j in range(n):
data[i].append(int(inp[j]))
if data == data_empty:
print(0)
else:
RESULT = []
for _ in range(4):
p = [0]
pre = [(0,0)]
result = []
group = []
if data[0][0] == 1:
group.append((0,0))
result.append(1)
else:
result.append(0)
for i in range(1,n):
p.append(i)
per = list(itertools.permutations(p,2))
per = list(set(per)-set(pre))
per = sorted(per, key=lambda x:(x[0],-x[1])) + [(i,i)]
cnt = 1
for j in range(len(per)):
if data[per[j][0]][per[j][1]] == 1:
cnt = max(result)
if result[0] == 0:
result[0] = 1
elif (per[j][0]-1, per[j][1]) in group:
result.append(result[group.index((per[j][0]-1, per[j][1]))])
elif (per[j][0], per[j][1]-1) in group:
result.append(result[group.index((per[j][0], per[j][1]-1))])
elif (per[j][0], per[j][1]+1) in group:
result.append(result[group.index((per[j][0], per[j][1]+1))])
elif (per[j][0]+1, per[j][1]) in group:
result.append(result[group.index((per[j][0]+1, per[j][1]))])
else:
cnt += 1
result.append(cnt)
group.append((per[j][0], per[j][1]))
pre += per
RESULT.append(result)
rot_data = [['' for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
rot_data[j][n-1-i] = data[i][j] # 90도 회전 행렬 (세로도 가로처럼)
data = rot_data
for _ in range(4):
group = []
result = []
if data[0][0] == 0:
result.append(0)
cnt = 1
for i in range(n):
for j in range(n):
if data[i][j] == 1:
if result == []:
result.append(1)
elif result[0] == 0:
result[0] = 1
elif (i-1, j) in group:
result.append(result[group.index((i-1, j))])
elif (i, j-1) in group:
result.append(result[group.index((i, j-1))])
elif (i+1, j) in group:
result.append(result[group.index((i+1, j))])
elif (i, j+1) in group:
result.append(result[group.index((i, j+1))])
else:
cnt += 1
result.append(cnt)
group.append((i,j))
cnt = max(result)
RESULT.append(result)
rot_data = [['' for _ in range(n)] for _ in range(n)]
for i in range(n):
for j in range(n):
rot_data[j][n-1-i] = data[i][j] # 90도 회전 행렬 (세로도 가로처럼)
data = rot_data
MIN = max(RESULT[0])
SUM = sum(RESULT[0])
NUM = 0
for i in range(1,4):
if MIN > max(RESULT[i]) and SUM > sum(RESULT[i]):
MIN = max(RESULT[i])
SUM = sum(RESULT[i])
NUM = i
dan = [0]*(MIN)
for i in RESULT[NUM]:
dan[i-1] += 1
dan.sort()
print(MIN)
for i in dan:
print(i)
반응형
'전.py' 카테고리의 다른 글
백준 6603 로또 (0) | 2020.12.12 |
---|---|
백준 1652 누울 자리를 찾아라 (0) | 2020.12.12 |
백준 2605 줄 세우기 (0) | 2020.12.12 |
백준 2606 바이러스 (0) | 2020.12.12 |
백준 2607 비슷한 단어 (0) | 2020.12.12 |