[๋ฐฑ์ค€/python] 1992๋ฒˆ : ์ฟผ๋“œํŠธ๋ฆฌ

2021. 5. 12. 23:20

๋ฌธ์ œ

ํ‘๋ฐฑ ์˜์ƒ์„ ์••์ถ•ํ•˜์—ฌ ํ‘œํ˜„ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์ฟผ๋“œ ํŠธ๋ฆฌ(Quad Tree)๋ผ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค. ํฐ ์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” 0๊ณผ ๊ฒ€์€ ์ ์„ ๋‚˜ํƒ€๋‚ด๋Š” 1๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์˜์ƒ(2์ฐจ์› ๋ฐฐ์—ด)์—์„œ ๊ฐ™์€ ์ˆซ์ž์˜ ์ ๋“ค์ด ํ•œ ๊ณณ์— ๋งŽ์ด ๋ชฐ๋ ค์žˆ์œผ๋ฉด, ์ฟผ๋“œ ํŠธ๋ฆฌ์—์„œ๋Š” ์ด๋ฅผ ์••์ถ•ํ•˜์—ฌ ๊ฐ„๋‹จํžˆ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฃผ์–ด์ง„ ์˜์ƒ์ด ๋ชจ๋‘ 0์œผ๋กœ๋งŒ ๋˜์–ด ์žˆ์œผ๋ฉด ์••์ถ• ๊ฒฐ๊ณผ๋Š” "0"์ด ๋˜๊ณ , ๋ชจ๋‘ 1๋กœ๋งŒ ๋˜์–ด ์žˆ์œผ๋ฉด ์••์ถ• ๊ฒฐ๊ณผ๋Š” "1"์ด ๋œ๋‹ค. ๋งŒ์•ฝ 0๊ณผ 1์ด ์„ž์—ฌ ์žˆ์œผ๋ฉด ์ „์ฒด๋ฅผ ํ•œ ๋ฒˆ์— ๋‚˜ํƒ€๋‚ด์ง€๋ฅผ ๋ชปํ•˜๊ณ , ์™ผ์ชฝ ์œ„, ์˜ค๋ฅธ์ชฝ ์œ„, ์™ผ์ชฝ ์•„๋ž˜, ์˜ค๋ฅธ์ชฝ ์•„๋ž˜, ์ด๋ ‡๊ฒŒ 4๊ฐœ์˜ ์˜์ƒ์œผ๋กœ ๋‚˜๋ˆ„์–ด ์••์ถ•ํ•˜๊ฒŒ ๋˜๋ฉฐ, ์ด 4๊ฐœ์˜ ์˜์—ญ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๊ด„ํ˜ธ ์•ˆ์— ๋ฌถ์–ด์„œ ํ‘œํ˜„ํ•œ๋‹ค

์œ„ ๊ทธ๋ฆผ์—์„œ ์™ผ์ชฝ์˜ ์˜์ƒ์€ ์˜ค๋ฅธ์ชฝ์˜ ๋ฐฐ์—ด๊ณผ ๊ฐ™์ด ์ˆซ์ž๋กœ ์ฃผ์–ด์ง€๋ฉฐ, ์ด ์˜์ƒ์„ ์ฟผ๋“œ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์••์ถ•ํ•˜๋ฉด "(0(0011)(0(0111)01)1)"๋กœ ํ‘œํ˜„๋œ๋‹ค.  N ×N ํฌ๊ธฐ์˜ ์˜์ƒ์ด ์ฃผ์–ด์งˆ ๋•Œ, ์ด ์˜์ƒ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

 

์ž…๋ ฅ

์ฒซ์งธ ์ค„์—๋Š” ์˜์ƒ์˜ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆซ์ž N ์ด ์ฃผ์–ด์ง„๋‹ค. N ์€ ์–ธ์ œ๋‚˜ 2์˜ ์ œ๊ณฑ์ˆ˜๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1 ≤ N ≤ 64์˜ ๋ฒ”์œ„๋ฅผ ๊ฐ€์ง„๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ๋Š” ๊ธธ์ด N์˜ ๋ฌธ์ž์—ด์ด N๊ฐœ ๋“ค์–ด์˜จ๋‹ค. ๊ฐ ๋ฌธ์ž์—ด์€ 0 ๋˜๋Š” 1์˜ ์ˆซ์ž๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ์˜์ƒ์˜ ๊ฐ ์ ๋“ค์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

์ถœ๋ ฅ

์˜์ƒ์„ ์••์ถ•ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

 

์ •๋‹ต

import sys
input = sys.stdin.readline

N = int(input())
video = []
for _ in range(N):
    line = input().rstrip()
    video.append(line)


