알고리즘/BaekJoon
[c++] 10026번 : 적록색약
__jhp_+
2022. 11. 16. 16:38
https://www.acmicpc.net/problem/10026
10026번: 적록색약
적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다. 크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록)
www.acmicpc.net
#include <iostream>
#include <string>
#include <stack>
using namespace std;
#define MAX_SIZE 100
bool IsValidate(char ch, char picture, bool isNormal)
{
if (isNormal == true || picture == 'B')
{
return ch == picture;
}
else
{
return ch == 'R' || ch == 'G';
}
}
void CheckBoard(
int N,
string board[],
bool pass[][MAX_SIZE],
bool isNormal,
int x, int y
) {
int offX[] = { -1, 0, 1, 0 };
int offY[] = { 0, -1, 0, 1 };
stack<pair<int, int>> st;
char picture = board[y][x];
bool isPush = false;
st.emplace(x, y);
while (!st.empty())
{
pair<int, int> node = st.top();
int curX = node.first;
int curY = node.second;
pass[curY][curX] = true;
for (int i = 0; i < 4; ++i)
{
int toX = curX + offX[i];
int toY = curY + offY[i];
if (toX < 0 || toY < 0 || toX >= N || toY >= N || pass[toY][toX] == true)
{
continue;
}
else
{
if (IsValidate(board[toY][toX], picture, isNormal) == true)
{
isPush = true;
st.emplace(toX, toY);
break;
}
}
}
if (isPush == false)
{
st.pop();
}
else
{
isPush = false;
}
}
return;
}
int Progress(int N, string board[], bool isNormal)
{
int result = 0;
bool pass[MAX_SIZE][MAX_SIZE] = { false, };
for (int i = 0; i < N; ++i)
{
for (int j = 0; j < N; ++j)
{
if (pass[i][j] == true)
{
continue;
}
else
{
++result;
CheckBoard(N, board, pass, isNormal, j, i);
}
}
}
return result;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
string board[MAX_SIZE];
int N;
cin >> N;
cin.ignore();
for (int i = 0; i < N; ++i)
{
getline(cin, board[i]);
}
int r1 = Progress(N, board, true);
int r2 = Progress(N, board, false);
cout << r1 << ' ' << r2 << '\n';
return 0;
}