Part6.13_완전탐색_깊이,넓이 우선탐색활용_안전영역(BFS)

Eugenius1st·2022년 2월 11일
0

Python_algorithm

목록 보기
58/83

안전영역

선생님 코드

check라는 이차원 리스트 만들어서 탐색하라

import sys
sys.stdin = open("input.txt", "rt")
#sys.setrecursionlimit(10**6) 

dx = [-1,0,1,0]
dy = [0,1,0,-1]

def DFS(x, y, h):
    ch[x][y] = 1
    for i in range(4):
        xx = x+dx[i]
        yy = y+dy[i]
        if 0<=xx<n and 0<=yy<n and ch[xx][yy] ==0 and board[xx][yy]>h:
            DFS(xx,yy,h)


if __name__ == "__main__":
    n = int(input())
    cnt = 0
    res = 0
    board = [list(map(int,input().split())) for _ in range(n)]
    for h in range(100):
        ch = [[0]*n for _ in range(n)]
        cnt = 0
        for i in range(n):
            for j in range(n):
                if ch[i][j] == 0 and board[i][j]>h:
                    cnt+=1
                    DFS(i, j, h)
        res = max(res,cnt)
        if cnt == 0:
            break
    print(res)

백준에서 주의할 것은,
파이썬으로 재귀함수를 돌리면,

sys.setrecursionlimit(10**6)

으로 시간 리밋을 준다,
그래서 저 시간을 넘으면 종료시키는 것이다.
띠리서 백준 사이트 같은 데에서는 저 문장을 꼭 넣어주어야 한다.

profile
최강 프론트엔드 개발자가 되고싶은 안유진 입니다

0개의 댓글