ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 1913 ๋‹ฌํŒฝ์ด (๊ตฌํ˜„)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 2. 15:39

     

    ๐Ÿ“Œ ๋ฌธ์ œ

    ํ™€์ˆ˜์ธ ์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด, ๋‹ค์Œ๊ณผ ๊ฐ™์ด 1๋ถ€ํ„ฐ N2๊นŒ์ง€์˜ ์ž์—ฐ์ˆ˜๋ฅผ ๋‹ฌํŒฝ์ด ๋ชจ์–‘์œผ๋กœ N×N์˜ ํ‘œ์— ์ฑ„์šธ ์ˆ˜ ์žˆ๋‹ค.

     

    N์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด๋Ÿฌํ•œ ํ‘œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋˜ํ•œ N2 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ฃผ์–ด์กŒ์„ ๋•Œ, ๊ทธ ์ขŒํ‘œ๋„ ํ•จ๊ป˜ ์ถœ๋ ฅํ•˜์‹œ์˜ค. ์˜ˆ๋ฅผ ๋“ค์–ด N=5์ธ ๊ฒฝ์šฐ 6์˜ ์ขŒํ‘œ๋Š” (4,3)์ด๋‹ค.

    ์ž…๋ ฅ

    ์ฒซ์งธ ์ค„์— ํ™€์ˆ˜์ธ ์ž์—ฐ์ˆ˜ N(3 ≤ N ≤ 999)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์œ„์น˜๋ฅผ ์ฐพ๊ณ ์ž ํ•˜๋Š” N2 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ํ•˜๋‚˜ ์ฃผ์–ด์ง„๋‹ค.

    ์ถœ๋ ฅ

    N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ํ‘œ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ๊ฐ ์ค„์— N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋ฅผ ํ•œ ์นธ์”ฉ ๋„์–ด์„œ ์ถœ๋ ฅํ•˜๋ฉด ๋˜๋ฉฐ, ์ž๋ฆฟ์ˆ˜๋ฅผ ๋งž์ถœ ํ•„์š”๊ฐ€ ์—†๋‹ค. N+1๋ฒˆ์งธ ์ค„์—๋Š” ์ž…๋ ฅ๋ฐ›์€ ์ž์—ฐ์ˆ˜์˜ ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‘ ์ •์ˆ˜๋ฅผ ํ•œ ์นธ ๋„์–ด์„œ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    7
    35

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    49 26 27 28 29 30 31
    48 25 10 11 12 13 32
    47 24 9 2 3 14 33
    46 23 8 1 4 15 34
    45 22 7 6 5 16 35
    44 21 20 19 18 17 36
    43 42 41 40 39 38 37
    5 7

     


     

    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    n = int(input())
    m = int(input())
    snail = [[0]*n for _ in range(n)]
    
    # ๊ฐ€์šด๋ฐ 1 ์ฑ„์šฐ๊ธฐ
    x = (n - 1) // 2
    y = (n - 1) // 2
    snail[x][y] = 1
    
    # ๋ฐฉํ–ฅ๋ฒกํ„ฐ (์ƒ, ํ•˜, ์ขŒ, ์šฐ)
    dx = [-1, 1, 0, 0]
    dy = [0, 0, -1, 1]
    
    i = 2
    start = 3
    
    while x != 0 or y != 0:
        while i <= start * start:
            if x == y == (n - 1) // 2: # ์‹œ์ž‘์ ์ด๋ฉด cnt ์„ธํŒ…, ํ•œ ์นธ ์œ„๋กœ
                cnt_up, cnt_down, cnt_left, cnt_right = start, start - 1, start - 1, start - 2
                x += dx[0]
                y += dy[0]
                cnt_up -= 1
    
            elif cnt_right > 0: # ์šฐ
                x += dx[3]
                y += dy[3]
                cnt_right -= 1
    
            elif cnt_down > 0: # ํ•˜
                x += dx[1]
                y += dy[1]
                cnt_down -= 1
    
            elif cnt_left > 0: # ์ขŒ
                x += dx[2]
                y += dy[2]
                cnt_left -= 1
    
            elif cnt_up > 0: # ์ƒ
                x += dx[0]
                y += dy[0]
                cnt_up -= 1
    
            snail[x][y] = i
            i += 1
    
        n -= 2
        start += 2
    
    for j in range(len(snail)):
        print(*snail[j])
        if m in snail[j]:
            mx = j + 1
            my = snail[j].index(m) + 1
    print(mx, my)

    ๐Ÿ’ก Soultion

    ๊ฐ€์šด๋ฐ์—์„œ๋ถ€ํ„ฐ ์ฑ„์›Œ๋‚˜๊ฐ€๋ ค๊ณ  ๊ทธ๋ ค๋ณด๋‹ˆ ์ด๋™ ํŒจํ„ด์ด ํ•ญ์ƒ ๊ฐ™์•˜์Šต๋‹ˆ๋‹ค. ๊ฐ€์šด๋ฐ ์ขŒํ‘œ๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์žก๊ณ  ์‹œ์ž‘์ ์—์„œ๋ถ€ํ„ฐ ์ฑ„์›Œ๋‚˜๊ฐ€๋Š” ์ˆœ์„œ์— ๋งž์ถฐ ์ˆซ์ž๋ฅผ ์ €์žฅํ•ด์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘์ ์€ ๋ณด์ด๋‹ค์‹œํ”ผ ํ•ญ์ƒ ((n-1)//2, (n-1)//2)์ž…๋‹ˆ๋‹ค.

     

    n = 7์ผ ๋•Œ๋ฅผ ๋ด…์‹œ๋‹ค.

     

    1. ๊ฐ€์žฅ ์ž‘์€ ์ •์‚ฌ๊ฐํ˜• ์ฑ„์šฐ๊ธฐ (3x3)
      • n = 7 → ์‹œ์ž‘์  (3, 3)
      • start = 3
      • n = 3์ผ ๋•Œ์˜ ๊ฐ€์žฅ ์ž‘์€ ์ •์‚ฌ๊ฐํ˜•๋ถ€ํ„ฐ ์ฑ„์›Œ์•ผ ํ•˜๋ฏ€๋กœ start = 3์œผ๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž i๊ฐ€ start * start์ธ 9๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ while๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ด๋™์ˆœ์„œ๋Œ€๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์— ์ˆซ์ž๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค.
    2. ์ค‘๊ฐ„ ํฌ๊ธฐ ์ •์‚ฌ๊ฐํ˜• ์ฑ„์šฐ๊ธฐ (5x5)
      • n = 5 → ์‹œ์ž‘์  (2, 2)
      • start = 5
      • n = 5์ผ ๋•Œ์˜ ์ •์‚ฌ๊ฐํ˜•์„ ์ฑ„์›Œ์•ผ ํ•˜๋ฏ€๋กœ start = 5๋กœ ์‹œ์ž‘ํ•˜๊ธฐ ์œ„ํ•ด start += 2๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค. ์‹œ์ž‘์ ์€ (2, 2)๊ฐ€ ๋˜์–ด์•ผ ํ•˜๋Š”๋ฐ, ์‹œ์ž‘์  ๊ณ„์‚ฐ ๊ณต์‹์ด ((n-1)//2, (n-1)//2)์ด๋ฏ€๋กœ ํ˜„์žฌ n๊ฐ’์ธ 7์—์„œ 2๋ฅผ ๋นผ์ฃผ๋Š” n -= 2์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ˆซ์ž i๊ฐ€ start * start์ธ 25๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ while๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ด๋™์ˆœ์„œ๋Œ€๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์— ์ˆซ์ž๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค.
    3. ๊ฐ€์žฅ ํฐ ์ •์‚ฌ๊ฐํ˜• ์ฑ„์šฐ๊ธฐ (7x7)
      • n = 3 → ์‹œ์ž‘์  (1, 1)
      • start = 7
      • n = 7์ผ ๋•Œ์˜ ์ •์‚ฌ๊ฐํ˜•์„ ์ฑ„์›Œ์•ผ ํ•˜๋ฏ€๋กœ start = 7์ด ๋ฉ๋‹ˆ๋‹ค. ์‹œ์ž‘์ ์„ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด n = 3์œผ๋กœ ๋งŒ๋“ค๊ณ , ์ˆซ์ž i๊ฐ€ start * start์ธ 49๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ while๋ฌธ์„ ๋ฐ˜๋ณตํ•˜๋ฉฐ ์ด๋™์ˆœ์„œ๋Œ€๋กœ ์ด๋™ํ•˜๋ฉด์„œ ๋ฐฐ์—ด์— ์ˆซ์ž๋ฅผ ์ฑ„์›๋‹ˆ๋‹ค.
     

    ๋Œ“๊ธ€

Designed by Tistory.