기본 콘텐츠로 건너뛰기

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

댓글

이 블로그의 인기 게시물

1978 : 소수 찾기 [C++]

# include < iostream > # include < vector > using namespace std ; int main ( ) { cin . tie ( NULL ) ; vector < int > Primes ; Primes . push_back ( 2 ) ; Primes . push_back ( 3 ) ; for ( int i = 4 ; i < 1000 ; i + + ) { bool IsPrime = true ; if ( i % 2 = = 0 | | i % 3 = = 0 ) continue ; for ( int j = 4 ; j < i ; j + + ) { if ( i % j = = 0 ) { IsPrime = false ; break ; } } if ( IsPrime ) Primes . push_back ( i ) ; } int N , Count = 0 ; cin > > N ; for ( int i = 0 ; i < N ; i + + ) { int Input ; cin > > Input ; for ( int j = 0 ; j < Primes . size ( ) ; j + + ) if ( Input = = Primes [ j ] ) Count + + ; } cout < < Count < < " \n " ; return 0 ; }

10828 : 스택 [Python]

Stack = [ ] def push ( num ) : Stack . append ( int ( num ) ) def pop ( ) : if len ( Stack ) > 0 : print ( Stack . pop ( ) ) else : print ( - 1 ) def size ( ) : print ( len ( Stack ) ) def empty ( ) : if len ( Stack ) == 0 : print ( 1 ) else : print ( 0 ) def top ( ) : if len ( Stack ) > 0 : print ( Stack [ len ( Stack ) - 1 ] ) else : print ( - 1 ) TestCase = int ( input ( ) ) while TestCase > 0 : Command = input ( ) if Command == 'top' : top ( ) elif Command == 'pop' : pop ( ) elif Command == 'empty' : empty ( ) elif Command == 'size' : size ( ) else : push ( Command [ 5 : ] ) TestCase - = 1