포도가게의 개발일지

python 백준 2468번 안전영역 본문

백준

python 백준 2468번 안전영역

grape.store 2021. 8. 11. 20:08
반응형

1. Dfs stack을 이용 - 시간초과

import sys

def dfs(graph, start_node):
    visit = []
    stack = []
    global count, maximum, level
    
    stack.append(start_node)
    while stack:
        node = stack.pop()
        if arr[node] > level:
            if node not in visit:
                if visit_check[node] == False:
                    visit_check[node] = True
                    visit.append(node)
                    stack.extend(graph[node])
    count += 1
    if count > maximum:
        maximum = count

    return count

if __name__ == '__main__':
    visit_check = [False] * 10000
    arr = []
    arr = [9, 9, 9, 9, 9, 9, 9,
            9, 2, 1, 2, 1, 2, 9,
            9, 1, 8, 7, 8, 1, 9,
            9, 2, 7, 9, 7, 2, 9,
            9, 1, 8, 7, 8, 1, 9,
            9, 2, 1, 2, 1, 2, 9,
            9, 9, 9, 9, 9, 9, 9]
    a_app = arr.append
    num = int(sys.stdin.readline())
    # for ins in range(num):
    #     for zzz in map(int,sys.stdin.readline().split()):
    #         a_app(zzz)
    check = dict()
    level = 0
    count = 0
    maximum = 0
    for x in range(len(arr)):
        tmp = []
        append = tmp.append
        if x-1 >= 0:
            if x>0 and x%num != 0: 
                append(x-1) 
        if(x+1)%num != 0:
            append(x+1)
        if x-num > 0:
            append(x-num)
        if x+num < len(arr):
            append(x+num)
        check[x] = tmp
        
        
    #print(check)
    # for del_left in range(0,len(arr),num):
    #     check[del_left].pop(0)
    # for del_right in range(num-1,len(arr),num):
    #     check[del_right].pop(1)
    
    while level < max(arr):
        count = 0
        visit_check = [False] * num * num
        for next in range(num*num):
            if visit_check[next] == False:
                if arr[next] > level:
                    dfs(check, next)
        level += 1
    print(maximum)

2. dfs 재귀를 이용한 방법

import sys 
sys.setrecursionlimit(10000) 

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

def dfs(x, y, h): 
    for i in range(4): 
        nx = x + dx[i] 
        ny = y + dy[i] 
        if(0 <= nx < N) and (0 <= ny < N): 
            if arr[nx][ny] > h and done[nx][ny] == 0: 
                done[nx][ny] = 1 
                dfs(nx, ny, h) 

N = int(sys.stdin.readline())
arr = []

for insert_arr in range(N):
    temp_arr = []
    for temp in map(int, sys.stdin.readline().split()):
        temp_arr.append(temp)
    arr.append(temp_arr)

ans = 0 

### increase water ###
for k in range(max(map(max, arr))): 
    cnt = 0 
    done = [[0]*N for _ in range(N)]
    for i in range(N): 
        for j in range(N): 
            if arr[i][j] > k and done[i][j] == 0: 
                done[i][j] = 1 
                cnt += 1 
                dfs(i, j, k) 
                ans = max(ans, cnt) 
print(ans)

'백준' 카테고리의 다른 글

python 백준 2805번 나무자르기  (0) 2021.08.15
python 백준 1655번 수찾기(이분탐색)  (0) 2021.08.15
백준 10971번 외판원의 순회 2  (0) 2021.08.10
백준 03일차  (0) 2021.08.09
백준 02일차  (0) 2021.08.07
Comments