-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2234.py
More file actions
83 lines (69 loc) ยท 2.6 KB
/
2234.py
File metadata and controls
83 lines (69 loc) ยท 2.6 KB
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
import sys
from collections import defaultdict
input = sys.stdin.readline
# ์ธ๋ก M, ๊ฐ๋ก N์ธ board
N, M = map(int, input().split())
board = [['']*N for _ in range(M)]
for i in range(M) :
inp = list(map(int, input().split()))
for j, spot in enumerate(inp) :
# ๋ฐ์ด๋๋ฆฌ๋ก ์ ์ฅ
bin_spot= bin(spot)[2:]
# 1 -> Ob1 -> ์์ 0์ผ๋ก ์ฑ์ -> 0001
board[i][j] = '0'*(4-len(bin_spot)) + bin_spot
d = [[1,0], [0,1], [-1,0], [0,-1]]
# ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ ๋ฐฐ์ด
visited=[[0]*N for _ in range(M)]
# ๊ฐ๊ฐ ์ํ ๋ฐฉ ๋ฒํธ
room =[[-1]*N for _ in range(M)]
def dfs(y, x) :
# ํ์ฌ ์ขํ์ ๋ฒฝ ์ํ
curr_wall = board[y][x]
room[y][x] = number # ๋ฐฉ ๋ฒํธ ๋ถ์ฌ
visited[y][x] = 1 # ๋ฐฉ๋ฌธ ์ฒ๋ฆฌ
'''
# ์ฒ์์๋ ๊ฐ์ด ์ ์์ ์ฃผ์ ์์ฒ๋ผ ๊ตฌํํ์ง๋ง, ๋์ค์๋ ๋จ์ํํ์ต๋๋ค.
# ์์ชฝ ๋ฒฝ ๋น์ด์์ (0)
if curr_wall[3] == '0' and 0 < x and visited[y][x-1] == 0 :
dfs(y, x-1)
# ๋์ชฝ ๋ฒฝ ๋น์ด์์ (0)
if curr_wall[1] == '0' and x < N-1 and visited[y][x+1] == 0 :
dfs(y, x+1)
# ๋ถ์ชฝ ๋ฒฝ ๋น์ด์์ (0)
if curr_wall[2] == '0' and 0 < y and visited[y-1][x] == 0 :
dfs(y-1, x)
if curr_wall[0] == '0' and y < M-1 and visited[y+1][x] == 0 :
dfs(y+1, x)
'''
for dir in range(4) :
ny, nx = y + d[dir][0], x + d[dir][1]
if curr_wall[dir] =='0' and 0<=ny<M and 0<=nx<N and visited[ny][nx] == 0 :
dfs(ny, nx)
number = 1 # ๋ถ์ฌํ ๋ฐฉ ๋ฒํธ
dic = defaultdict(int) # ๋ฐฉ ๋ฒํธ์ ํด๋นํ๋ ๋ฐฉ ์ count
for i in range(M) :
for j in range(N) :
if visited[i][j] == 0 :
dfs(i, j)
number += 1
dic[room[i][j]] += 1
print(len(dic.keys())) # ๋ฐฉ ์
print(max(dic.values())) # ์ ์ผ ํฐ ๋ฐฉ ํฌ๊ธฐ
tmp = list(dic.values()).sort() # ์ผ์ฐ ๋๋ด๊ธฐ ์ํ ์กฐ๊ฑด
tmp_max = tmp[-1] + tmp[-2]
max_combined_room = 0
for i in range(M) :
for j in range(N) :
curr = room[i][j]
for idx in range(4) :
ny, nx = i + d[idx][0], j + d[idx][1]
if 0<= ny < M and 0 <= nx < N :
next_room= room[ny][nx]
# ๋ง์ฝ ํฉ์น ์ ์๋ ๋ฐฉ์ด๋ฉด
if curr != next_room :
max_combined_room = max( max_combined_room, dic[curr] + dic[next_room])
# ์ผ์ฐ ๋๋ด๊ธฐ ์ํ ์กฐ๊ฑด
if max_combined_room == tmp_max :
print(max_combined_room)
sys.exit()
print(max_combined_room)