[C++] 10026 : 적녹색약

리폐·2023년 11월 14일

백준

목록 보기
15/18

📝 문제

10026 : 적녹색약


✏️ 입력1

5
RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

💻 출력1

4 3

⌨️ 소스코드

10026 적녹색약
#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
#define X first
#define Y second

int n;
char board[102][102];
int vis[102][102];
int dx[4] = { 1, 0, -1, 0 };
int dy[4] = { 0, 1, 0, -1 };

void bfs(int x, int y) {
	queue<pair<int, int>> que;
	que.push({ x, y });
	vis[x][y] = 1;
	while (!que.empty()) {
		pair<int, int> cur = que.front(); que.pop();
		for (int dir = 0; dir < 4; dir++) {
			int nx = cur.X + dx[dir];
			int ny = cur.Y + dy[dir];
			if (nx < 0 || nx >= n || ny < 0 || ny >= n) continue;
			if (vis[nx][ny] == 1 || board[x][y] != board[nx][ny]) continue;
			vis[nx][ny] = 1;
			que.push({ nx, ny });
		}
	}
}

int area() {
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (!vis[i][j]) {
				ans++;
				bfs(i, j);
			}
		}
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> board[i][j];
		}
	}
	int no = area();
	
    //적녹색약이 아닌사람에서 사용하여 다시 초기화
	for (int i = 0; i < n; i++) fill(vis[i], vis[i] + n, false);

	//초록색일때 빨간색으로 바꿔주기
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			if (board[i][j] == 'G') board[i][j] = 'R';
		}
	}
	int gre = area();
	cout << no << " " << gre << "\n";
}

profile
Unreal 5, Unity 공부

0개의 댓글