Creative Code

[10026번]적록색약 본문

백준 문제풀이

[10026번]적록색약

빛하루 2023. 9. 24. 17:31

https://www.acmicpc.net/problem/10026

 

10026번: 적록색약

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)

www.acmicpc.net

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <queue>

using namespace std;

int arr[101][101];
char arr1[101][101];
bool visit[101][101];
bool visit1[101][101];
int dx[] = { 0,1,0,-1 };
int dy[] = { -1,0,1,0 };

int main() {
	cin.tie(0);
	cout.tie(0);
	ios_base::sync_with_stdio(false);
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			char c;
			cin >> c;
			arr1[i][j] = c;
			if (c == 'R' || c == 'G') {
				arr[i][j] = 1;
				
			}
			else {
				arr[i][j] = 2;
			}
			visit[i][j] = false;
		}
	}
	queue<pair<int, int>>q1;
	vector<int>answer1;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (visit1[i][j] == false) {
				answer1.push_back(1);
				q1.push(make_pair(i, j));
				while (!q1.empty()) {
					int x = q1.front().first;
					int y = q1.front().second;
					q1.pop();
					for (int k = 0; k < 4; k++) {
						int nx = x + dx[k];
						int ny = y + dy[k];
						if (nx == -1 || ny == -1 || nx == N || ny == N || arr1[nx][ny] != arr1[x][y] || visit1[nx][ny] == true) {
							continue;
						}
						else if (arr1[nx][ny] == arr1[x][y]) {
							visit1[nx][ny] = true;
							q1.push(make_pair(nx, ny));
						}
					}
				}
			}

		}
	}
	cout << answer1.size() << '\n';

	queue<pair<int, int>>q;
	vector<int>answer;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (visit[i][j] == false) {
				answer.push_back(1);
				q.push(make_pair(i, j));
				while (!q.empty()) {
					int x = q.front().first;
					int y = q.front().second;
					q.pop();
					for (int k = 0; k < 4; k++) {
						int nx = x + dx[k];
						int ny = y + dy[k];
						if (nx == -1 || ny == -1 || nx == N || ny == N || arr[nx][ny] != arr[x][y] ||visit[nx][ny] == true) {
							continue;
						}
						else if (arr[nx][ny] == arr[x][y]) {
							visit[nx][ny] = true;
							q.push(make_pair(nx, ny));
						}
					}
				}
			}

		}
	}
	cout << answer.size() << '\n';
}

'백준 문제풀이' 카테고리의 다른 글

[16928번]뱀과 사다리 게임  (0) 2023.09.24
[12856번]평범한 배낭  (0) 2023.09.24
[7576번]토마토  (0) 2023.09.24
[7569번]토마토  (0) 2023.09.24
[5430번]AC  (0) 2023.09.24