์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Python

[BOJ/Step10] 2447 : ๋ณ„ ์ฐ๊ธฐ - 10 (Python)

NaNaRin๐Ÿ™ƒ 2021. 2. 17. 21:13

www.acmicpc.net/problem/2447

 

2447๋ฒˆ: ๋ณ„ ์ฐ๊ธฐ - 10

์žฌ๊ท€์ ์ธ ํŒจํ„ด์œผ๋กœ ๋ณ„์„ ์ฐ์–ด ๋ณด์ž. N์ด 3์˜ ๊ฑฐ๋“ญ์ œ๊ณฑ(3, 9, 27, ...)์ด๋ผ๊ณ  ํ•  ๋•Œ, ํฌ๊ธฐ N์˜ ํŒจํ„ด์€ N×N ์ •์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋‹ค. ํฌ๊ธฐ 3์˜ ํŒจํ„ด์€ ๊ฐ€์šด๋ฐ์— ๊ณต๋ฐฑ์ด ์žˆ๊ณ , ๊ฐ€์šด๋ฐ๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์นธ์— ๋ณ„์ด

www.acmicpc.net


 

n์€ 3์˜ ๋ฐฐ์ˆ˜๋กœ ์ž…๋ ฅ๋˜๊ณ , n*n ํฌ๊ธฐ์˜ ์‚ฌ๊ฐํ˜•์œผ๋กœ *์ด ์ถœ๋ ฅ๋œ๋‹ค.

์ „์ฒด ์‚ฌ๊ฐํ˜•์„ 9๊ฐœ๋กœ ๋‚˜๋ˆด์„ ๋•Œ, ์ • ๊ฐ€์šด๋ฐ๋Š” ํ•ญ์ƒ ๋นˆ์นธ, ๋‚˜๋จธ์ง€ 8๊ฐœ ๋ถ€๋ถ„์€ ๋˜ 9๊ฐœ๋กœ ๋‚˜๋ˆ ์ ธ ๊ฐ™์€ ๊ทœ์น™์„ ๋ฐ˜๋ณตํ•œ๋‹ค => ์žฌ๊ท€ํ•จ์ˆ˜

 

1. ์ž…๋ ฅ๋ฐ›์€ n*n ํฌ๊ธฐ์˜ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ์— ๋ฏธ๋ฆฌ '*'์„ ์ฑ„์›Œ๋‘๊ณ  ์ • ๊ฐ€์šด๋ฐ ๋ถ€๋ถ„๋งŒ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ• (1)

- divstar(arrays, x, y, n) : arrays์™€ ํฌ๊ธฐ n, (x, y)์ขŒํ‘œ๊ฐ€ ์ „๋‹ฌ๋˜๋ฉด arrays์˜ (x, y) ์œ„์น˜๋ถ€ํ„ฐ n/3 ํฌ๊ธฐ์˜ 9๊ฐœ ์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ  ์ • ๊ฐ€์šด๋ฐ ์‚ฌ๊ฐํ˜•์€ blank()๋ฅผ, ๋‚˜๋จธ์ง€ ์‚ฌ๊ฐํ˜•์€ ๊ฐ ๊ผญ์ง€์  ์ขŒํ‘œ๋กœ star() ํ˜ธ์ถœ

- star(arrays, x, y, n) : n์ด 1์ด๋ฉด ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  1์ด ์•„๋‹ˆ๋ฉด divstar() ํ˜ธ์ถœ

- blank(arrays, x, y, n) : arrays์˜ (x, y)๋กœ๋ถ€ํ„ฐ n ํฌ๊ธฐ๋งŒํผ์˜ ์‚ฌ๊ฐํ˜•์„ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€๊ฒฝ

# 2447.py

def divstar(arrays, x, y, n):
    star(arrays, x, y, n / 3)
    star(arrays, x, y + n / 3, n / 3)
    star(arrays, x, y + 2 * n / 3, n / 3)
    star(arrays, x + n / 3, y, n / 3)
    blank(arrays, x + n / 3, y + n / 3, n / 3)
    star(arrays, x + n / 3, y + 2 * n / 3, n / 3)
    star(arrays, x + 2 * n / 3, y, n / 3)
    star(arrays, x + 2 * n / 3, y + n / 3, n / 3)
    star(arrays, x + 2 * n / 3, y + 2 * n / 3, n / 3)


def blank(arrays, x, y, n):
    for i in range(int(x), int(x + n)):
        for j in range(int(y), int(y + n)):
            arrays[i][j] = ' '


