[๋ฐฑ์ค€/python] 2630๋ฒˆ : ์ƒ‰์ข…์ด ๋งŒ๋“ค๊ธฐ

2021. 4. 6. 20:41

๋ฌธ์ œ 

์•„๋ž˜ <๊ทธ๋ฆผ 1>๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ •์‚ฌ๊ฐํ˜•์นธ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ์ข…์ด๊ฐ€ ์ฃผ์–ด์ ธ ์žˆ๊ณ , ๊ฐ ์ •์‚ฌ๊ฐํ˜•๋“ค์€ ํ•˜์–€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ฑฐ๋‚˜ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๋‹ค. ์ฃผ์–ด์ง„ ์ข…์ด๋ฅผ ์ผ์ •ํ•œ ๊ทœ์น™์— ๋”ฐ๋ผ ์ž˜๋ผ์„œ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ๋ฅผ ๊ฐ€์ง„ ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์˜ ํ•˜์–€์ƒ‰ ๋˜๋Š” ํŒŒ๋ž€์ƒ‰ ์ƒ‰์ข…์ด๋ฅผ ๋งŒ๋“ค๋ ค๊ณ  ํ•œ๋‹ค.

์ „์ฒด ์ข…์ด์˜ ํฌ๊ธฐ๊ฐ€ N×N(N=2k, k๋Š” 1 ์ด์ƒ 7 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜) ์ด๋ผ๋ฉด ์ข…์ด๋ฅผ ์ž๋ฅด๋Š” ๊ทœ์น™์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

์ „์ฒด ์ข…์ด๊ฐ€ ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ์ง€ ์•Š์œผ๋ฉด ๊ฐ€๋กœ์™€ ์„ธ๋กœ๋กœ ์ค‘๊ฐ„ ๋ถ€๋ถ„์„ ์ž˜๋ผ์„œ <๊ทธ๋ฆผ 2>์˜ I, II, III, IV์™€ ๊ฐ™์ด ๋˜‘๊ฐ™์€ ํฌ๊ธฐ์˜ ๋„ค ๊ฐœ์˜ N/2 × N/2์ƒ‰์ข…์ด๋กœ ๋‚˜๋ˆˆ๋‹ค. ๋‚˜๋ˆ„์–ด์ง„ ์ข…์ด I, II, III, IV ๊ฐ๊ฐ์— ๋Œ€ํ•ด์„œ๋„ ์•ž์—์„œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ชจ๋‘ ๊ฐ™์€ ์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ์ง€ ์•Š์œผ๋ฉด ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๋˜‘๊ฐ™์€ ํฌ๊ธฐ์˜ ๋„ค ๊ฐœ์˜ ์ƒ‰์ข…์ด๋กœ ๋‚˜๋ˆˆ๋‹ค. ์ด์™€ ๊ฐ™์€ ๊ณผ์ •์„ ์ž˜๋ผ์ง„ ์ข…์ด๊ฐ€ ๋ชจ๋‘ ํ•˜์–€์ƒ‰ ๋˜๋Š” ๋ชจ๋‘ ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ ธ ์žˆ๊ฑฐ๋‚˜, ํ•˜๋‚˜์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์ด ๋˜์–ด ๋” ์ด์ƒ ์ž๋ฅผ ์ˆ˜ ์—†์„ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณตํ•œ๋‹ค.

์œ„์™€ ๊ฐ™์€ ๊ทœ์น™์— ๋”ฐ๋ผ ์ž˜๋ž์„ ๋•Œ <๊ทธ๋ฆผ 3>์€ <๊ทธ๋ฆผ 1>์˜ ์ข…์ด๋ฅผ ์ฒ˜์Œ ๋‚˜๋ˆˆ ํ›„์˜ ์ƒํƒœ๋ฅผ, <๊ทธ๋ฆผ 4>๋Š” ๋‘ ๋ฒˆ์งธ ๋‚˜๋ˆˆ ํ›„์˜ ์ƒํƒœ๋ฅผ, <๊ทธ๋ฆผ 5>๋Š” ์ตœ์ข…์ ์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ 9์žฅ์˜ ํ•˜์–€์ƒ‰ ์ƒ‰์ข…์ด์™€ 7์žฅ์˜ ํŒŒ๋ž€์ƒ‰ ์ƒ‰์ข…์ด๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์žˆ๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„ ์ข…์ด์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด N๊ณผ ๊ฐ ์ •์‚ฌ๊ฐํ˜•์นธ์˜ ์ƒ‰(ํ•˜์–€์ƒ‰ ๋˜๋Š” ํŒŒ๋ž€์ƒ‰)์ด ์ฃผ์–ด์งˆ ๋•Œ ์ž˜๋ผ์ง„ ํ•˜์–€์ƒ‰ ์ƒ‰์ข…์ด์™€ ํŒŒ๋ž€์ƒ‰ ์ƒ‰์ข…์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์ „์ฒด ์ข…์ด์˜ ํ•œ ๋ณ€์˜ ๊ธธ์ด N์ด ์ฃผ์–ด์ ธ ์žˆ๋‹ค. N์€ 2, 4, 8, 16, 32, 64, 128 ์ค‘ ํ•˜๋‚˜์ด๋‹ค. ์ƒ‰์ข…์ด์˜ ๊ฐ ๊ฐ€๋กœ์ค„์˜ ์ •์‚ฌ๊ฐํ˜•์นธ๋“ค์˜ ์ƒ‰์ด ์œ—์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ์ค„๊นŒ์ง€ ์ฃผ์–ด์ง„๋‹ค. ํ•˜์–€์ƒ‰์œผ๋กœ ์น ํ•ด์ง„ ์นธ์€ 0, ํŒŒ๋ž€์ƒ‰์œผ๋กœ ์น ํ•ด์ง„ ์นธ์€ 1๋กœ ์ฃผ์–ด์ง€๋ฉฐ, ๊ฐ ์ˆซ์ž ์‚ฌ์ด์—๋Š” ๋นˆ์นธ์ด ํ•˜๋‚˜์”ฉ ์žˆ๋‹ค.

 

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์ž˜๋ผ์ง„ ํ–์–€์ƒ‰ ์ƒ‰์ข…์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ๋‘˜์งธ ์ค„์—๋Š” ํŒŒ๋ž€์ƒ‰ ์ƒ‰์ข…์ด์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

