[BOJ][๐ŸŸก4][๋ฐฑ์ค€#02448] ๋ณ„ ์ฐ๊ธฐ - 11

์ž‘์„ฑ:    

์—…๋ฐ์ดํŠธ:

์นดํ…Œ๊ณ ๋ฆฌ:

ํƒœ๊ทธ: , , ,

๋ฌธ์ œ ์ถœ์ฒ˜

BAEKJOON Online Judge #2448


๋ฌธ์ œ

์˜ˆ์ œ๋ฅผ ๋ณด๊ณ  ๊ทœ์น™์„ ์œ ์ถ”ํ•œ ๋’ค์— ๋ณ„์„ ์ฐ์–ด ๋ณด์„ธ์š”.


์ž…๋ ฅ

์ฒซ์งธ ์ค„์— N์ด ์ฃผ์–ด์ง„๋‹ค. N์€ ํ•ญ์ƒ 3ร—2kย ์ˆ˜์ด๋‹ค. (3, 6, 12, 24, 48, โ€ฆ) (0 โ‰ค k โ‰ค 10, k๋Š” ์ •์ˆ˜)


์ถœ๋ ฅ

์ฒซ์งธ ์ค„๋ถ€ํ„ฐ N๋ฒˆ์งธ ์ค„๊นŒ์ง€ ๋ณ„์„ ์ถœ๋ ฅํ•œ๋‹ค.


์˜ˆ์ œ

์ž…๋ ฅ

24


์ถœ๋ ฅ

                       *                        
                      * *                       
                     *****                      
                    *     *                     
                   * *   * *                    
                  ***** *****                   
                 *           *                  
                * *         * *                 
               *****       *****                
              *     *     *     *               
             * *   * *   * *   * *              
            ***** ***** ***** *****             
           *                       *            
          * *                     * *           
         *****                   *****          
        *     *                 *     *         
       * *   * *               * *   * *        
      ***** *****             ***** *****       
     *           *           *           *      
    * *         * *         * *         * *     
   *****       *****       *****       *****    
  *     *     *     *     *     *     *     *   
 * *   * *   * *   * *   * *   * *   * *   * *  
***** ***** ***** ***** ***** ***** ***** *****


My Sol

def foo(ni, nj, M):
    if M == 3:
        k = 0
        while k < 3:
            for j in range(-k, k+1):
                mat[ni+k][nj+j] = '*'
            k += 1
        mat[ni+1][nj]=' '
        return

    H = M//2
    foo(ni, nj, H)
    foo(ni+H, nj-H, H)
    foo(ni+H, nj+H, H)


N = int(input())
mat = [[' ']*(2*N-1) for _ in range(N)]
foo(0, N-1, N)

for lst in mat:
    print(*lst, sep='')

๊ฐ ์‚ผ๊ฐํ˜•์˜ ๋งจ ์œ„์˜ ์ขŒํ‘œ๋ฅผ ๊ธฐ์ค€์ ์œผ๋กœ ๋‘๊ณ , ํŠธ๋ฆฌ ํ˜•์‹์œผ๋กœ ๋‚˜๋‰˜์–ด ๋ฒ”์œ„๊ฐ€ 1/2๋ฐฐ์”ฉ ์ž‘์•„์งˆ ๋•Œ๋งˆ๋‹ค 3๊ฐœ์˜ ๊ธฐ์ค€์ ์œผ๋กœ ๊ฐˆ๋ผ์ง€๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜์˜€๋‹ค. ์ดํ›„, ์ „๋‹ฌํ•ด์ค€ ํฌ๊ธฐ๊ฐ€ 3์ด ๋˜๋ฉด, ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„์˜ ์‚ผ๊ฐํ˜•์ด ๋˜๋ฏ€๋กœ, ์ด๋ฅผ ์ขŒํ‘œ์— ํ‘œ์‹œํ•ด์ค€ ๋’ค, returnํ•˜๋ฉฐ ๊ฐ€์žฅ ์ž‘์€ ์‚ผ๊ฐํ˜•์— ๋Œ€ํ•œ ์ž‘์—…๋“ค์˜ ์ง‘ํ•ฉ์œผ๋กœ ํ’€์–ด์ฃผ๋ฉด ๋˜๊ฒ ๋‹ค.


๊ฒฐ๊ณผ

๋งž์•˜์Šต๋‹ˆ๋‹ค!!


๋ชจ๋ฒ”๋‹ต์•ˆ

์ถœ์ฒ˜

import math
def makeStar(n):
  
  star.append(['  *  ',' * * ','*****'])
  
  for k in range(1,n+1):
    listT = list()
    
    for i in range(len(star[k-1])):
      listT.append(' '*3*2**(k-1) + star[k-1][i] + ' '*3*2**(k-1))
    for i in range(len(star[k-1])):
      listT.append(star[k-1][i] + ' ' + star[k-1][i])

    star.append(listT)
  
star = list()

n = int(input())
n = int(math.log2(n//3))

makeStar(n)

for i in range(len(star[n])):
  print(star[n][i])

์—ฐ์‚ฐ์‹œ๊ฐ„์ด ์ •๋ง ๋น ๋ฅธ ํ’€์ด๊ฐ€ ์žˆ์–ด ๋ถ„์„ํ•ด๋ณด๋ ค ํ•œ๋‹ค. DP๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ณด์ด๊ณ , ๊ฐ€์žฅ ์ž‘์€ ๋‹จ์œ„ ์‚ผ๊ฐํ˜•์„ ๋ฐฐ์—ด star์˜ 0๋ฒˆ ์ธ๋ฑ์Šค์— ์ €์žฅํ•˜์˜€๋‹ค. ์ดํ›„ ๊ทœ์น™์„ฑ์— ๋งž๊ฒŒ ์ˆ˜์ ์œผ๋กœ ๊ณต๋ฐฑ๊ณผ star ๋ฐฐ์—ด ๋‚ด์˜ ๋ณ„์˜ ํฌ๊ธฐ๋ฅผ listT์— ์ €์žฅํ•˜์—ฌ star์˜ ์ƒˆ๋กœ์šด ์ธ๋ฑ์Šค์— ์ถ”๊ฐ€ํ•˜์˜€๋‹ค. ์ฆ‰, ์ธ๋ฑ์Šค๊ฐ€ ์ปค์งˆ ๋•Œ๋งˆ๋‹ค ์ด์ „ ์ธ๋ฑ์Šค์˜ ์‚ผ๊ฐํ˜•์„ ๋‹จ์œ„๋กœ ํ•˜์—ฌ 3๊ฐœ๋ฅผ ๋ชจ์•„ ํฐ ์‚ผ๊ฐํ˜•์„ ๋งŒ๋“œ๋Š” ๋ฐฐ์—ด์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ดํ›„ ์ž…๋ ฅ๊ฐ’์„ 3์œผ๋กœ ๋‚˜๋ˆˆ ๊ฐ’์˜ log2๋ฅผ ์ทจํ•ด ๊ตฌํ•ด ๋ฐ˜๋ณตํšŸ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•ด์ฃผ๊ณ , star ๋ฐฐ์—ด์˜ ๋งจ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค์˜ ์‚ผ๊ฐํ˜•์„ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