Skip to content

Commit 92613e7

Browse files
committed
연구소 풀이
1 parent ec77958 commit 92613e7

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

BAEKJOON/3Gold/연구소.py

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# 구현, 그래프 이론, 브루트포스 알고리즘, 그래프 탐색, 너비 우선 탐색
2+
# https://www.acmicpc.net/problem/14502
3+
4+
"""
5+
7 7
6+
2 0 0 0 1 1 0
7+
0 0 1 0 1 2 0
8+
0 1 1 0 1 0 0
9+
0 1 0 0 0 0 0
10+
0 0 0 0 0 1 1
11+
0 1 0 0 0 0 0
12+
0 1 0 0 0 0 0
13+
"""
14+
15+
arrow = ((-1,0),(1,0),(0,-1),(0,1))
16+
17+
def find_map(Maps,N,M,target):
18+
pairs = []
19+
for i in range(N):
20+
for j in range(M):
21+
if Maps[i][j] == target:
22+
pairs.append((i,j))
23+
return pairs
24+
25+
def count_virus(Maps,N,M,virus,case):
26+
from collections import deque
27+
visit = [[False for _ in range(M)] for _ in range(N)]
28+
queue = deque(virus)
29+
cnt = 0
30+
for cx, cy in case:
31+
Maps[cx][cy] = 3
32+
33+
while queue:
34+
x,y = queue.popleft()
35+
cnt += 1
36+
for dx,dy in arrow:
37+
nx,ny = x+dx,y+dy
38+
if nx < 0 or nx >= N: continue
39+
if ny < 0 or ny >= M: continue
40+
if Maps[nx][ny] >= 1: continue # 1wall, 2virus, 3tempwall
41+
if visit[nx][ny] is True: continue
42+
visit[nx][ny] = True
43+
queue.append((nx,ny))
44+
45+
for cx, cy in case:
46+
Maps[cx][cy] = 0
47+
48+
return cnt
49+
50+
if __name__ == "__main__":
51+
from itertools import combinations
52+
input = __import__("sys").stdin.readline
53+
54+
N, M = map(int, input().split())
55+
Maps = [
56+
list(map(int, input().split()))
57+
for _ in range(N)
58+
]
59+
60+
virus = find_map(Maps,N,M,2)
61+
empty = find_map(Maps,N,M,0)
62+
total_cnt = N*M
63+
base_wall_cnt = total_cnt-(len(virus)+len(empty))+3
64+
65+
wall_cases = combinations(empty, 3)
66+
answer = 0
67+
for wall_case in wall_cases:
68+
virus_cnt = count_virus(Maps,N,M,virus,wall_case)
69+
# 전체갯수 - virus 갯수 - 벽갯수
70+
alive_cnt = total_cnt-virus_cnt-base_wall_cnt
71+
if answer < alive_cnt:
72+
answer = alive_cnt
73+
74+
print(answer)
75+

0 commit comments

Comments
 (0)