본문 바로가기

C언어

2025년 2학기 9주차 C언어

import sys
input = sys.stdin.readline

N = int(input())
paper = [list(map(int, input().split())) for _ in range(N)]

stack = { -1: 0, 0: 0, 1: 0 }
#-1, 0, 1이 각각 얼마나 있는지 사전의 형태로 나타낸다
#처음에는 아무것도 없으니 0이다

#재귀함수다
#종이의 숫자를 확인하는 도중 다른 숫자가 나타나면 함수를 호출

def check(x, y, size):
    first = paper[x][y]

    for i in range(x, x + size):
        for j in range(y, y + size):
            if paper[i][j] != first:
            #재귀함수가 시작되는 부분이다
                cut = size // 3
                
                check(x, y, cut)
                check(x, y + cut, cut)
                check(x, y + 2 * cut, cut)
                #정사각형을 9개로 나눴을 때 아래의 3개

                check(x + cut, y, cut)
                check(x + cut, y + cut, cut)
                check(x + cut, y + 2 * cut, cut)
                #정사각형을 9개로 나눴을 때 중간의 3개

                check(x + 2 * cut, y, cut)
                check(x + 2 * cut, y + cut, cut)
                check(x + 2 * cut, y + 2 * cut, cut)
                #정사각형을 9개로 나눴을 때 위의 3개 
                #이런 식으로 각각 순차적으로 탐색한다
                
                return

    stack[first] += 1

check(0, 0, N)

print(stack[-1])
print(stack[0])
print(stack[1])

 

 

 

import sys
input = sys.stdin.readline

n = int(input())
line = [int(input()) for _ in range(n)]

stack = []
oper = [] #연산자를 기록한다
now = 1 #push할 수다. 초기값은 1이다
make = True #수열을 만들 수 있을지다

for num in line:
    # 목표 수 num이 스택에 없으면 push
    while now <= num:
        stack.append(now)
        oper.append('+')
        now += 1
    
    # 스택의 top을 확인한다
    if stack[-1] == num: #스택의 top과 숫자가 같은 경우
        stack.pop()
        oper.append('-')
        
    #스택의 top과 목표 숫자가 다르면 수열 만들기가 불가능
    else:
        make = False
        break

if make:
    print('\n'.join(oper))
else:
    print('NO')

'C언어' 카테고리의 다른 글

2025년 2학기 8주차 C언어  (0) 2025.11.16
2025년 2학기 7주차 C언어  (0) 2025.11.09
2025년 2학기 6주차 C언어  (0) 2025.11.02
2025년 2학기 5주차 C언어  (0) 2025.10.11
2025년 2학기 4주차 C언어  (1) 2025.10.05