티스토리 뷰

Algorithm/BOJ

백준 10026번 C++

poopooreum 2023. 9. 4. 09:19
반응형
백준 10026번 적록색약

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

10026번: 적록색약

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

www.acmicpc.net



정답 코드

#include<iostream>
#include<queue>
using namespace std;
char arr[101][101];
int vis[101][101];
int dx[4] = { 1,0,-1,0 };
int dy[4] = { 0,1,0,-1 };
int four(int a) {
	queue<pair<int, int>>q;
	int num = 0;
	for (int x = 0; x < a; x++) {
		for (int y = 0; y < a; y++) {
			if (vis[x][y])
				continue;
			if (arr[x][y] == 'R') {
				num++;
				q.push({ x,y });
				while (!q.empty()) {
					pair<int, int>cur = q.front();
					q.pop();
					for (int x = 0; x < 4; x++) {
						int mx = cur.first + dx[x];
						int my = cur.second + dy[x];
						if (mx < 0 || my < 0 || mx >= a || my >= a)
							continue;
						if (arr[mx][my] != 'R' || vis[mx][my])
							continue;
						vis[mx][my] = 1;
						q.push({ mx,my });

					}
				}

			}
			if (arr[x][y] == 'G') {
				num++;
				q.push({ x,y });
				while (!q.empty()) {
					pair<int, int>cur = q.front();
					q.pop();
					for (int x = 0; x < 4; x++) {
						int mx = cur.first + dx[x];
						int my = cur.second + dy[x];
						if (mx < 0 || my < 0 || mx >= a || my >= a)
							continue;
						if (arr[mx][my] != 'G' || vis[mx][my])
							continue;
						vis[mx][my] = 1;
						q.push({ mx,my });

					}
				}

			}
			if (arr[x][y] == 'B') {
				num++;
				q.push({ x,y });
				while (!q.empty()) {
					pair<int, int>cur = q.front();
					q.pop();
					for (int x = 0; x < 4; x++) {
						int mx = cur.first + dx[x];
						int my = cur.second + dy[x];
						if (mx < 0 || my < 0 || mx >= a || my >= a)
							continue;
						if (arr[mx][my] != 'B' || vis[mx][my])
							continue;
						vis[mx][my] = 1;
						q.push({ mx,my });

					}
				}

			}

		}
	}

	return num;
	

}
int three(int a) {
	for (int x = 0; x < a; x++) {
		for (int y = 0; y < a; y++)
			vis[x][y] = 0;
	}
	queue<pair<int, int>>q;
	int num = 0;
	for (int x = 0; x < a; x++) {
		for (int y = 0; y < a; y++) {
			if (vis[x][y])
				continue;
			if (arr[x][y] == 'B') {
				num++;
				q.push({ x,y });
				while (!q.empty()) {
					pair<int, int>cur = q.front();
					q.pop();
					for (int x = 0; x < 4; x++) {
						int mx = cur.first + dx[x];
						int my = cur.second + dy[x];
						if (mx < 0 || my < 0 || mx >= a || my >= a)
							continue;
						if (arr[mx][my] != 'B' || vis[mx][my])
							continue;
						vis[mx][my] = 1;
						q.push({ mx,my });

					}
				}

			}
			else {
				num++;
				q.push({ x,y });
				while (!q.empty()) {
					pair<int, int>cur = q.front();
					q.pop();
					for (int x = 0; x < 4; x++) {
						int mx = cur.first + dx[x];
						int my = cur.second + dy[x];
						if (mx < 0 || my < 0 || mx >= a || my >= a)
							continue;
						if (arr[mx][my] =='B' || vis[mx][my])
							continue;
						vis[mx][my] = 1;
						q.push({ mx,my });

					}
				}

			}
		}
	}
	return num;
}
int main() {
	int a;
	cin >> a;
	for (int x = 0; x < a; x++) {
		for (int y = 0; y < a; y++) {
			cin >> arr[x][y];
		}
	}
	cout << four(a) << " ";
	cout<< three(a);
}

제 풀이

bfs를 이용해서 문제를 풀었습니다. 적록색약 구역을 구하는 문제인데 적록색약인 사람이 볼 수 있는 경우와
적록색약이 아닌 사람이 볼 수 있는 경우의 수를 나누어서 풀었습니다. 전자는 r,g를 같은 선상으로 취급하고
후자는 r,g를 다른 선상으로 취급했습니다. 배열의.범위를 벗어나거나 이미 방문한 곳들은 continue시켜서 문제를 해결했습니다.

반응형

'Algorithm > BOJ' 카테고리의 다른 글

백준 10170번 C++  (0) 2023.09.04
백준 10101번 C++  (0) 2023.09.04
백준 9711번 C++  (0) 2023.09.04
백준 9663번 C++  (0) 2023.09.03
백준 9506번 C++  (0) 2023.08.24
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
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 31
글 보관함