๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿบ ๋งค์ผ ์•Œ๊ณ ๋ฆฌ์ฆ˜

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค - ๋“ฑ๊ตฃ๊ธธ

๋ฌธ์ œ

๋ฌผ ์›…๋ฉ์ด๊ฐ€ ์—†๋Š” ์นธ๋งŒ์œผ๋กœ ํ•™๊ต์— ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ผ

์ง‘์€(1,1)์— ํ•™๊ต๋Š” (4,4)์— ์œ„์น˜: ์ด ๋ฌธ์ œ์—์„œ๋Š” ์ธ๋ฑ์Šค๋ฅผ 1๋ถ€ํ„ฐ ์„ผ๋‹ค

์ž…๋ ฅ๊ฐ’

  • m: ๊ฐ€๋กœ ๊ธธ์ด
  • n: ์„ธ๋กœ ๊ธธ์ด
  • puddles: ๋ฌผ์— ์ –์€ ์ง€์—ญ๋“ค ์ขŒํ‘œ

์ œํ•œ์‚ฌํ•ญ

  • 1 <= m, n <= 100 (m๊ณผ n์ด ๋ชจ๋‘ 1์ธ ๊ฒฝ์šฐ๋Š” ์—†๋‹ค)
  • 0 <= len(puddles) <= 10
  • ํ•™๊ต์™€ ์ง‘์€ ๋ฌผ์— ์ž ๊ธฐ์ง€ ์•Š๋Š”๋‹ค.
  • 1000000007๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ผ

๋ฌธ์ œํ’€์ด

  1. ๊ฐ€์žฅ ์ž‘์€ ํ•˜์œ„๋ฌธ์ œ๋Š” 3๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. m๊ณผ n์ด ๊ฐ๊ฐ 2x2, 1x2, 2x1 ์ธ ๊ฒฝ์šฐ.
  2. 1x2๋‚˜ 2x1์ธ ๊ฒฝ์šฐ ๊ฐ๊ฐ ์•„๋ž˜์ชฝ์ด๋‚˜ ์˜ค๋ฅธ์ชฝ์— ๋ฌผ์›…๋ฉ์ด๊ฐ€ ์žˆ๋Š”๊ฐ€ ์—†๋Š”๊ฐ€์— ๋”ฐ๋ผ์„œ ๊ฐˆ ์ˆ˜ ์žˆ๊ณ (๊ฒฝ๋กœ ๊ฐฏ์ˆ˜ 1) ์—†๊ณ (๊ฒฝ๋กœ ๊ฐฏ์ˆ˜ 0)๊ฐ€ ๊ฒฐ์ •๋œ๋‹ค.
  3. 2x2 ์˜ ๊ฒฝ์šฐ ์•„๋ž˜์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ์— ๋ชจ๋‘ ๋ฌผ์›…๋ฉ์ด๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ ๊ฐˆ ์ˆ˜ ์—†๊ณ  (๊ฒฝ๋กœ ๊ฐฏ์ˆ˜ 0), ๋‘˜ ์ค‘ ํ•œ ๊ตฐ๋ฐ์— ๋ฌผ์›…๋ฉ์ด๊ฐ€ ์žˆ์œผ๋ฉด ๋‹ค๋ฅธ ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐˆ ์ˆ˜ ์žˆ๊ณ (๊ฒฝ๋กœ ๊ฐฏ์ˆ˜ 1), ๋ฌผ์›…๋ฉ์ด๊ฐ€ ๋‘˜ ๋‹ค ์—†์œผ๋ฉด ๋‘ ๋ฐฉํ–ฅ ๋ชจ๋‘ ๊ฐˆ ์ˆ˜ ์žˆ๋‹ค (๊ฒฝ๋กœ ๊ฐฏ์ˆ˜ 2).
  4. ์ด๋ฅผ ํ™•์žฅํ•ด์„œ ์˜ค๋ฅธ์ชฝ ํ•˜๋‹จ์— ์œ„์น˜ํ•œ ํ•™๊ต ๊ทผ์ฒ˜ ๋ธ”๋ก๋“ค๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ๊ณ„์‚ฐํ•˜์—ฌ ๋งจ ์™ผ์ชฝ ์ƒ๋‹จ์— ์œ„์น˜ํ•œ ์ง‘๊นŒ์ง€ ๊ณ„์‚ฐํ•ด ์˜ฌ๋ผ์˜ค๋ฉด ๋œ๋‹ค.
MAX = 1000000007

def solution(m, n, puddles):
    memo = [[1 for __ in range(m)] for _ in range(n)]
    for x, y in puddles:
        memo[y-1][x-1] = 0
    for y in reversed(range(n)):
        for x in reversed(range(m)):
            if not memo[y][x] or (x == m-1 and y == n-1):
                continue
            elif x == m - 1:
                memo[y][x] = memo[y+1][x]
            elif y == n - 1:
                memo[y][x] = memo[y][x+1]
            else:
                memo[y][x] = (memo[y+1][x] + memo[y][x+1]) % MAX
    return memo[0][0]

 

์‹œ๊ฐ„๋ณต์žก๋„: O(M*N + P), M๊ณผ N์€ 100์ดํ•˜๋กœ ์ œํ•œ๋˜์–ด์žˆ๊ณ  ๋ฌผ์›…๋ฉ์ด๋„ 10์ดํ•˜๋กœ ์ œํ•œ๋˜์–ด์žˆ์œผ๋ฏ€๋กœ O(1) 

๊ณต๊ฐ„๋ณต์žก๋„: O(1)