-
Notifications
You must be signed in to change notification settings - Fork 0
/
3085.py
92 lines (69 loc) · 2.2 KB
/
3085.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from sys import stdin
# 모든 칸들을 순회하면서 교환 결과를 비교한다.
# 교환 결과: 무조건 1번의 교환이 발생해야 하므로, 교환이 발생하고 난 뒤에 변경되는 행과 열로부터 구한 최대 사탕 갯수.
# calculate_max()에서 최대 사탕 갯수를 구하는 로직에 오류가 있어서 시간 다소 소요.
table = []
N = int(stdin.readline())
for _ in range(N):
row = list(stdin.readline().strip('\n'))
table.append(row)
answer = 0
counter = {
'Y': 0,
'C': 0,
'Z': 0,
'P': 0,
}
def calculate_max(line):
cur = line[0]
counts = [1]
for i in range(1, N):
candy = line[i]
if cur == candy:
counts[-1] += 1
else:
counts.append(1)
cur = candy
return max(counts)
def init_counter():
for key in counter:
counter[key] = 0
def create_column(n):
column = []
for r in range(N):
column.append(table[r][n])
return column
# 가로 교환
for i in range(N):
for j in range(N):
if 0 <= j <= N-2:
table[i][j], table[i][j+1] = table[i][j+1], table[i][j]
row = table[i]
a = calculate_max(row)
init_counter()
column1 = create_column(j)
b = calculate_max(column1)
init_counter()
column2 = create_column(j+1)
c = calculate_max(column2)
answer = max(answer, a, b, c)
# 딱 1번의 교환만 가정하므로 원복시킨다.
table[i][j], table[i][j+1] = table[i][j+1], table[i][j]
# 세로 교환
for i in range(N):
for j in range(N):
if 0 <= i <= N-2:
table[i][j], table[i+1][j] = table[i+1][j], table[i][j]
row1 = table[i]
a = calculate_max(row1)
init_counter()
row2 = table[i+1]
b = calculate_max(row2)
init_counter()
column = create_column(j)
c = calculate_max(column)
init_counter()
answer = max(answer, a, b, c)
# 딱 1번의 교환만 가정하므로 원복시킨다.
table[i][j], table[i+1][j] = table[i+1][j], table[i][j]
print(answer)