def check(start1, end1, start2, end2):
    part1 = [[start1, start1 + (end1-start1) // 2],
             [start1 + (end1-start1) // 2, end1]]
    part2 = [[start2, start2 + (end2-start2) // 2],
             [start2 + (end2-start2) // 2, end2]]

    result = "("

    for a in part1:
        for b in part2:
            diff = False
            bfr = video[a[0]][b[0]]  # ์ฒซ๋ฒˆ์งธ ๊ฐ’ ์ €์žฅ
            for i in range(a[0], a[1]):
                for j in range(b[0], b[1]):
                    if bfr != video[i][j]:  # ๋ชจ๋‘ ๊ฐ™์€์ง€ ํ™•์ธ
                        diff = True
                        break
                if diff == True:    # ๋‹ค๋ฅธ ์ˆซ์ž ๋ฐœ๊ฒฌ, ์žฌ๊ท€ ํ˜ธ์ถœ
                    bfr = check(a[0], a[1], b[0], b[1])
                    break
            result += str(bfr)

    result += ")"
    return result


answer = check(0, N, 0, N)
# ๋ชจ๋‘ ๊ฐ™์€ ์ˆซ์ž๋กœ๋งŒ ๋˜์–ด์žˆ์„ ๋•Œ ๊ด„ํ˜ธ์—†์ด ์ˆซ์ž๋งŒ answer์— ์ €์žฅ
if len(answer) == 6 and answer[1] == answer[2] == answer[3] == answer[4]:
    answer = answer[1]
print(answer)

์žฌ๊ท€ํ•จ์ˆ˜ ํ˜ธ์ถœ์„ ์œ„ํ•ด check๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•œ๋‹ค.

def check(start1, end1, start2, end2):
    part1 = [[start1, start1 + (end1-start1) // 2],
             [start1 + (end1-start1) // 2, end1]]
    part2 = [[start2, start2 + (end2-start2) // 2],
             [start2 + (end2-start2) // 2, end2]]

    result = "("

    for a in part1:
        for b in part2:
            diff = False
            bfr = video[a[0]][b[0]]  # ์ฒซ๋ฒˆ์งธ ๊ฐ’ ์ €์žฅ
            for i in range(a[0], a[1]):
                for j in range(b[0], b[1]):
                    if bfr != video[i][j]:  # ๋ชจ๋‘ ๊ฐ™์€์ง€ ํ™•์ธ
                        diff = True
                        break
                if diff == True:    # ๋‹ค๋ฅธ ์ˆซ์ž ๋ฐœ๊ฒฌ, ์žฌ๊ท€ ํ˜ธ์ถœ
                    bfr = check(a[0], a[1], b[0], b[1])
                    break
            result += str(bfr)

    result += ")"
    return result

part1์—๋Š” ์ด์ฐจ์›๋ฐฐ์—ด์˜ ์ฒซ๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ๋“ค์–ด๊ฐˆ ์ˆซ์ž์˜ ๋ฒ”์œ„๋“ค์„ ์ €์žฅํ•˜๊ณ 

part2์—๋Š” ์ด์ฐจ์›๋ฐฐ์—ด์˜ ๋‘๋ฒˆ์งธ ์ธ๋ฑ์Šค์— ๋“ค์–ด๊ฐˆ ์ˆซ์ž์˜ ๋ฒ”์œ„๋“ค์„ ์ €์žฅํ•œ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด N=4์ผ ๊ฒฝ์šฐ

[0~1][0~1]

[0~1][2~3]

[2~3][0~1]

[2~3][2~3] ์œผ๋กœ 4๊ตฌ์—ญ์œผ๋กœ ๋‚˜๋‰˜๊ฒŒ ๋˜๋ฏ€๋กœ

part1์—๋Š” [0,1][2,3]์ด, part2์—๋Š” [0,1][2,3]์ด ๋“ค์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค.

 

๊ฐ™์€ ๊ตฌ์—ญ์— ๋‹ค๋ฅธ ์ˆซ์ž๊ฐ€ ์žˆ๋‹ค๋ฉด diff์— True๋ฅผ ์ €์žฅํ•˜์—ฌ ์žฌ๊ท€ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

 

answer = check(0, N, 0, N)
# ๋ชจ๋‘ ๊ฐ™์€ ์ˆซ์ž๋กœ๋งŒ ๋˜์–ด์žˆ์„ ๋•Œ ๊ด„ํ˜ธ์—†์ด ์ˆซ์ž๋งŒ answer์— ์ €์žฅ
if len(answer) == 6 and answer[1] == answer[2] == answer[3] == answer[4]:
    answer = answer[1]
print(answer)

์œ„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด return๊ฐ’์ด ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆซ์ž ํ˜•ํƒœ๊ฐ€ ๋˜๊ณ 

๊ด„ํ˜ธ ์•ˆ์˜ ์ˆซ์ž๋Š” 4๊ฐœ ์ด์ƒ์ด ๋œ๋‹ค.

๋งŒ์•ฝ ๊ด„ํ˜ธ ์•ˆ์˜ ์ˆซ์ž๊ฐ€ 4๊ฐœ๊ณ  ๋ชจ๋‘ ๊ฐ™์€ ์ˆซ์ž๋ผ๋ฉด ๊ด„ํ˜ธ ์—†์ด ์ˆซ์ž๋งŒ ์ถœ๋ ฅ๋˜์–ด์•ผํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด (1111)์ด๋ผ๋ฉด 1๋กœ ์ถœ๋ ฅ๋˜์–ด์•ผํ•œ๋‹ค.

๋”ฐ๋ผ์„œ ์œ„์™€๊ฐ™์€ ์˜ˆ๋ฅผ if๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ๊ฑธ๋Ÿฌ์ค€๋‹ค.

 

728x90

BELATED ARTICLES

more