Thief of Wealth
Published 2019. 4. 12. 15:33
10026 적록색약 개발/알고리즘
시간 제한메모리 제한제출정답맞은 사람정답 비율
1 초128 MB69803970318058.359%

문제

적록색약은 빨간색과 초록색의 차이를 거의 느끼지 못한다. 따라서, 적록색약인 사람이 보는 그림은 아닌 사람이 보는 그림과는 좀 다를 수 있다.

크기가 N×N인 그리드의 각 칸에 R(빨강), G(초록), B(파랑) 중 하나를 색칠한 그림이 있다. 그림은 몇 개의 구역으로 나뉘어져 있는데, 구역은 같은 색으로 이루어져 있다. 또, 같은 색상이 상하좌우로 인접해 있는 경우에 두 글자는 같은 구역에 속한다. (색상의 차이를 거의 느끼지 못하는 경우도 같은 색상이라 한다)

예를 들어, 그림이 아래와 같은 경우에

RRRBB
GGBBB
BBBRR
BBRRR
RRRRR

적록색약이 아닌 사람이 봤을 때 구역의 수는 총 4개이다. (빨강 2, 파랑 1, 초록 1) 하지만, 적록색약인 사람은 구역을 3개 볼 수 있다. (빨강-초록 2, 파랑 1)

그림이 입력으로 주어졌을 때, 적록색약인 사람이 봤을 때와 아닌 사람이 봤을 때 구역의 수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (1 ≤ N ≤ 100)

둘째 줄부터 N개 줄에는 그림이 주어진다.

출력

적록색약이 아닌 사람이 봤을 때의 구역의 개수와 적록색약인 사람이 봤을 때의 구역의 수를 공백으로 구분해 출력한다.


간단하게 적록색맹일경우에는 R과 G를 같은 경우로 두고 풀면된다.!

#include <bits/stdc++.h>
using namespace std;

struct Pair{
int x;
int y;
};

int x_direction[4] = {-1,1,0,0};
int y_direction[4] = {0,0,-1,1};

string img[101];
string img2[101];
bool visited[101][101];
bool visited2[101][101];
int n;

queue<Pair> que;
queue<Pair> que2;

void bfs();
void bfs2();

int main(){

freopen("input.txt","r",stdin);

ios::sync_with_stdio(false);
cin.tie(0);

string temp;
cin>>n;

memset(visited, false, sizeof(visited));
memset(visited2, false, sizeof(visited2));

for(int i=0; i<n; ++i){
cin>>temp;
img[i] = temp;
}
int nomalCanSee=0;
int notNomalCanSee=0;
for(int i=0; i<n; ++i){
for(int j=0; j<n; ++j){
if( !visited[i][j] ){
que.push({i,j}); //only find this charactor.
bfs();
nomalCanSee++;
}
if( !visited2[i][j] ){
que2.push({i,j}); //only find this charactor.
bfs2();
notNomalCanSee++;
}
}
}

cout<<nomalCanSee<<" "<<notNomalCanSee;

return 0;
}

void bfs(){

Pair p = que.front();
char pivot_charactor = img[p.x][p.y]; //this is a critical pivot.
int step_width = que.size();
int temp;
int howManyArea=0;
while( !que.empty() ){
step_width = que.size();
temp =0;
for(int j=0; j<step_width; ++j){
p = que.front();
que.pop();
for(int i=0; i<4; ++i){
if( (p.x+x_direction[i] >= 0 && p.x+x_direction[i] < n) &&
(p.y+y_direction[i] >= 0 && p.y+y_direction[i] < n) &&
!visited[p.x+x_direction[i]][p.y+y_direction[i]] &&
img[p.x+x_direction[i]][p.y+y_direction[i]] == pivot_charactor
) {

que.push({p.x+x_direction[i],p.y+y_direction[i]});
visited[p.x+x_direction[i]][p.y+y_direction[i]] = true;
temp++;
}
}
}
}
}


void bfs2(){

Pair p = que2.front();
char pivot_charactor = img[p.x][p.y]; //this is a critical pivot.
char pivot_charactor2 = pivot_charactor;

if(pivot_charactor == 'G'){
pivot_charactor2 = 'R';
} else if(pivot_charactor == 'R'){
pivot_charactor2 = 'G';
}

int step_width = que2.size();
int temp;
int howManyArea=0;
while( !que2.empty() ){
step_width = que2.size();
temp =0;
for(int j=0; j<step_width; ++j){
p = que2.front();
que2.pop();
for(int i=0; i<4; ++i){
if( (p.x+x_direction[i] >= 0 && p.x+x_direction[i] < n) &&
(p.y+y_direction[i] >= 0 && p.y+y_direction[i] < n) &&
!visited2[p.x+x_direction[i]][p.y+y_direction[i]] &&
(img[p.x+x_direction[i]][p.y+y_direction[i]] == pivot_charactor || img[p.x+x_direction[i]][p.y+y_direction[i]] == pivot_charactor2 )
) {

que2.push({p.x+x_direction[i],p.y+y_direction[i]});
visited2[p.x+x_direction[i]][p.y+y_direction[i]] = true;
temp++;
}
}
}
}
}






'개발 > 알고리즘' 카테고리의 다른 글

2468 안전영역  (0) 2019.04.14
2206 벽 부수고 이동하기 (풀이보고 풀었음)  (0) 2019.04.14
7562 나이트의 이동  (0) 2019.04.12
11404 플로이드  (0) 2019.04.11
12865 평범한 배낭 (dp로 풀기)  (0) 2019.04.10
profile on loading

Loading...