기본 콘텐츠로 건너뛰기

K사 : 같은 색깔의 블록 갯수 구하기 (Flood Fill)(Recursive) [C,C++]

K사의 17년도 1번 문제이다.

문제를 한 마디로 말하면

'가장 많은 그룹을 가진 색의 그룹 개수와 최대 색칠 개수를 세라' 인데

이해하기 어려우시죠?

그림으로 보여드리겠습니다.


여기서 가장 많은 그룹을 가진 색은 빨간색입니다.
그룹의 개수는 4개입니다.
최대 색칠 개수는 오른쪽 위에 있는 43개입니다.

이해가 가시나요?

재귀 알고리즘 중 하나인 Flood Fill 알고리즘을 이용해 푸는 문제입니다.

계속 방문하여 개수를 체크 후에 더 체크할 게 없다면 return하는 방식입니다.

색깔을 찾는 코드만 짤 수 있으면 이 문제도 풀 수 있기 때문에

저는 하얀 배경에서 검은 색깔을 찾는 코드를 만들었습니다.

여러 색깔을 담는 배열 하나만 만들어주면 바로 바꿀 수 있는 문제입니다.

void MoveDirection(vector<vector<int>> &picture, int row, int column, int count)
{
 picture[row][column] = -1;
 
 count++;
 if (count > MaxCount)
  MaxCount = count;
 
 if (column < picture[0].size() - 1)
 {
  if (picture[row][column + 1] > 0) // RIGHT
   MoveDirection(picture, row, column + 1, count);
 }
 if (column > 0)
 {
  if (picture[row ][column - 1] > 0) // LEFT
   MoveDirection(picture, row, column - 1, count);
 }
 if (row > 0)
 {
  if (picture[row - 1][column] > 0) // UP
   MoveDirection(picture, row - 1, column, count);
 }
 if (row < picture.size() - 1)
 {
  if (picture[row + 1][column] > 0) // DOWN
   MoveDirection(picture, row + 1, column, count);
 }
}

vector<int> solution(vector<vector<int>> &picture)
{
 for (int iIndex = 0; iIndex < picture.size(); iIndex++)
 {
  for (int jIndex = 0; jIndex < picture[0].size(); jIndex++)
  {
   if (picture[iIndex][jIndex] > 0)
   {
    MoveDirection(picture, iIndex, jIndex, 0);
    PartCount++;
   }
  }
 }

 vector<int> answer = { PartCount, MaxCount };
 return answer;
}
<검정색의 그룹 개수와 최대 색칠 개수를 찾는 코드>

댓글

이 블로그의 인기 게시물

6359 : 만취한 상범 (Dynamic Programming) [C++]

# include < iostream > # include < vector > using namespace std ; int Num_of_Divisor ( int n ) { int Count = 0 ; for ( int i = 1 ; i < = n ; i + + ) if ( n % i = = 0 ) Count + + ; return Count ; } int main ( ) { int Testcase ; cin > > Testcase ; while ( Testcase - - ) { int Rooms ; cin > > Rooms ; vector < int > Prisons ; for ( int i = 0 ; i < Rooms ; i + + ) Prisons . push_back ( 0 ) ; for ( int i = 1 ; i < Prisons . size ( ) + 1 ; i + + ) { if ( ( Num_of_Divisor ( i ) % 2 ) = = 0 ) Prisons [ i - 1 ] = 0 ; else Prisons [ i - 1 ] = 1 ; } int Fleer = 0 ; for ( int i = 0 ; i < Prisons . size ( ) ; i + + ) if ( Prisons ...

11004 : K번째 수 [C++]

# include < iostream > # include < cstdio > # include < algorithm > int main ( ) { int * Number = new int [ 5000000 ] ; int N , K ; scanf ( " %d %d " , & N , & K ) ; for ( int i = 0 ; i < N ; i + + ) scanf ( " %d " , Number [ i ] ) ; std :: sort ( Number , Number + N ) ; printf ( " %d " , Number [ K - 1 ] ) ; return 0 ; }

1149 : RGB Street Coloring (Dynamic Programming) [C,C++]

The key to this problem lies in understanding the principles. Let me explain the algorithm to solve the problem by using DP. First, you need the same storage space like input data's size. When you draw any color of the nth house, the space will contain the minimum value. If you paint the red in the second house, this value is sum of blue or green of the first house.  You must use DP because you must use the previous value.  Of course, you can also use the recursive algorithm to solve it. But if it gets bigger, it will take a lot of time.  If you paint the red in the nth house in the same way, you should add the lower value of the blue and green of the n-1th house.  Therefore, the minimum value can be found in the value of the storage space (n-1) index. <pesudo code> *Source of the problem =  https://www.acmicpc.net/problem/1149 *문제 출처 : BAEKJOON ONLINE JUDGE