본문 바로가기
Programming

백준_1012_유기농 배추_dfs_bfs_파이썬

by WelcomeBro 2023. 8. 5.
반응형

문제 링크

 

1012번: 유기농 배추

차세대 영농인 한나는 강원도 고랭지에서 유기농 배추를 재배하기로 하였다. 농약을 쓰지 않고 배추를 재배하려면 배추를 해충으로부터 보호하는 것이 중요하기 때문에, 한나는 해충 방지에 

www.acmicpc.net

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
def bfs(row,col):
    score = 0
    if map1[row][col] ==1:
        score+=1
        map1[row][col] = 0
        now_point = [(row,col)]
        while len(now_point)!= 0:
            now_row, now_col = now_point.pop(0)
            for i in range(4):
                now_x = now_row+dx[i]
                now_y = now_col+dy[i]
                if -1<now_x<row_num and -1<now_y<col_num:
                    if map1[now_x][now_y] == 1:
                        score+=1
                        map1[now_x][now_y] = 0
                        now_point.append((now_x,now_y))
    return score 
 
def dfs(row,col):
    global score
    if map1[row][col] ==1:
        score +=1
        map1[row][col] = 0
        for i in range(4):
            now_x = row+dx[i]
            now_y = col+dy[i]
            if -1<now_x<row_num and -1<now_y<col_num:
                dfs(now_x,now_y)
    return score
 
dx = [1,-1,0,0]
dy = [0,0,1,-1]
 
test_case = int(input())
for test in range(test_case):
    score = 0
    ans = 0
    row_num, col_num, cab_num = map(int,input().split())
    map1 = [[0 for _ in range(col_num)] for _ in range(row_num)]
    for i in range(cab_num):
        row,col = map(int,input().split())
        map1[row][col] = 1
    for i in range(row_num):
        for k in range(col_num):
            now_score = bfs(i,k)
            if now_score != 0:
                ans+=1
                score = 0
    print(ans)
cs

 

 

Be positive!

Be rich!

Live your life!

반응형

'Programming' 카테고리의 다른 글

강화학습_5_0809  (0) 2023.08.09
강화학습_4_0808  (0) 2023.08.08
백준_2667_단지번호 붙이기_dfs_bfs_파이썬  (0) 2023.08.04
백준_1260_DFS와 BFS_0803  (0) 2023.08.03
강화학습_3_0802  (0) 2023.08.02