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

728x90

๐Ÿšฉ ๋ธŒ๋ฃจํŠธํฌ์Šค(์™„์ „ํƒ์ƒ‰)

 

thinking

๊ฐ€๋Šฅํ•œ ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๊ตฌํ•œ๋‹ค์Œ, ์ตœ์†Œ๊ฐ’์„ ๊ตฌํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋œฌ๋‹ค.

์ตœ์•…์˜ ๊ฒฝ์šฐ (2N!/N!*N!)*T = 10,400,600*50 = 520,030,000 ๋ฒˆ์˜ ์—ฐ์‚ฐ์„ ํ•ด์„œ ๊ทธ๋Ÿฐ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋ž˜์„œ ์• ์ดˆ์— ๋ถ€๋ถ„ํ•ฉ์ด ๊ฒฐ๊ณผ๊ฐ’๋ณด๋‹ค ํฌ๋ฉด ํ•จ์ˆ˜๊ฐ€ ๋๋‚˜๋„๋ก ์ฒ˜๋ฆฌํ•ด์คฌ๋‹ค.

ํ˜„์žฌ ์ขŒํ‘œ๋ฅผ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋ฐ›์•„, ๋ฐฉ๋ฌธํ•œ์ ์ด ์—†๋‹ค๋ฉด ๋ฐฉ๋ฌธ์ฒดํฌ ํ›„ ํ•ฉ์„ ๋”ํ•˜๋ฉฐ ์žฌ๊ท€์ ์œผ๋กœ ํ•จ์ˆ˜๊ฐ€ ๋Œ์•„๊ฐ€๋„๋ก ๊ตฌ์„ฑํ–ˆ๋‹ค.

 

์ฝ”๋“œ

T = int(input())

dx = [0, 1]
dy = [1, 0]

def dfs(x, y):
    global res, tmp
    if res < tmp:  # ํ˜„์žฌ ๊ฒฐ๊ณผ๊ฐ’ ๋ณด๋‹ค ํฌ๋ฉด ํ•จ์ˆ˜ ๋๋‚˜๋„๋ก -> ์ œํ•œ์‹œ๊ฐ„๋•Œ๋ฌธ์— ๊ฐ€์ง€์น˜๊ธฐ ํ•ด์•ผํ•จ
        return
    if x == N-1 and y == N-1:
        res = tmp
        return
    for d in range(2):
        nx = x + dx[d]
        ny = y + dy[d]
        if nx<0 or nx>=N or ny<0 or ny>=N:
            continue
        if (nx, ny) not in visited:
            visited.append((nx, ny))  # ์ขŒํ‘œ ์—…๋กœ๋“œ
            tmp += a[nx][ny]
            dfs(nx, ny)
            tmp -= a[nx][ny]  # ์›์ƒ๋ณต๊ตฌ
            visited.remove((nx, ny))


for tc in range(1, T+1):
    N = int(input())
    a = [list(map(int, input().split())) for _ in range(N)]
    visited = []
    res = 3000
    tmp = a[0][0]
    dfs(0, 0)  # ํ˜„์žฌ์ขŒํ‘œ
    print("#{} {}".format(tc, res))
๋Œ“๊ธ€