ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 11048 ์ด๋™ํ•˜๊ธฐ with Python
    PS 2022. 2. 19. 17:59
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 11048 ์ด๋™ํ•˜๊ธฐ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. Nร—M ํฌ๊ธฐ์˜ ๋ฏธ๋กœ (1 โ‰ค N, M โ‰ค 1,000)

    2. (r, c)์— ์žˆ์œผ๋ฉด, (r+1, c), (r, c+1), (r+1, c+1)๋กœ ์ด๋™๊ฐ€๋Šฅ.

    3. ์ค€๊ทœ๊ฐ€ (N, M)์œผ๋กœ ์ด๋™ํ•  ๋•Œ, ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ์‚ฌํƒ• ๊ฐœ์ˆ˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ

    4. N๊ฐœ ์ค„์—๋Š” ์ด M๊ฐœ์˜ ์ˆซ์ž๊ฐ€ ์ฃผ์–ด์ง€๋ฉฐ, r๋ฒˆ์งธ ์ค„์˜ c๋ฒˆ์งธ ์ˆ˜๋Š” (r, c)์— ๋†“์—ฌ์ ธ ์žˆ๋Š” ์‚ฌํƒ•์˜ ๊ฐœ์ˆ˜์ด๋‹ค.

    5. ์‚ฌํƒ•์˜ ๊ฐœ์ˆ˜๋Š” 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค.

    6. ๋‹ค์ด๋‚˜๋ฏนํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ํ˜•์˜ ๋ฌธ์ œ

    ๐Ÿ–ฅ ์†Œ์Šค ์ฝ”๋“œ

    from sys import stdin
    
    n, m = map(int, stdin.readline().split())
    arr = []
    candy = [[0] * m for _ in range(n)]
    res = 0
    
    for _ in range(n):
        arr.append(list(map(int, stdin.readline().split())))
    
    candy[0][0] = arr[0][0]
    
    for i in range(1, m):
        candy[0][i] = arr[0][i] + candy[0][i - 1]
    
    for i in range(1, n):
        candy[i][0] = arr[i][0] + candy[i - 1][0]
    
    
    for i in range(1, n):
        for j in range(1, m):
            candy[i][j] = max(candy[i - 1][j], candy[i][j - 1], candy[i - 1][j - 1]) + arr[i][j]
    
    print(candy[n - 1][m - 1])

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    4 3
    1 2 3
    6 5 4
    7 8 9
    12 11 10

    ์‹คํ–‰๊ฒฐ๊ณผ

    47

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ํ˜•์˜ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค. ์ค€๊ทœ๊ฐ€ ์ด๋™ํ•˜๋ฉด์„œ ์‚ฌํƒ•์„ ๋ชจ๋‘ ๊ฐ€์ ธ๊ฐˆ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    2. arr ๋ฆฌ์ŠคํŠธ์— ๊ฐ ์‚ฌํƒ•์ด ๋†“์—ฌ์ ธ ์žˆ๋Š” ์ˆซ์ž๋ฅผ ์ž…๋ ฅ๋ฐ›์Šต๋‹ˆ๋‹ค.

    3. candy ๋ฆฌ์ŠคํŠธ๋Š” ์ค€๊ทœ๊ฐ€ ์ฑ™๊ธด ์ˆซ์ž์˜ ์ตœ๋Œ“๊ฐ’์„ ์ €์žฅํ•  ๋ฆฌ์ŠคํŠธ ์ž…๋‹ˆ๋‹ค.

    4. candy[0][0] ์€ arr[0][0] ์ด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.
      ์™œ๋ƒํ•˜๋ฉด ์ค€๊ทœ๋Š” (1, 1)์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

    5. 1๋ถ€ํ„ฐ ๊ฐ๊ฐ n, m ๋งŒํผ ์ด์ค‘ ๋ฐ˜๋ณต๋ฌธ์œผ๋กœ ์ˆœํšŒํ•ด์ค๋‹ˆ๋‹ค.
      candy[i][j] ์˜ ์ˆซ์ž๋ฅผ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.

    6. ์ค€๊ทœ๊ฐ€ ์›€์ง์ผ ์ˆ˜ ์žˆ๋Š” ๋ฐฉํ–ฅ์€ (0, 1), (1, 0), (1, 1) ์ด๋ฉฐ
      ์ด๋™ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ์„œ ์ตœ๋Œ“๊ฐ’์„ ์„ ํƒํ•ด์„œ candy๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  (n - 1, m - 1) ์ธ๋ฑ์Šค์— ์žˆ๋Š” ๊ฐ’์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ์‹ค๋ฒ„ ์ˆ˜์ค€์˜ ๋‹ค์ด๋‚˜๋ฏนํ”„๋กœ๊ทธ๋ž˜๋ฐ ์œ ํ˜•์˜ ๋ฌธ์ œ์˜€๋Š”๋ฐ, ์ด ์œ ํ˜•์„ ํ’€์–ด๋ณธ ์ ์ด ์žˆ๋‹ค๋ฉด ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€์Šต๋‹ˆ๋‹ค.
    2. ๊ทธ๋ฆฌ๋”” ๋ฌธ์ œ๋„ ์ด๋Ÿฐ ๋ฌธ์ œ๊ฐ€ ๋น„์Šทํ•˜๊ฒŒ ์žˆ๋Š”๋ฐ, ์ž…๋ ฅ๋ฐ›์€ N๊ณผ M์˜ ๊ฐ’์„ ๋ณด๊ณ  ์Ž„ํ•œ ๋Š๋‚Œ์„ ๋Š๋ผ๊ณ  ๊ทธ๋ฆฌ๋””๊ฐ€ ์•„๋‹ ๊ฒƒ์ด๋ผ๋Š” ์ƒ๊ฐ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค.
    3. ์ ํ™”์‹์„ ์งœ๋Š”๊ฒŒ ์•„์ง ์–ด๋ ต์Šต๋‹ˆ๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.