HyeLog
๋ฐฑ์ค_10026_์ ๋ก์์ฝ ๋ณธ๋ฌธ
๐โ๏ธ ๋ฌธ์
https://www.acmicpc.net/problem/10026
10026๋ฒ: ์ ๋ก์์ฝ
์ ๋ก์์ฝ์ ๋นจ๊ฐ์๊ณผ ์ด๋ก์์ ์ฐจ์ด๋ฅผ ๊ฑฐ์ ๋๋ผ์ง ๋ชปํ๋ค. ๋ฐ๋ผ์, ์ ๋ก์์ฝ์ธ ์ฌ๋์ด ๋ณด๋ ๊ทธ๋ฆผ์ ์๋ ์ฌ๋์ด ๋ณด๋ ๊ทธ๋ฆผ๊ณผ๋ ์ข ๋ค๋ฅผ ์ ์๋ค. ํฌ๊ธฐ๊ฐ N×N์ธ ๊ทธ๋ฆฌ๋์ ๊ฐ ์นธ์ R(๋นจ๊ฐ), G(์ด๋ก)
www.acmicpc.net
๐งฉ ์๊ณ ๋ฆฌ์ฆ
์ต์(์ต๋จ)๋ฅผ ๊ตฌํ๋ ๋ฌธ์ ๊ฐ ์๋๋ฏ๋ก BFS, DFS ๋๋ค ์ฌ์ฉ ๊ฐ๋ฅ (๋๋ BFS๋ก ํ์๋ค.)
์ ์ : ํฌ๊ธฐ๊ฐ N×N์ธ ๊ทธ๋ฆฌ๋์ ๊ฐ ์นธ
๊ฐ์ : ์นธ → ์นธ
๐ก ์์ด๋์ด
-์ด๋ ๊ฐ๋ฅํ ์ ์ ์ธ์ง ์ฒดํฌํ๋ ํจ์๋ฅผ ๋ฐ๋ก ๊ตฌํ.
-์ ๋ก์์ฝ์ธ ์ฌ๋์ ์ํ ํจ์, ์๋ ์ฌ๋์ ์ํ ํจ์ 2๊ฐ๋ฅผ ๋ฐ๋ก ๋ง๋๋ ๊ฒ์ด ์๋๋ผ ํ๋์ ํจ์๋ก ์ฒ๋ฆฌ. (๋งค๊ฐ๋ณ์๋ก ์ ๋ก์์ฝ ์ฌ๋ถ ์ ๋ฌ)
-๊ตฌ์ญ์ ๊ฐ์ = BFS์ ์์ ํ์
๐ฉ๐ป ์ฝ๋
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
int dx[4] = { 0,0,-1,1 };
int dy[4] = { -1,1,0,0 };
bool can(bool blind, char from, char to) { // ์ด๋ ๊ฐ๋ฅํ ์ ์ ์ธ์ง ํ์ธํ๋ ํจ์
if (from == to) return true;
if (blind) {
if (from == 'R' && to == 'G') return true;
if (from == 'G' && to == 'R') return true;
}
return false;
}
int solve(vector<string> &a, bool blind = false) {
// ์ด๊ธฐํ
int n = a.size();
vector<vector<bool>> visited(n, vector<bool>(n, false));
int ans = 0; // ๊ตฌ์ญ ๊ฐ์
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (visited[i][j] == false) {
// ๊ตฌ์ญ ์ฆ๊ฐ์ํค๊ธฐ
ans += 1;
// BFS
queue<pair<int, int>> q;
q.push(make_pair(i, j));
visited[i][j] = true;
while (!q.empty()) {
auto cur = q.front();
q.pop();
for (int i = 0; i < 4; i++) {
int nx = cur.first + dx[i];
int ny = cur.second + dy[i];
if (nx >= 0 && nx < n && ny >= 0 && ny < n) { // ๋ฒ์ ์ฒดํฌ
if (visited[nx][ny] == false && can(blind, a[cur.first][cur.second], a[nx][ny])) { // ๋ฐฉ๋ฌธ ์ฌ๋ถ, ์๊น ์ฒดํฌ
q.push(make_pair(nx, ny));
visited[nx][ny] = true;
}
}
}
}
}
}
}
return ans;
}
int main() {
// input
int n;
cin >> n;
vector<string> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// solve, output
cout << solve(a) << ' ' << solve(a, true) << '\n';
return 0;
}
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค_5014_์คํํธ๋งํฌ (0) | 2022.07.06 |
---|---|
๋ฐฑ์ค_14395_4์ฐ์ฐ (0) | 2022.07.02 |
๋ฐฑ์ค_1963_์์ ๊ฒฝ๋ก (0) | 2022.07.02 |
๋ฐฑ์ค_12886_๋๊ทธ๋ฃน (0) | 2022.06.24 |
๋ฐฑ์ค_14502_์ฐ๊ตฌ์ (0) | 2022.06.16 |