boj1202 유기농배추

17700 단어 코테코테

문제 : 유기농배추

맞은 코드

import sys
from collections import deque

def bfs(graph, x,y, row, col):
    q = deque()
    q.append([x, y])
    graph[x][y] = 0

    while q:
        x, y = q.popleft()
        
        for i in range(4):
            tx = x + dx[i]
            ty = y + dy[i]
            if tx < 0 or tx > row-1 or ty < 0 or ty > col-1 or graph[tx][ty] == 0:
                continue
            else:
                q.append([tx ,ty])
                graph[tx][ty] = 0 # visit
                



if __name__ == "__main__":
    input = sys.stdin.readline
    dx = [0, 0, 1, -1]
    dy = [1, -1, 0, 0 ]

    tc = int(input())

    for _ in range(tc):
        answer = 0
        col, row, n = map(int, input().split()) # 가로길이, 세로길이, 지렁이 갯수
        # print(col, row, n)
        graph = [[0 for _ in range(col)] for _ in range(row)]
        for _ in range(n):
            i, j = map(int, input().split())
            graph[j][i] = 1
        # print(graph)

        for i in range(row):
            for j in range(col):
                if graph[i][j] == 1:
                    bfs(graph, i, j, row, col)
                    answer += 1
        print(answer)
        del graph

런타임에러 코드

import sys

def bfs(graph, x,y, row, col):
    if x < 0 or x > row-1 or y < 0 or y > col-1:
        return False
    
    if graph[x][y] == 1:
        graph[x][y] = 0 # visit
        bfs(graph, x-1, y,row, col)
        bfs(graph, x+1, y,row, col)
        bfs(graph, x, y-1,row, col)
        bfs(graph, x, y+1,row, col)
        return True
    return False



if __name__ == "__main__":
    input = sys.stdin.readline

    tc = int(input())

    for _ in range(tc):
        answer = 0
        col, row, n = map(int, input().split()) # 가로길이, 세로길이, 지렁이 갯수
        # print(col, row, n)
        graph = [[0 for _ in range(col)] for _ in range(row)]
        for _ in range(n):
            i, j = map(int, input().split())
            graph[j][i] = 1
        # print(graph)

        for i in range(row):
            for j in range(col):
                    if bfs(graph, i, j, row, col) == True:
                        answer += 1
        print(answer)
        del graph

런타임 원인 : boj의 최대 재귀 허용 크기인 1000보다 많이 재귀를 돔
두번째 줄에 sys.setrecursionlimit(10**6) 추가하믄 맞았습니다.

좋은 웹페이지 즐겨찾기