hades

[Baekjoon] 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ” ๋ณธ๋ฌธ

๐Ÿ‘Š PS/Algorithm

[Baekjoon] 1012๋ฒˆ: ์œ ๊ธฐ๋† ๋ฐฐ์ถ”

hades1 2024. 7. 2. 20:07

๐Ÿฅ… ๋ฌธ์ œ

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

 

๐Ÿ” ์„ค๊ณ„

์ง€๋ ์ด๊ฐ€ ๊ฐ€๋กœ์„ธ๋กœ๋กœ ์ธ์ ‘ํ•œ ์˜์—ญ์€ ์ด๋™๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๊ฒƒ์—์„œ BFS๋ฅผ ์ด์šฉํ•ด์„œ ์˜์—ญ์„ ๊ตฌํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ‘Š ํ’€์ด

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;	
int dx[] = {-1,1,0,0};
int dy[] = {0,0,1,-1};
int t, m, n, k, x, y;

void bfs(int start_y, int start_x, vector<vector<int>>& farm){
	queue<pair<int, int>> q;
	farm[start_y][start_x] = 0;
	q.push({start_y, start_x});
	while (!q.empty()){
		int cur_y = q.front().first;
		int cur_x = q.front().second;
		q.pop();
		for (int i=0; i<4; i++){
			int new_y = cur_y + dy[i];
			int new_x = cur_x + dx[i];
			if (!(new_x >= 0 && new_x < m && new_y >= 0 && new_y < n)){
				continue;
			}
			if (farm[new_y][new_x] == 1){
				farm[new_y][new_x] = 0;
				q.push({new_y, new_x});
			}
		}
	}

}

int main() {
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);
	cin >> t;
	for (int q=0; q<t; q++){
		cin >> m >> n >> k;
		vector<vector<int>> farm(n, vector<int>(m));
		int number_of_earthworm = 0;
		for (int w=0; w<k; w++){
			cin >> x >> y;
			farm[y][x] = 1;
		}
		for (int i=0; i<n; i++){
			for (int j=0; j<m; j++){
				if (farm[i][j] == 1){
					number_of_earthworm+=1;
					bfs(i, j, farm);
				}
			}
		}
		cout << number_of_earthworm << "\n";
	}
	return 0;
}

 

๐Ÿ“ ๋ฉ”๋ชจ

ํŠน๋ณ„ํ•œ ์–ด๋ ค์›€ ์—†์ด ํ’€ ์ˆ˜ ์žˆ๋Š” ์ „ํ˜•์ ์ธ BFS ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง€๋Š” ๋ณ€์ˆ˜๋งŒ ์ œ๋Œ€๋กœ ํŒŒ์•…ํ•˜๋ฉด ํฐ ์–ด๋ ค์›€์€ ์—†๋‹ค.