본문 바로가기
알고리즘 공부

백준 1012 유기농배추 [BFS]

by kjwkjw 2020. 5. 16.

https://www.acmicpc.net/problem/1012

 

1012번: 유기농 배추

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

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;

int arr[51][51];
int chk[51][51];
int cnt;
int n,m,t;

int bfs(int a, int b)
{
    queue< pair<int,int> >q;
    q.push(make_pair(a,b));
    while(!q.empty())
    {
        int y = q.front().first;
        int x = q.front().second;
        q.pop();
        
        if(y+1<n&&chk[y+1][x]==0&&arr[y+1][x]==1)
        {
            chk[y+1][x] = cnt;
            q.push(make_pair(y+1,x));
        }
        if(y-1>=0&&chk[y-1][x]==0&&arr[y-1][x]==1)
        {
            chk[y-1][x] = cnt;
            q.push(make_pair(y-1,x));
        }
        if(x+1<m&&chk[y][x+1]==0&&arr[y][x+1]==1)
        {
            chk[y][x+1] = cnt;
            q.push(make_pair(y,x+1));
        }
        if(x-1>=0&&chk[y][x-1]==0&&arr[y][x-1]==1)
        {
            chk[y][x-1] = cnt;
            q.push(make_pair(y,x-1));
        }
    }
    return 0;
}

int main(int argc, char** argv)
{
    ios::sync_with_stdio(false); 
    cin.tie(NULL); 
    cout.tie(NULL);
    int k,a,b;
    cin>>t;
    for (int i = 0; i < t; ++i)
    {
        cin>>m>>n>>k;
        cnt = 0;
        for (int j = 0; j < k; ++j)
        {
            cin>>a>>b;
            arr[b][a] = 1;
        }
        for (int j = 0; j < n; ++j)
        {
            for (int l = 0; l < m; ++l)
            {
                if(arr[j][l]==1&&chk[j][l]==0)
                {
                    cnt++;
                    chk[j][l] = cnt;
                    bfs(j,l);
                }
            }
        }
        for (int j = 0; j < n; ++j)
        {
            for (int l = 0; l < m; ++l)
            {
                arr[j][l] = 0;
                chk[j][l] = 0;
            }
        }
        cout<<cnt<<"\n";
    }

}  

'알고리즘 공부' 카테고리의 다른 글

백준 1026 보물[sort]  (0) 2020.05.21
백준 1018 체스판 다시 칠하기 [Brute Force]  (0) 2020.05.21
백준 1011 Fly me to the Alpha Centauri [Greedy]  (0) 2020.05.16
백준 1009 분산처리  (0) 2020.05.16
백준 1008 A/B  (0) 2020.05.16

댓글