ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

1520๋ฒˆ: ๋‚ด๋ฆฌ๋ง‰ ๊ธธ

์—ฌํ–‰์„ ๋– ๋‚œ ์„ธ์ค€์ด๋Š” ์ง€๋„๋ฅผ ํ•˜๋‚˜ ๊ตฌํ•˜์˜€๋‹ค. ์ด ์ง€๋„๋Š” ์•„๋ž˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์ง์‚ฌ๊ฐํ˜• ๋ชจ์–‘์ด๋ฉฐ ์—ฌ๋Ÿฌ ์นธ์œผ๋กœ ๋‚˜๋‰˜์–ด์ ธ ์žˆ๋‹ค. ํ•œ ์นธ์€ ํ•œ ์ง€์ ์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ ๊ฐ ์นธ์—๋Š” ๊ทธ ์ง€์ ์˜ ๋†’์ด๊ฐ€ ์“ฐ์—ฌ ์žˆ์œผ๏ฟฝ

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 (0, 0)์—์„œ ์ถœ๋ฐœํ•˜์—ฌ, (m - 1, n - 1)์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ์˜ ์กฐ๊ฑด์€ ๋” ๋‚ฎ์€ ์ง€์ ์œผ๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ชจ๋‘ ๊ตฌํ•˜์—ฌ์•ผ ํ•œ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด ๋‹จ์ˆœํžˆ BFS, DFS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ ๊ฒฝ๋กœ๋ฅผ ํƒ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•ด ์ค‘๋ณต์œผ๋กœ ํƒ์ƒ‰ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ํฌ๋‹ค.

 

 ๋”ฐ๋ผ์„œ, ์žฌ๊ท€ ํ˜ธ์ถœ์„ ํ†ตํ•ด์„œ ๋ฐฉ๋ฌธ ๊ฐ€๋Šฅํ•œ ๊ฒฝ๋กœ์˜ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ํ•œ๋ฒˆ ํƒ์ƒ‰ํ•œ ๊ฒฝ๋กœ๋Š” ๋” ์ด์ƒ ํƒ์ƒ‰ํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ์•ผ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค์ง€ ์•Š๊ณ  ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 ์˜ˆ์ œ ์ž…๋ ฅ 1์€ ์œ„์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด 3๊ฐ€์ง€์˜ ๊ฒฝ๋กœ ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๋‹ค. ์ด๋ฅผ ๊ณ„์‚ฐ ํ•˜๊ธฐ ์œ„ํ•ด dfs๋ฅผ ํ†ตํ•ด (m - 1, n - 1)๊นŒ์ง€ ํƒ์ƒ‰ํ•œ ํ›„์—, ๊ฐ’์„ return ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋๋ถ€ํ„ฐ ๊ฐ’์ด ์˜ฌ๋ผ์˜ค๊ฒŒ ๋  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ ์ด๋ฅผ ์ค‘์ฒฉํ•ด์ฃผ๋ฉด 3์ด๋ผ๋Š” ๊ฒฐ๊ณผ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


def visitable(x, y):
    return 0 <= x < m and 0 <= y < n


def dfs(x, y):
    if x == m - 1 and y == n - 1:
        return 1

    if visited[x][y] == -1:
        visited[x][y] = 0

        for dx, dy in dirs:
            next_x, next_y = x + dx, y + dy

            if visitable(next_x, next_y):
                if graph[next_x][next_y] < graph[x][y]:
                    visited[x][y] += dfs(next_x, next_y)

    return visited[x][y]


if __name__ == '__main__':
    dirs = ((0, 1), (0, -1), (1, 0), (-1, 0))
    m, n = map(int, stdin.readline().split())
    graph = [list(map(int, stdin.readline().split())) for _ in range(m)]
    visited = [[-1] * n for _ in range(m)]

    print(dfs(0, 0))

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€