10026번 적록색약

동도리동·2021년 9월 2일
0

코딩테스트

목록 보기
29/76

최근에 sk하이닉스 원서랑 영어때문에 코테연습을 못했었다. 쫌만 안해도 감을 잃어버리는 것 같다. 꾸준히 하자!!

이번 문제는 상당히 쉽다. 건질게 있다면 영역을 구분할 필요가 있기 때문에 queue를 이용하되, 브루트포스로 하나하나 위치를 탐색하도록 감싸주는게 필요하다.

#include <iostream>
#include <algorithm>
#include <string>
#include <queue>
#include <string.h>
using namespace std;
char map[100][100];
int ch[100][100];
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
struct Loc {
	int x, y;
	Loc(int a, int b) {
		x = a;
		y = b;
	}
};
int main() {
	int n;
	//freopen("in1.txt", "rt", stdin);
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> map[i];
	}
	queue<Loc> Q;
	int cnt1 = 0;
	for (int x = 0; x < n; x++) {
		for (int y = 0; y < n; y++) {
			if (ch[x][y] == 0) {
				Q.push(Loc(x, y));
				cnt1++;
				while (!Q.empty()) {
					Loc tmp = Q.front();
					Q.pop();
					for (int i = 0; i < 4; i++) {
						int xx = tmp.x + dx[i];
						int yy = tmp.y + dy[i];
						if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
						if (ch[xx][yy] == 0 && map[xx][yy] == map[tmp.x][tmp.y]) {
							ch[xx][yy] = 1;
							Q.push(Loc(xx, yy));
						}
					}
				}
			}
		}
	}
	memset(ch, 0, sizeof(ch));
	int cnt2 = 0;
	for (int x = 0; x < n; x++) {
		for (int y = 0; y < n; y++) {
			if (ch[x][y] == 0) {
				Q.push(Loc(x, y));
				cnt2++;
				while (!Q.empty()) {
					Loc tmp = Q.front();
					Q.pop();
					for (int i = 0; i < 4; i++) {
						int xx = tmp.x + dx[i];
						int yy = tmp.y + dy[i];
						if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
						if (ch[xx][yy] == 0) {
							if ((map[tmp.x][tmp.y] == 'R' && map[xx][yy] == 'G') || (map[tmp.x][tmp.y] == 'G' && map[xx][yy] == 'R') || map[xx][yy] == map[tmp.x][tmp.y]) {
								ch[xx][yy] = 1;
								Q.push(Loc(xx, yy));
							}
						}
					}
				}
			}
		}
	}
	cout << cnt1 << " " << cnt2 << '\n';
	return 0;
}
profile
긍정코딩세상

0개의 댓글

관련 채용 정보