[백준 바킹독] 0x09 - BFS 토마토(7576)
Introduction
https://www.acmicpc.net/problem/1926
어떤 큰 도화지에 그림이 그려져 있을 때, 그 그림의 개수와, 그 그림 중 넓이가 가장 넓은 것의 넓이를 출력하여라. 단, 그림이라는 것은 1로 연결된 것을 한 그림이라고 정의하자. 가로나 세로로 연결된 것은 연결이 된 것이고 대각선으로 연결이 된 것은 떨어진 그림이다. 그림의 넓이란 그림에 포함된 1의 개수이다.
첫째 줄에 도화지의 세로 크기 n(1 ≤ n ≤ 500)과 가로 크기 m(1 ≤ m ≤ 500)이 차례로 주어진다. 두 번째 줄부터 n+1 줄 까지 그림의 정보가 주어진다. (단 그림의 정보는 0과 1이 공백을 두고 주어지며, 0은 색칠이 안된 부분, 1은 색칠이 된 부분을 의미한다)
첫째 줄에는 그림의 개수, 둘째 줄에는 그 중 가장 넓은 그림의 넓이를 출력하여라. 단, 그림이 하나도 없는 경우에는 가장 넓은 그림의 넓이는 0이다.
from collections import deque
n, m = map(int,input().split())
painting = []
for _ in range(n):
painting.append(list(map(int,input().split())))
visited = [[0]*m for _ in range(n)]
def bfs(paint,x,y,n,m):
global visited
count = 1
q = deque([[x,y]])
paint[x][y] = 0
visited[x][y] = 1
while q:
col, row = q.popleft()
# 오른쪽
if row+1<m and paint[col][row+1]==1 and visited[col][row+1]==0:
q.append([col,row+1])
paint[col][row+1] = 0
visited[col][row+1] = 1
count += 1
# 아래
if col+1<n and paint[col+1][row]==1 and visited[col+1][row]==0:
q.append([col+1,row])
paint[col+1][row] = 0
visited[col+1][row] = 1
count += 1
# 왼쪽
if row-1>=0 and paint[col][row-1]==1 and visited[col][row-1]==0:
q.append([col,row-1])
paint[col][row-1] = 0
visited[col][row-1] = 1
count += 1
# 위
if col-1>=0 and paint[col-1][row]==1 and visited[col-1][row]==0:
q.append([col-1,row])
paint[col-1][row] = 0
visited[col-1][row] = 1
count += 1
return count
def search(paint,n,m):
global visited
max_count = 0
paint_count = 0
count = 0
for i in range(n):
for j in range(m):
if paint[i][j] == 1 and visited[i][j] == 0:
paint_count += 1
count = bfs(paint,i,j,n,m)
if max_count < count:
max_count=count
return paint_count, max_count
print("\n".join(map(str,search(painting,n,m))))
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
1167 트리의 지름
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Git 명령어를 사용한 하위 디렉토리 다운로드 Clone 할 로컬 저장소 생성
Introduction
# Fetch the submodule commits into the main repository git remote add submodule_origin git://url/to/submodule/origin git fetch submodule_origin
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Graph
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Introduction
Spring Project
the page for java
가벼운 Base image를 사용
version: 3.0.0a10
WIDTH
version: 3.0.0a10
#include <iostream> #include <thread> #include <chrono> #include <mutex> #include <atomic> #include <string.h>
version: 3.0.0a10
https://cplusplus.com/reference/future/
Multithreading support was introduced in C++11.
how to costom github blog using jekyll
You’ll find this post in your _posts directory. Go ahead and edit it and re-build the site to see your changes. You can rebuild the site in many different wa...