import sys
input = sys.stdin.readline

N = int(input())
colors = []
for n in range(N):
    colors.append(list(map(int, input().split())))

white = 0
blue = 0


def check(start_i, start_j, n):
    global white, blue
    std = colors[start_i][start_j]
    for i in range(start_i, start_i + n):
        for j in range(start_j, start_j + n):
            if std != colors[i][j]:
                check(start_i, start_j, n//2)
                check(start_i, start_j + n//2, n//2)
                check(start_i + n//2, start_j, n//2)
                check(start_i + n//2, start_j + n//2, n//2)
                return
    if std == 0:
        white += 1
    else:
        blue += 1
    return


check(0, 0, N)
print(white)
print(blue)

def check(start_i, start_j, n):
    global white, blue
    std = colors[start_i][start_j]
    for i in range(start_i, start_i + n):
        for j in range(start_j, start_j + n):
            if std != colors[i][j]:
                check(start_i, start_j, n//2)
                check(start_i, start_j + n//2, n//2)
                check(start_i + n//2, start_j, n//2)
                check(start_i + n//2, start_j + n//2, n//2)
                return
    if std == 0:
        white += 1
    else:
        blue += 1
    return

๊ธฐ์ค€๊ฐ’ std์— ๊ฐ€์žฅ ์ฒซ๋ฒˆ์งธ ๊ฐ’ colors[start_i][start_j]๋ฅผ ์ €์žฅํ•œ๋‹ค.

n์€ start๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ๋กœ, start_i๋ถ€ํ„ฐ start_i + n๊นŒ์ง€, start_j๋ถ€ํ„ฐ start_j + n๊นŒ์ง€์˜ ๊ฑฐ๋ฆฌ๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค.

๋งŒ์•ฝ colors[i][j]์˜ ๊ฐ’์ด ๊ธฐ์ค€๊ฐ’ std์™€ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ์ชผ๊ฐœ์•ผํ•œ๋‹ค๋Š” ๋œป์ด๋ฏ€๋กœ

start ๊ฐ’์„ ์กฐ์ ˆํ•˜์—ฌ 4๊ฐœ๋กœ ์ชผ๊ฐœ check๋ฅผ ๋‹ค์‹œ ํ˜ธ์ถœํ•˜์—ฌ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•œ๋‹ค.

๋ฐ˜์œผ๋กœ ์ชผ๊ฐœ๋ฏ€๋กœ n๊ฐ’์€ ํ•ญ์ƒ n//2๊ฐ€ ๋˜์–ด ์žฌ๊ท€์ ์œผ๋กœ ํ˜ธ์ถœํ•˜๊ณ  

๊ฐ๊ฐ์˜ ์ฒซ ๊ธฐ์ค€์ ์„ start_i์™€ start_j๋กœ ์„ค์ •ํ•œ๋‹ค.

 

๋งŒ์•ฝ ๋ฐ˜๋ณต๋ฌธ์„ ๋น ์ ธ๋‚˜์™”๋‹ค๋ฉด ๋ชจ๋“  ์นธ์˜ ์ˆซ์ž๊ฐ€ ๊ธฐ์ค€๊ฐ’๊ณผ ๊ฐ™๋‹ค๋Š” ์˜๋ฏธ์ด๋ฏ€๋กœ

๊ธฐ์ค€๊ฐ’์— ๋”ฐ๋ผ white ๋˜๋Š” blue์˜ ๊ฐ’์„ 1 ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.

 

 

728x90

BELATED ARTICLES

more