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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

17070๋ฒˆ: ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 1

์œ ํ˜„์ด๊ฐ€ ์ƒˆ ์ง‘์œผ๋กœ ์ด์‚ฌํ–ˆ๋‹ค. ์ƒˆ ์ง‘์˜ ํฌ๊ธฐ๋Š” N×N์˜ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นธ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ r์€ ํ–‰์˜ ๋ฒˆํ˜ธ, c๋Š” ์—ด์˜

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 NxN ๊ฒฉ์žํŒ์—์„œ ํŒŒ์ดํ”„๋ฅผ ํ•œ์ชฝ ๋ (N, N)์œผ๋กœ ์ด๋™์‹œํ‚ค๋Š” ๋ฐฉ๋ฒ•์˜ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ํŒŒ์ดํ”„๋Š” ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์œผ๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ ์›€์ง์ด๋Š” ๊ฐ๋„๋Š” 45๋„์ด๋‹ค. ์ฆ‰ ๊ฐ€๋กœ์—์„œ ์„ธ๋กœ, ์„ธ๋กœ์—์„œ ๊ฐ€๋กœ๋Š” ์ฆ‰์‹œ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์—†๋‹ค. ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด `DFS`๋กœ ํ’€์—ˆ๋‹ค.

 

from sys import stdin


def visitable(x, y, direction):
    if direction == DIAGONAL:
        return x < n and y < n and \
               graph[x - 1][y] == 0 and graph[x][y - 1] == 0 and graph[x][y] == 0

    return x < n and y < n and graph[x][y] == 0


def dfs(loc, direction):
    global answer

    if loc == [n - 1, n - 1]:
        answer += 1

    for next_dir, delta in enumerate(dirs):
        # 45๋„๋งŒ ํšŒ์ „์‹œํ‚ฌ ์ˆ˜ ์žˆ๊ธฐ์—, ๊ฐ€๋กœ - ์„ธ๋กœ, ์„ธ๋กœ - ๊ฐ€๋กœ๋Š” ๋ฐ”๋กœ ์ „ํ™˜ ๋ถˆ๊ฐ€.
        if (direction == SERO and next_dir == GARO) or \
                (direction == GARO and next_dir == SERO):
            continue

        next_x, next_y = loc[X] + delta[X], loc[Y] + delta[Y]

        if visitable(next_x, next_y, next_dir):
            dfs([next_x, next_y], next_dir)


if __name__ == '__main__':
    X, Y = 0, 1
    GARO, SERO, DIAGONAL = 0, 1, 2
    answer = 0
    dirs = [(0, 1), (1, 0), (1, 1)]
    n = int(stdin.readline())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]
    dfs([0, 1], 0)
    print(answer)

 ์˜ˆ์ œ 1, 2, 3, 4๋Š” ์ •๋‹ต์„ ์ž˜ ์ถœ๋ ฅํ•˜๋Š”๋ฐ ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ฝ”๋“œ์ด๋‹ค. ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 2 ๋ฌธ์ œ๋ฅผ ๋ณด๋‹ˆ, N์˜ ๋ฒ”์œ„๊ฐ€ ์ฆ๊ฐ€ํ•˜์—ฌ `DP`๋กœ ์ ‘๊ทผํ•˜์—ฌ์•ผ ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

 `DP`๋กœ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” (N, N)์— ๋„์ฐฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•จ์œผ๋กœ์จ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

  • DP ๋ฐฐ์—ด์„ `[X][Y][Direction]`์œผ๋กœ ๊ตฌ์„ฑํ•˜์—ฌ, ์ขŒํ‘œ์— ๋”ฐ๋ผ ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์„ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ๋ฐฉ๋ฒ•์„ ๊ตฌํ•œ๋‹ค.
  • ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์— ๋ฒฝ์ด ์—†์–ด์•ผ ํ•œ๋‹ค.
  • ์„ธ๋กœ, ๊ฐ€๋กœ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ด๋™ํ•˜๋Š” ๋ฐฉํ–ฅ์— ๋ฒฝ์ด ์—†์–ด์•ผ ํ•œ๋‹ค.
  • ๊ฐ ๋ฐฉํ–ฅ์˜ ์ ํ™”์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
    • ๊ฐ€๋กœ : `dp[x][y - 1][๊ฐ€๋กœ] + dp[x][y - 1][๋Œ€๊ฐ์„ ]`
    • ์„ธ๋กœ : `dp[x - 1][y][์„ธ๋กœ] + dp[x - 1][y][๋Œ€๊ฐ์„ ]`
    • ๋Œ€๊ฐ์„  : `dp[x - 1][y - 1][๊ฐ€๋กœ] + dp[x - 1][y - 1][์„ธ๋กœ] + dp[x - 1][y - 1][๋Œ€๊ฐ์„ ]`
    • ์ฆ‰ ๊ฐ€๋กœ, ์„ธ๋กœ๋Š” ์ด์ „ ์ขŒํ‘œ์˜ ์ž์‹ ์˜ ๋ฐฉํ–ฅ + ๋Œ€๊ฐ์„ ์—์„œ ๊ฒฝ๋กœ๊ฐ€ ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๊ณ , ๋Œ€๊ฐ์„ ์˜ ๊ฒฝ์šฐ ๋ชจ๋“  ๊ฒฝ์šฐ์—์„œ ๊ฒฝ๋กœ๊ฐ€ ์ง„ํ–‰๋  ์ˆ˜ ์žˆ๋‹ค.
      • 45๋„๋กœ ์›€์ง์ผ ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ฌธ์ œ์˜ ์ œํ•œ์กฐ๊ฑด ๋•Œ๋ฌธ์ด๋‹ค.
  • ์ด์™€ ๊ฐ™์€ ๋กœ์ง์„ ๋ฐ˜๋ณตํ•˜๋ฉด, DP๋กœ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


if __name__ == '__main__':
    GARO, SERO, DIAGONAL = 0, 1, 2
    n = int(stdin.readline())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]

    # [x][y][direction]
    dp = [[[0] * 3 for _ in range(n)] for _ in range(n)]
	
    # ์‹œ์ž‘ ์œ„์น˜ 1๋กœ ์ดˆ๊ธฐํ™”
    dp[0][1][GARO] = 1

    for y in range(2, n):
        if graph[0][y] == 0:
            dp[0][y][GARO] = dp[0][y - 1][GARO]

    for x in range(n):
        for y in range(2, n):
            # ๋Œ€๊ฐ์„ ์œผ๋กœ ์ด๋™ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์ด ๋ฒฝ์ด ์•„๋‹ˆ์–ด์•ผ ํ•จ.
            if graph[x][y] == graph[x][y - 1] == graph[x - 1][y] == 0:
                dp[x][y][DIAGONAL] = \
                    dp[x - 1][y - 1][GARO] + dp[x - 1][y - 1][SERO] + dp[x - 1][y - 1][DIAGONAL]

            if graph[x][y] == 0:
                dp[x][y][GARO] = dp[x][y - 1][GARO] + dp[x][y - 1][DIAGONAL]
                dp[x][y][SERO] = dp[x - 1][y][SERO] + dp[x - 1][y][DIAGONAL]

    print(sum(dp[-1][-1]))

 

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