188. 그림
1) 어떤 전략(알고리즘)으로 해결?
2) 코딩 설명
<내 풀이>
- bfs로 접근해서 재귀가 아닌 큐가 있는 동안 돌게 해주었더니 잘 통과가 됐다.
from collections import deque
import sys
sys.setrecursionlimit(10**6)
n,m = map(int, sys.stdin.readline().rstrip().split())
map = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
cnt = 0
rdis = [ 1, -1, 0, 0]
cdis = [0, 0 , 1, -1]
def bfs(q) :
global big
while q :
now = q.popleft()
for i in range(4) :
tmpr = now[0]+rdis[i]
tmpc = now[1]+cdis[i]
if 0<=tmpr<n and \
0<=tmpc<m and \
visited[tmpr][tmpc]==0 and \
map[tmpr][tmpc]==1 :
big+=1
visited[tmpr][tmpc] = 1
q.append((tmpr,tmpc))
return big
maxarea = 0
q=deque()
for i in range(n) :
for j in range(m) :
if(map[i][j]==1 and visited[i][j]==0) :
q.append((i,j))
cnt+=1
big = 1
visited[i][j] = 1
maxarea = max(maxarea, bfs(q))
print(cnt)
print(maxarea)
<내 틀렸던 풀이, 문제점>
메모리초과 및 recursion 에러 (DFS)
- 다들 dfs 로는 통과를 못하고 bfs 로 풀어야 통과가 된대서 수정
import sys
n,m = map(int, sys.stdin.readline().rstrip().split())
map = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
cnt = 0
rdis = [ 1, -1, 0, 0]
cdis = [0, 0 , 1, -1]
def dfs(r,c) :
global visited, big
for i in range(4) :
tmpr = r+rdis[i]
tmpc = c+cdis[i]
if 0<=tmpr<n and \
0<=tmpc<m and \
visited[tmpr][tmpc]==0 and \
map[tmpr][tmpc]==1 :
big+=1
visited[tmpr][tmpc] = 1
dfs(tmpr,tmpc)
maxarea = 0
for i in range(n) :
for j in range(m) :
if(map[i][j]==1 and visited[i][j]==0) :
cnt+=1
big = 1
visited[i][j] = 1
dfs(i,j)
if big > maxarea :
maxarea = big
print(cnt)
print(maxarea)
bfs도 61%에서 메모리 초과;
from collections import deque
import sys
sys.setrecursionlimit(10**6)
n,m = map(int, sys.stdin.readline().rstrip().split())
map = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
cnt = 0
rdis = [ 1, -1, 0, 0]
cdis = [0, 0 , 1, -1]
def bfs(q) :
global big
while q :
now = q.popleft()
for i in range(4) :
tmpr = now[0]+rdis[i]
tmpc = now[1]+cdis[i]
if 0<=tmpr<n and \
0<=tmpc<m and \
visited[tmpr][tmpc]==0 and \
map[tmpr][tmpc]==1 :
big+=1
visited[tmpr][tmpc] = 1
q.append((tmpr,tmpc))
bfs(q)
maxarea = 0
q = deque()
for i in range(n) :
for j in range(m) :
if(map[i][j]==1 and visited[i][j]==0) :
q.append((i,j))
cnt+=1
big = 1
visited[i][j] = 1
bfs(q)
if big > maxarea :
maxarea = big
print(cnt)
print(maxarea)
61%,,
from collections import deque
import sys
sys.setrecursionlimit(10**6)
n,m = map(int, sys.stdin.readline().rstrip().split())
map = [list(map(int,sys.stdin.readline().rstrip().split())) for _ in range(n)]
visited = [[0]*m for _ in range(n)]
cnt = 0
rdis = [ 1, -1, 0, 0]
cdis = [0, 0 , 1, -1]
def bfs(q) :
global big
while q :
now = q.popleft()
for i in range(4) :
tmpr = now[0]+rdis[i]
tmpc = now[1]+cdis[i]
if 0<=tmpr<n and \
0<=tmpc<m and \
visited[tmpr][tmpc]==0 and \
map[tmpr][tmpc]==1 :
big+=1
visited[tmpr][tmpc] = 1
q.append((tmpr,tmpc))
bfs(q)
else : continue
else : return big
return big
maxarea = 0
q=deque()
for i in range(n) :
for j in range(m) :
if(map[i][j]==1 and visited[i][j]==0) :
q.append((i,j))
cnt+=1
big = 1
visited[i][j] = 1
maxarea = max(maxarea, bfs(q))
print(cnt)
print(maxarea)
<반성 점>
<배운 점>