def star(arrays, x, y, n):
    if n == 1:
        return

    divstar(arrays, x, y, n)


a = int(input())
array = [['*' for i in range(a)] for i in range(a)]

divstar(array, 0, 0, a)

for i in range(a):
    for j in range(a):
        print(array[i][j], end='')
    print()

 

2. ์ž…๋ ฅ๋ฐ›์€ n*n ํฌ๊ธฐ์˜ ์ด์ค‘ ๋ฆฌ์ŠคํŠธ์— ๋ฏธ๋ฆฌ '*'์„ ์ฑ„์›Œ๋‘๊ณ  ์ • ๊ฐ€์šด๋ฐ ๋ถ€๋ถ„๋งŒ ๊ณต๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ• (2)

- star(arrays, x, y, n) : n์ด 1์ด๋ฉด ํ•จ์ˆ˜๋ฅผ ์ข…๋ฃŒ. 1์ด ์•„๋‹ˆ๋ฉด ๊ฐ€์šด๋ฐ ์‚ฌ๊ฐํ˜• ์œ„์น˜๋กœ blank()๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  arrays์˜ (x, y) ์œ„์น˜๋ถ€ํ„ฐ n/3 ํฌ๊ธฐ์˜ 9๊ฐœ ์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜๋ˆ  ๊ฐ ๊ผญ์ง€์  ์ขŒํ‘œ๋กœ star() ํ˜ธ์ถœ

- blank(arrays, x, y, n) : arrays์˜ (x, y)๋กœ๋ถ€ํ„ฐ n ํฌ๊ธฐ๋งŒํผ์˜ ์‚ฌ๊ฐํ˜•์„ ๊ณต๋ฐฑ์œผ๋กœ ๋ณ€๊ฒฝ

# 2447-2.py

def blank(arrays, x, y, n):
    for i in range(int(x), int(x + n)):
        for j in range(int(y), int(y + n)):
            arrays[i][j] = ' '


def star(arrays, x, y, n):
    if n == 1:
        return
    n /= 3
    blank(arrays, x + n, y + n, n)
    for i in range(3):
        for j in range(3):
            star(arrays, x + n * i, y + n * j, n)


a = int(input())
array = [['*' for i in range(a)] for i in range(a)]

star(array, 0, 0, a)

for i in range(a):
    for j in range(a):
        print(array[i][j], end='')
    print()

 

(์‹œ๊ฐ„์ดˆ๊ณผ ๋ฐœ์ƒ.. ์™œ๊ทธ๋Ÿฐ์ง€ ๋ชจ๋ฅด๊ฒ ์Œ ใ…œ)

 

3. ๊ฐ€์žฅ ์ž‘์€ ์‚ฌ๊ฐํ˜•๋ถ€ํ„ฐ ๋งŒ๋“ค์–ด ํฌ๊ธฐ๋ฅผ ํ‚ค์›Œ ๋‚˜๊ฐ€๋Š” ๋ฐฉ๋ฒ•

 

- star(x)

  1. ์ž…๋ ฅ๋ฐ›์€ x๊ฐ€ 1์ด๋ฉด ['*'] ๋ฐ˜ํ™˜ => ๊ฐ€์žฅ ์ž‘์€ ํฌ๊ธฐ์˜ ์‚ฌ๊ฐํ˜•

  2. 3๋ณด๋‹ค ํฌ๋ฉด a = star(x/3) ํ˜ธ์ถœ

  3. ๋ฐ˜ํ™˜๋ฐ›์€ ๋ฆฌ์ŠคํŠธ a๊ฐ€ 3๋ฒˆ / 1๋ฒˆ ๊ณต๋ฐฑ 1๋ฒˆ / 3๋ฒˆ ๋ฐ˜๋ณต๋˜๋ฉด

=> ๋‹ค์Œ ํฌ๊ธฐ์˜ ์‚ฌ๊ฐํ˜•

 

 

 

 

 

 

# 2447-3.py

def star(x):
    if x == 1:
        return ['*']
    x = x // 3
    a = star(x)
    topbottom = [i * 3 for i in a]
    middle = [i + ' ' * x + i for i in a]
    return topbottom + middle + topbottom


n = int(input())
mystar = '\n'.join(star(n))
print(mystar)

 

 

์–ด๋งˆ์–ด๋งˆํ•œ ์ฐจ์ด๊ฐ€ ๋‚œ๋‹ค..