[Python - ๋ฐฑ์ค€ 18290] NM๊ณผ K(1)

2021. 4. 14. 12:21ใ†๐Ÿ“š Algorithm/๐Ÿ’ช๐Ÿป Python ๋ฌธ์ œ ํ’€์ด

๋ฐ˜์‘ํ˜•
 

18290๋ฒˆ: NM๊ณผ K (1)

ํฌ๊ธฐ๊ฐ€ N×M์ธ ๊ฒฉ์žํŒ์˜ ๊ฐ ์นธ์— ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜์”ฉ ๋“ค์–ด์žˆ๋‹ค. ์ด ๊ฒฉ์žํŒ์—์„œ ์นธ K๊ฐœ๋ฅผ ์„ ํƒํ•  ๊ฒƒ์ด๊ณ , ์„ ํƒํ•œ ์นธ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋‹จ, ์„ ํƒํ•œ ๋‘ ์นธ์ด ์ธ์ ‘

www.acmicpc.net

๋ฌธ์ œ ์„ค๋ช…

ํฌ๊ธฐ๊ฐ€ N×M์ธ ๊ฒฉ์žํŒ์˜ ๊ฐ ์นธ์— ์ •์ˆ˜๊ฐ€ ํ•˜๋‚˜์”ฉ ๋“ค์–ด์žˆ๋‹ค. ์ด ๊ฒฉ์žํŒ์—์„œ ์นธ K๊ฐœ๋ฅผ ์„ ํƒํ•  ๊ฒƒ์ด๊ณ , ์„ ํƒํ•œ ์นธ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋ ค๊ณ  ํ•œ๋‹ค. ๋‹จ, ์„ ํƒํ•œ ๋‘ ์นธ์ด ์ธ์ ‘ํ•˜๋ฉด ์•ˆ๋œ๋‹ค. rํ–‰ c์—ด์— ์žˆ๋Š” ์นธ์„ (r, c)๋ผ๊ณ  ํ–ˆ์„ ๋•Œ, (r-1, c), (r+1, c), (r, c-1), (r, c+1)์— ์žˆ๋Š” ์นธ์ด ์ธ์ ‘ํ•œ ์นธ์ด๋‹ค.

์ œํ•œ ์‚ฌํ•ญ

  • 1 ≤ N, M ≤ 10
  • 1 ≤ K ≤ min(4, N×M)
  • ๊ฒฉ์žํŒ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋Š” -10,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,000๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ •์ˆ˜์ด๋‹ค.
  • ํ•ญ์ƒ K๊ฐœ์˜ ์นธ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ๋งŒ ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅํ˜•์‹

์ฒซ์งธ ์ค„์— N, M, K๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ๊ฒฉ์žํŒ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

 

์ถœ๋ ฅํ˜•์‹

์„ ํƒํ•œ ์นธ์— ๋“ค์–ด์žˆ๋Š” ์ˆ˜๋ฅผ ๋ชจ๋‘ ๋”ํ•œ ๊ฐ’์˜ ์ตœ๋Œ“๊ฐ’์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ž…์ถœ๋ ฅ ์˜ˆ์‹œ

ํ’€์ด ๊ณผ์ •

๋ฐฑํŠธ๋ž˜ํ‚น์„ ์ด์šฉํ•˜์—ฌ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์˜€๋‹ค. ์ฒ˜์Œ์— ๋ฌธ์ œ๋ฅผ ํ’€๋–„ ์ตœ๋Œ€๊ฐ’์„ ๊ตฌํ•˜๋Š” ๊ฑธ ๋ณด๊ณ  bfs๋กœ ํ’€ ์ˆ˜ ์žˆ์„๊ฑฐ ๊ฐ™์•„์„œ bfs๋กœ ์ ‘๊ทผํ–ˆ์ง€๋งŒ, 2*2 ๋ฐฐ์—ด์€ ๊ฒฐ๊ณผ๊ฐ€ ์ž˜ ๋‚˜์™”์ง€๋งŒ 3๊ฐœ๋ฅผ ๊ณ ๋ฅด๋Š” ๋ฌธ์ œ๋ถ€ํ„ฐ ์ •๋‹ต์— ๊ทผ์ ‘ํ•  ์ˆ˜ ์—†์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ dfs๋กœ ๋‹ค์‹œ ์ ‘๊ทผํ•ด์„œ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. x,y๋ฅผ ๋ชจ๋‘ ์กฐํ•ฉํ•ด์•ผ๋˜๋Š” ๋ฌธ์ œ๋ผ์„œ ๋ณต์žกํ–ˆ๋‹ค. ํ•˜์ง€๋งŒ 2์ฐจ์› for๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ํ’€๊ณ , ์ƒํ•˜์ขŒ์šฐ ์ค‘ ์•„๋ž˜,์˜ค๋ฅธ์ชฝ์œผ๋กœ๋งŒ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์—ฌ ๋ณต์žก์„ฑ์„ ์ค„์˜€๊ณ , ์„ ํƒ๋œ i,j ์˜ ๊ฐ’์— ์ƒํ•˜์ขŒ์šฐ๊ฐ€ ๋ชจ๋‘ False์—ฌ์•ผ ๋˜๋ฏ€๋กœ ์ƒํ•˜์ขŒ์šฐ๋ฅผ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋Š” dxy ๋ฅผ ๋งŒ๋“ค์–ด nx,ny์˜ ๊ฐ’์ด ๋ชจ๋‘ False์ผ ๋•Œ๋งŒ ๋‹ค์Œ dfs๋ฅผ ์‹คํ–‰ํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

def dfs(x, y, cnt, sum_value):
    dxy = [(-1, 0), (1, 0), (0, 1), (0, -1)]
    if cnt == k:
        global max_value
        max_value = max(max_value, sum_value)
        return
    for i in range(x, n):
        for j in range(y if i == x else 0, m):
            if check[i][j]:
                continue
            for dx, dy in dxy:
                nx, ny = i + dx, j + dy
                if 0 <= nx < n and 0 <= ny < m and check[nx][ny]:
                    break
            else:
                check[i][j] = True
                dfs(i, j, cnt + 1, sum_value + item[i][j])
                check[i][j] = False


if __name__ == '__main__':
    n, m, k = map(int, input().split())
    item = []
    for _ in range(n):
        item.append(list(map(int, input().split())))

    max_value = 0
    check = [[False for _ in range(m)] for _ in range(n)]
    dfs(0, 0, 0, 0)
    print(max_value)

 

 

 

๋ฐ˜์‘ํ˜•