[백준] C++ 1012번:유기농배추

2025. 12. 14. 14:44·알고리즘
#include <bits/stdc++.h>
using namespace std;

int dx[4] = {1 , 0 , -1 , 0};
int dy[4] = {0, -1, 0, 1};

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    for (int r = 0; r < t; r++) {
        int cnt = 0;
        queue<pair<int,int>> q;
        int y, x; // x: 가로 , y: 세로
        cin >> x >> y;
        vector<vector<int>> v(y, vector<int>(x, 0));
        vector<vector<int>> vis(y, vector<int>(x, 0));

        int k;
        cin >> k;
        for (int i = 0; i < k; i++) {
            int yy;
            int xx;
            cin >> xx >> yy;
            v[yy][xx] = 1;
        }

        for (int yy = 0; yy < y; yy++) {
            for (int xx = 0; xx < x; xx++) {
                if (vis[yy][xx] == 0 && v[yy][xx] == 1) {
                    cnt++;
                    vis[yy][xx] = 1;
                    q.push({yy, xx});

                    while(!q.empty()) {
                        auto cur = q.front();
                        q.pop();
                        for (int h = 0; h < 4; h++) {
                            int nx = cur.second + dx[h];
                            int ny = cur.first + dy[h];
                            if (nx > -1 && nx < x && ny > -1 && ny < y && vis[ny][nx] == 0 && v[ny][nx] == 1) {
                                q.push({ny, nx});
                                vis[ny][nx] = 1;
                            }
                        }
                    }
                }
            }
        }
        cout << cnt << "\n";
   
    }

    return 0;
}

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

[백준] C++ 2583번:영역 구하기!  (0) 2025.12.15
[백준] C++ 5427번: 불!  (0) 2025.12.15
[백준] C++ 7562번:나이트의 이동!  (0) 2025.12.15
[백준] C++ 7569번:토마토!  (0) 2025.12.14
[백준] C++ 10026번:적록색약  (0) 2025.12.14
'알고리즘' 카테고리의 다른 글
  • [백준] C++ 5427번: 불!
  • [백준] C++ 7562번:나이트의 이동!
  • [백준] C++ 7569번:토마토!
  • [백준] C++ 10026번:적록색약
Deepmind_딥마인드
Deepmind_딥마인드
깊게 사고하자
  • Deepmind_딥마인드
    딥마인드
    Deepmind_딥마인드
  • 전체
    오늘
    어제
    • 분류 전체보기 (19)
      • 알고리즘 (11)
      • 메모 (0)
      • C언어 (0)
      • C언어 동아리 (0)
      • Ubuntu (0)
      • Ollama (1)
      • AI (4)
      • 정보공유 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    먼지
    ubuntu server 24.04.4 LTS
    백준
    배터리 광탈
    c언어
    에어팟 프로
    Ai
    소셜딜레마 #넷플릭스 #다큐멘터리 #넷플릭스 영화추천
    만다라트 #오타니 #목표설정 #무료템플릿
    냄새
    DBeaver
    Claude
    에어팟
    호환
    스크래치
    노이즈캔슬링
    ollama
    렉
    단차
    OpenAI
    agent
    Air Pods Pro
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.5
Deepmind_딥마인드
[백준] C++ 1012번:유기농배추
상단으로

티스토리툴바