Skip to content

Commit ab4e4a9

Browse files
authored
Create 2.py
1 parent 82e14aa commit ab4e4a9

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

13/2.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
n, m = map(int, input().split())
2+
data = [] # 초기 맵 리스트
3+
temp = [[0] * m for _ in range(n)] # 벽을 설치한 뒤의 맵 리스트
4+
5+
for _ in range(n):
6+
data.append(list(map(int, input().split())))
7+
8+
# 4가지 이동 방향에 대한 리스트
9+
dx = [-1, 0, 1, 0]
10+
dy = [0, 1, 0, -1]
11+
12+
result = 0
13+
14+
# 각 바이러스가 사방으로 퍼짐
15+
def virus(x, y):
16+
for i in range(4):
17+
nx = x + dx[i]
18+
ny = y + dy[i]
19+
# 상, 하, 좌, 우 중에서 바이러스가 퍼질 수 있는 경우
20+
if nx >= 0 and nx < n and ny >= 0 and ny < m:
21+
if temp[nx][ny] == 0:
22+
# 해당 위치에 바이러스 배치하고, 다시 재귀적으로 수행
23+
temp[nx][ny] = 2
24+
virus(nx, ny)
25+
26+
# 현재 맵에서 안전 영역의 크기 계산
27+
def get_score():
28+
score = 0
29+
for i in range(n):
30+
for j in range(m):
31+
if temp[i][j] == 0:
32+
score += 1
33+
return score
34+
35+
# 재귀적으로 울타리를 설치하면서, 안전 영역의 크기 계산
36+
def dfs(count):
37+
global result
38+
# 울타리가 3개 설치된 경우
39+
if count == 3:
40+
for i in range(n):
41+
for j in range(m):
42+
temp[i][j] = data[i][j]
43+
# 각 바이러스의 위치에서 전파 진행
44+
for i in range(n):
45+
for j in range(m):
46+
if temp[i][j] == 2:
47+
virus(i, j)
48+
# 안전 영역의 최대값 계산
49+
result = max(result, get_score())
50+
return
51+
# 빈 공간에 울타리를 설치합니다.
52+
for i in range(n):
53+
for j in range(m):
54+
if data[i][j] == 0:
55+
data[i][j] = 1
56+
count += 1
57+
dfs(count)
58+
data[i][j] = 0
59+
count -= 1
60+
61+
dfs(0)
62+
print(result)

0 commit comments

Comments
 (0)