Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- DP
- CVE
- ๋งต
- dfs
- ๋ฌธ์์ด
- ๋์ ํฉ
- thymeleaf
- dynamic debugging
- GCP
- Reversing
- c++
- ์ฌ๊ท
- ๋ถํ ์ ๋ณต
- Spring
- ๊ทธ๋ฆฌ๋
- BFS
- ๋ฐ์ดํฌ์คํธ๋ผ
- java
- web
- ์คํ
- ์ต๋จ ๊ฒฝ๋ก
- ๋ฐฑํธ๋ํน
- ์ด๋ถ ํ์
- ์๋ฎฌ๋ ์ด์
- ์ฐ์ ์์ ํ
- ์์ ์ ๋ ฌ
- error
- OS
- ๊ตฌํ
- JPA
Archives
- Today
- Total
hades
[Baekjoon] 1012๋ฒ: ์ ๊ธฐ๋ ๋ฐฐ์ถ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
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 ๋ฌธ์ ์ด๋ค. ๋ฌธ์ ์์ ์ฃผ์ด์ง๋ ๋ณ์๋ง ์ ๋๋ก ํ์ ํ๋ฉด ํฐ ์ด๋ ค์์ ์๋ค.
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 1260๋ฒ: DFS์ BFS (0) | 2024.07.03 |
---|---|
[Baekjoon] 1074๋ฒ: Z (0) | 2024.07.02 |
[Baekjoon] 1003๋ฒ: ํผ๋ณด๋์น ํฉ (0) | 2024.07.02 |
[Baekjoon] 1920๋ฒ: ์ ์ฐพ๊ธฐ (0) | 2024.07.01 |
[Baekjoon] 1463๋ฒ: 1๋ก ๋ง๋ค๊ธฐ (0) | 2024.07.01 |