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
- dynamic debugging
- ์ฐ์ ์์ ํ
- error
- GCP
- BFS
- java
- ๊ทธ๋ฆฌ๋
- ์ต๋จ ๊ฒฝ๋ก
- DP
- ์๋ฎฌ๋ ์ด์
- ๋ฐฑํธ๋ํน
- CVE
- Spring
- JPA
- web
- ๊ตฌํ
- ๋์ ํฉ
- ๋ฌธ์์ด
- Reversing
- ๋ถํ ์ ๋ณต
- c++
- thymeleaf
- ๋งต
- ์คํ
- OS
- ๋ฐ์ดํฌ์คํธ๋ผ
- ์์ ์ ๋ ฌ
- ์ด๋ถ ํ์
- dfs
- ์ฌ๊ท
Archives
- Today
- Total
hades
[Baekjoon] 10026๋ฒ: ์ ๋ก์์ฝ ๋ณธ๋ฌธ
๐ฅ ๋ฌธ์
https://www.acmicpc.net/problem/10026
๐ ์ค๊ณ
์ํ์ข์ฐ๋ก ์ธ์ ํ ์์ญ ์ค ๊ฐ์ ์์ ๊ฐ์ง๋ค๋ฉด ํ ๊ตฌ์ญ์ผ๋ก ์ธ์ํ๋ค๋ ๊ฒ์์ BFS์์ ์์์ฐจ๋ฆด ์ ์๋ค.
์ ๋ก์์ฝ์ธ ์ฌ๋๊ณผ ์ ๋ก์์ฝ์ด ์๋ ์ฌ๋์ ๊ฒฝ์ฐ๋ฅผ ๋๋์ด์ BFS๋ฅผ ์ํํ๋ค. ์ ๋ก์์ฝ์ด ์๋๋ผ๋ฉด, BFS๊ฐ ์์๋๋ ์ง์ ์ ์๊น๊ณผ ๊ฐ์์ผ ํ ๊ตฌ์ญ์ ํฌํจ๋๊ณ , ์ ๋ก์์ฝ์ด๋ผ๋ฉด ์์๋๋ ์ง์ ์ ์๊น์ด ๋นจ๊ฐ์ ๋๋ ์ด๋ก์์ด๋ฉด, ์ด์ํ ์ง์ ์ ์๊น๋ ๋นจ๊ฐ์ ๋๋ ์ด๋ก์์ด๋ฉด ํ ๊ตฌ์ญ์ ํฌํจ๋๊ณ , ํ๋์์๋ฉด ์ด์ํ ์ง์ ์ ์๊น๋ ํ๋์์ด์ด์ผ ํ๋ค.
๐ ํ์ด
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int n;
vector<vector<char>> painting1(100, vector<char>(100));
vector<vector<char>> painting2(100, vector<char>(100));
int a = 0, b = 0;
int dx[] = { -1,1,0,0 };
int dy[] = { 0,0,-1,1 };
void bfs1(int start_x, int start_y) {
char color = painting1[start_x][start_y];
painting1[start_x][start_y] = 'X';
queue<pair<int, int>> q;
q.push({ start_x, start_y });
while (!q.empty()) {
int cur_x = q.front().first;
int cur_y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int new_x = cur_x + dx[i];
int new_y = cur_y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < n)) {
continue;
}
if (painting1[new_x][new_y] == color) {
painting1[new_x][new_y] = 'X';
q.push({ new_x, new_y });
}
}
}
}
void bfs2(int start_x, int start_y) {
char color = painting2[start_x][start_y];
painting2[start_x][start_y] = 'X';
queue<pair<int, int>> q;
q.push({ start_x, start_y });
while (!q.empty()) {
int cur_x = q.front().first;
int cur_y = q.front().second;
q.pop();
for (int i = 0; i < 4; i++) {
int new_x = cur_x + dx[i];
int new_y = cur_y + dy[i];
if (!(new_x >= 0 && new_x < n && new_y >= 0 && new_y < n)) {
continue;
}
if (((color == 'R' || color == 'G') && (painting2[new_x][new_y] == 'R' || painting2[new_x][new_y] == 'G')) || (color == 'B' && painting2[new_x][new_y] == 'B')) {
painting2[new_x][new_y] = 'X';
q.push({ new_x, new_y });
}
}
}
}
int main(void)
{
cin >> n;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> painting1[i][j];
painting2[i][j] = painting1[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (painting1[i][j] != 'X') {
a += 1;
bfs1(i, j);
}
if (painting2[i][j] != 'X') {
b += 1;
bfs2(i, j);
}
}
}
cout << a << " " << b << "\n";
return 0;
}
'๐ PS > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 11399๋ฒ: ATM (0) | 2024.07.24 |
---|---|
[Baekjoon] 11286๋ฒ: ์ ๋๊ฐ ํ (1) | 2024.07.23 |
[Baekjoon] 9095๋ฒ: 1, 2, 3 ๋ํ๊ธฐ (1) | 2024.07.20 |
[Baekjoon] 7662๋ฒ: ์ด์ค ์ฐ์ ์์ ํ (0) | 2024.07.17 |
[Baekjoon] 7569๋ฒ: ํ ๋งํ (0) | 2024.07.16 |