하다보니

10026번-적록색약 본문

알고리즘 풀이/백준

10026번-적록색약

claire 2022. 2. 4. 11:47
#include<bits/stdc++.h>
#define X first
#define Y second
using namespace std;

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

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

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

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> board[i];
	}
	
	int n_rb = 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] == 'R')board[i][j] = 'G';
		}
	}

	int rb = area();
	cout << n_rb <<" "<< rb;
}

'알고리즘 풀이 > 백준' 카테고리의 다른 글

7562번-나이트의 이동  (0) 2022.02.05
7569번-토마토  (0) 2022.02.05
1012번-유기농 배추  (0) 2022.02.03
1697번-숨바꼭질  (0) 2022.02.03
1697번-불!  (0) 2022.01.30