Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 락
- Spring Security
- 스레드
- Spring
- 백준 2529
- 프로세스
- 후위 표기식
- 스프링
- 백준
- 백준 15686
- 백준 14499
- 운영체제
- 파이썬
- 자바스크립트
- 백준 파이썬
- CI/CD
- 9465 스티커
- 프로그래머스
- 백트래킹
- 도커
- 백준 10819
- 네이버
- docker
- 백준 1120
- 코테
- 증가하는부분수열
- 다익스트라
- 코딩테스트
- CS
- 백준 2302
Archives
- Today
- Total
개발
[백준] 14502 연구소(파이썬) 본문
벽의 개수가 3개보다 적으면 벽을 세우고 3일때 바이러스 전파 후 0의 개수를 센다.
마지막으로 벽을 세운곳의 벽을 없애고 다음 자리에 벽을 세우고 계산한다.
전파와 0의 개수는 bfs 함수로 벽을 세우는건 재귀를 통해서 구현했는데
pypy로는 통과하지만 python으로 제출하는 경우 시간초과로 통과하지 못함
import sys
from collections import deque
import copy
input = sys.stdin.readline
n, m = map(int, input().split())
graph = [list(map(int,input().split())) for _ in range(n)]
virus = []
dx = [-1,1,0,0]
dy = [0,0,-1,1]
for i in range(n):
for j in range(m):
if graph[i][j] == 2:
virus.append((i, j))
def bfs(board):
temp = copy.deepcopy(board)
res = 0
dq = deque([])
for i in virus:
dq.append(i)
while dq:
x, y = dq.popleft()
for i in range(4):
nx = x + dx[i]
ny = y + dy[i]
if 0 <= nx < n and 0 <= ny < m:
if temp[nx][ny] == 1:
continue
if temp[nx][ny] == 0:
temp[nx][ny] = 2
dq.append((nx, ny))
for i in range(n):
for j in range(m):
if temp[i][j] == 0:
res += 1
return res
def recursion(level):
count = 0
if level == 3:
return bfs(graph)
for i in range(n):
for j in range(m):
if graph[i][j] == 0:
graph[i][j] = 1
count = max(count, recursion(level+1))
graph[i][j] = 0
return count
print(recursion(0))'알고리즘 > 문제' 카테고리의 다른 글
| [백준] 11779 최소비용 구하기2 (파이썬) (0) | 2022.07.19 |
|---|---|
| [백준] 1753 최단거리(파이썬) (0) | 2022.07.18 |
| [백준] 1918 후위 표기식(파이썬) (0) | 2022.07.16 |
| [백준] 16928 뱀과 사다리 게임 (0) | 2022.07.06 |
| [프로그래머스] [1차] 비밀지도 (자바스크립트) (0) | 2022.07.01 |