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 |
Tags
- 오퍼레이팅시스템
- Stack
- 컴공
- 스택
- 문제풀이
- coding
- 오에스
- Operating System
- 정석
- 백준
- 구현
- 코테
- 알고리즘
- 코딩
- 그래프
- OS
- 컴공과
- DP
- cs
- 정석학술정보관
- 개발
- 브루트포스
- Computer science
- 컴퓨터공학과
- 너비우선탐색
- c++
- 자료구조
- vector
- 북리뷰
- bfs
Archives
- Today
- Total
Little Jay
[C++] 백준 10026번 - 적녹색약 본문
BFS를 기반으로 하는 문제이다. 단순히 BFS를 두번 돌리면 되는데, 첫 번째는 정상적인 BFS로, 두 번째는 R==G일때도 통과되게 만들면 된다.
#include <bits/stdc++.h>
#define endl '\n'
#define pii pair<int, int>
using namespace std;
int t;
int n;
char graph[101][101];
bool visited[101][101];
const int dx[] = { 1, -1, 0, 0 };
const int dy[] = { 0, 0, 1, -1 };
int bfs(int x, int y, bool eye) {
visited[x][y] = true;
char target = graph[x][y];
queue<pii> q;
q.push({ x, y });
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 < 1 || ny < 1 || nx > n || ny > n) continue;
if (target != graph[nx][ny]) {
if (eye && (target == 'R' && graph[nx][ny] == 'G' || target == 'G' && graph[nx][ny] == 'R')) {
}
else continue;
}
if (visited[nx][ny]) continue;
q.push({ nx, ny });
visited[nx][ny] = true;
}
}
return 1;
}
void solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> graph[i][j];
}
}
int normal = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!visited[i][j]) {
normal += bfs(i, j, false);
}
}
}
memset(visited, false, sizeof(visited));
int abnormal = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (!visited[i][j]) {
abnormal += bfs(i, j, true);
}
}
}
cout << normal << " " << abnormal << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
t = 1;
while (t--) {
solve();
}
return 0;
}
'알고리즘 > BOJ' 카테고리의 다른 글
[C++] 백준 14675번 - 단절점과 단절선 (1) | 2022.09.19 |
---|---|
[C++] 백준 13164번 - 행복한 유치원 (0) | 2022.09.03 |
[C++] 백준 1965번 - 상자 넣기 (0) | 2022.08.30 |
[C++] 백준 1759번 - 암호 만들기 (0) | 2022.08.26 |
[C++] 백준 2206번 - 벽 부수고 이동하기 (0) | 2022.08.23 |
Comments