기본 콘텐츠로 건너뛰기

1074 : Z (Recursive) [C,C++]

먼저 방문 할 인덱스들의 행,열 값을 입력하면

길이가 2일 때(입력 값이 1일 때)

(0,0), (0,1)
(1,0), (1,1)

길이가 4일 때(입력 값이 2일 때)

(0,0), (0,1), (0,2), (0,3)
(1,0), (1,1), (1,2), (1,3)
(2,0), (2,1), (2,2), (2,3)
(3,0), (3,1), (3,2), (3,3)

길이가 8일 때(입력 값이 3일 때)

(0,0), (0,1), (0,2), (0,3), (0,4), (0,5), (0,6), (0,7)
(1,0), (1,1), (1,2), (1,3), (1,4), (1,5), (1,6), (1,7)
(2,0), (2,1), (2,2), (2,3), (2,4), (2,5), (2,6), (2,7)
(3,0), (3,1), (3,2), (3,3), (3,4), (3,5), (3,6), (3,7)
(4,0), (4,1), (4,2), (4,3), (4,4), (4,5), (4,6), (4,7)
(5,0), (5,1), (5,2), (5,3), (5,4), (5,5), (5,6), (5,7)
(6,0), (6,1), (6,2), (6,3), (6,4), (6,5), (6,6), (6,7)
(7,0), (7,1), (7,2), (7,3), (7,4), (7,5), (7,6), (7,7)

빨간색 부분이 계속 나오는 것을 알 수 있습니다. 

그래서 재귀함수를 써야합니다.

재귀적으로 부를 소스를 보면

void Z::GetCount(int row, int column, int size)
{
 if (size == 2)
 {
  if (!this->IsEnd)
  {
   this->Count++;
   if (row == this->Target[0] && column == this->Target[1])
    this->IsEnd = true;
  }
  if (!this->IsEnd)
  {
   this->Count++;
   if (row == this->Target[0] && column + size / 2 == this->Target[1])
    this->IsEnd = true;
  }
  if (!this->IsEnd)
  {
   this->Count++;
   if (row + size / 2 == this->Target[0] && column == this->Target[1])
    this->IsEnd = true;
  }
  if (!this->IsEnd)
  {
   this->Count++;
   if (row + size / 2 == this->Target[0] && column + size / 2 == this->Target[1])
    this->IsEnd = true;
  }
 }
 else if(size > 2)
 {
  GetCount(row, column, size / 2);
  GetCount(row, column + size / 2, size / 2);
  GetCount(row + size / 2, column, size / 2);
  GetCount(row + size / 2, column + size / 2, size / 2);
 }
}
<소스 코드>

size가 2라는 말은 길이가 2라는 말입니다.

길이가 2인 행렬 2*2는 4가지의 인덱스로 구성되어 있는데

4가지의 인덱스에 다시 재귀함수를 써 접근했더니 

시간 초과가 떠서 이런 식으로 풀었습니다.

*Source of the problem = https://www.acmicpc.net/problem/1074
*문제 출처 : BAEKJOON ONLINE JUDGE

댓글

이 블로그의 인기 게시물

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