반응형
250x250
Notice
Recent Posts
Recent Comments
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- DP
- dfs
- Learning
- React
- 알고리즘
- pytorch
- ssafy 7기
- 웹 표준 사이트 만들기
- 삼성 청년 SW 아카데미
- SSAFY
- 코딩교육
- 백준
- 삼성청년sw아카데미
- 프로그래머스
- DenseNet
- 전이학습
- ssafy 7기 교수님
- git
- 싸피 7기 입학식
- SSAFY 8기
- 백준7576 bfs
- SWEA
- 유니온 파인드
- bfs
- SSAFYcial
- 코딩 교육
- SSAFY 입학식
- ssafy 7기 합격
- 프로그래머스 고득점 kit
- 이코테
Archives
- Today
- Total
개미의 개열시미 프로그래밍
[알고리즘] 백준14502 연구소 - 파이썬 본문
728x90
반응형
https://www.acmicpc.net/problem/14502
14502번: 연구소
인체에 치명적인 바이러스를 연구하던 연구소에서 바이러스가 유출되었다. 다행히 바이러스는 아직 퍼지지 않았고, 바이러스의 확산을 막기 위해서 연구소에 벽을 세우려고 한다. 연구소는 크
www.acmicpc.net
[풀이 코드]
import copy
from sys import stdin
from collections import deque
n ,m = map(int, input().split())
graph = []
dy = [-1, 1, 0, 0]
dx = [0, 0, -1, 1]
max_result = 0
queue = deque()
for _ in range(n):
graph.append(list(map(int, input().split())))
def bfs(graph):
global max_result
while queue:
y, x = queue.popleft()
for l in range(4):
ny = dy[l] + y
nx = dx[l] + x
if 0 <= ny < n and 0 <= nx < m and graph[ny][nx] == 0:
queue.append((ny, nx))
graph[ny][nx] = 2
zero_cnt = 0
for i in range(n):
for j in range(m):
if graph[i][j] == 0:
zero_cnt += 1
max_result = max(max_result, zero_cnt)
def wallSetting(cnt):
if cnt == 3:
for i in range(n):
for j in range(m):
if graph[i][j] == 2:
queue.append((i, j))
graph_copy = copy.deepcopy(graph)
bfs(graph_copy)
return
for i in range(n):
for j in range(m):
if graph[i][j] == 0:
graph[i][j] = 1
wallSetting(cnt+1)
graph[i][j] = 0
wallSetting(0)
print(max_result)
- 벽 세 개를 재귀를 통해 설치하는 부분은 도저히 해결하지 못해 다른 분의 블로그를 참고했다. 재귀를 하는 방법 말고 브루트 포스로 구현하는 법도 있는 것 같다.
- 벽을 설치하는 것 외에는 별로 크게 어려움 부분은 없었다.
728x90
반응형
'알고리즘 > DFS, BFS, 백트래킹' 카테고리의 다른 글
[알고리즘] 백준13913 숨바꼭질4 - 파이썬 (0) | 2021.12.04 |
---|---|
[알고리즘] 백준13549 숨바꼭질3 - 파이썬 (0) | 2021.11.30 |
[알고리즘] 백준5547 일루미네이션 - 파이썬 (1) | 2021.11.29 |
[알고리즘] 백준16918 봄버맨 - 파이썬 (0) | 2021.11.28 |
[알고리즘] 백준1325 효율적인 해킹 - 파이썬 (0) | 2021.11.26 |
Comments