Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 18290๋ฒˆ - NM๊ณผ K(1) (feat. ๋ธŒ๋ฃจํŠธํฌ์Šค, DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 15. 18:17
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ 

https://www.acmicpc.net/problem/18290

 

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

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

www.acmicpc.net

๐ŸŸ  ๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

์ฒ˜์Œ ๋ณด๊ณ  ์™„์ „ ์ฐธ์ƒ‰์— DFS๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜! ๊นŒ์ง€๋Š” ์ƒ๊ฐ์ด ๋‚ฌ๋‹ค. (์ด์ „์— ๋ฐ˜๋ณตํ•ด์„œ ํ‘ผ N๊ณผ M๋ฌธ์ œ ๋•๋ถ„..)... ๊ทผ๋ฐ,,, ์ด์ œ ๊ทธ ํ›„์— ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ๊ฒƒ์ธ์ง€ ๊ณ ๋ฏผ์ด ํ•„์š”ํ–ˆ๋‹ค. 

๋ฌธ์ œ๋ฅผ ์š”์•ฝํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. 2์ฐจ์› ๋ฐฐ์—ด์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค (์„ธ๋กœ N ๊ฐ€๋กœ M)
  2. ๋ฐฐ์—ด ๋‚ด๋ถ€์—์„œ K๊ฐœ์˜ ์ˆซ์ž๋ฅผ ๋ฝ‘์•„์„œ ํ•ฉ์„๊ตฌํ•  ๊ฑด๋ฐ, ๊ทธ ํ•ฉ์ด ์ตœ๋Œ€๊ฐ€ ๋˜์–ด์•ผ ํ•œ๋‹ค. 
  3. K๊ฐœ๋ฅผ ๋ฝ‘์„๋•Œ ์ธ์ ‘ํ•œ ์ˆซ์ž๋Š” ๊ณ ๋ฅด๋ฉด ์•ˆ๋œ๋‹ค. 

์ผ๋‹จ,,, for๋ฌธ ๋‘๊ฐœ๋ฅผ ๋Œ๋ ค์„œ 2์ฐจ์› ๋ฐฐ์—ด์„ ๋Œ์•„๊ฐ€๋ฉด์„œ ํ•˜๋‚˜์”ฉ ์ „๋ถ€ ํƒ์ƒ‰ํ•ด์ฃผ์–ด์•ผํ•œ๋‹ค๊ณ  ํŒ๋‹จํ–ˆ๊ณ , ๊ทธ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ณผ์ •์—์„œ '์ƒํ•˜์ขŒ์šฐ'๋กœ ๋Œ๋ ค์„œ ๊ทธ ์ฃผ๋ณ€์— visited๋œ ์ด๋ ฅ์ด ์—†๋‹ค๋ฉด! ๋”ํ•ด์ฃผ๊ณ , ๋งŒ์•ฝ ์žˆ๋‹ค๋ฉด ํ•ด๋‹น ์œ„์น˜๋ฅผ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ€๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
์ด๋ ‡๊ฒŒ ํŒ๋‹จ์„ ํ•ด๋ณด๊ณ , ์žฌ๊ท€๋ฅผ ์ด์šฉํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์žฌ๊ท€๊ฐ€ ์–ด๋–ป๊ฒŒ ๋Œ์•„๊ฐ€๋Š”์ง€ ๊ทธ๋ฆผ์œผ๋กœ ๊ทธ๋ ค์„œ ์ƒ๊ฐํ•ด๋ดค๋‹ค.

์˜ˆ์ œ 4๋ฒˆ์œผ๋กœ ํ•œ ๋ฐ”ํ€ด๋งŒ ๋Œ๋ ค๋ณธ ์žฌ๊ท€

์ด๋ ‡๊ฒŒ ๋Œ์•„๊ฐ€๊ฒŒ ๋˜๊ฒ ์ง€? ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์— count ์ฆ‰, depth๊ฐ€ k (์—ฌ๊ธฐ์„ 3)๊ฐ€ ๋˜๋Š” ์ˆœ๊ฐ„ dfs์—์„œ return์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ๋ž˜์„œ dfs๊ฐ€ ์‹œ์ž‘ํ• ๋•Œ depth๋ฅผ ๊ฒ€์‚ฌํ•ด์ฃผ๋ฉด ๋˜๊ฒ ๊ตฌ๋‚˜! ์‹ถ์—ˆ๋‹ค. (N๊ณผ M ์‹œ๋ฆฌ์ฆˆ์˜ ์ฝ”๋“œ์™€ ํ˜๋Ÿฌ๊ฐ€๋Š”๊ฒŒ ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค.

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

return ํ•˜๊ณ , ํ•ด๋‹น ์ง„ํ–‰ํ–ˆ๋˜ ๋‚ด์šฉ์„ ๋‹ค์‹œ ๋ฆฌ์…‹ํ•˜๊ณ  ํƒ์ƒ‰์„ ๋Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— visited์—์„œ false๋ฅผ ํ•ด์ฃผ์—ˆ๋‹ค. 

์ค‘๊ฐ„์— ์žˆ๋Š” tempY๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ๋‹ค.

๋งŒ์•ฝ ์ด์ „ ํ–‰๊ณผ ํ˜„์žฌ ๋ฐ”๋ผ๋ณผ ํ–‰์ด ๊ฐ™๋‹ค๋ฉด ์ด์ „ ์—ด์— ์ด์–ด์„œ ํƒ์ƒ‰ํ•˜๊ณ , ๋งŒ์•ฝ ์ด์ „ ํ–‰๊ณผ ํ˜„์žฌ ๋ฐ”๋ผ๋ณผ ํ–‰์ด ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๊ฒ ๋‹ค๋Š” ์ฝ”๋“œ์ด๋‹ค. ์ฆ‰, (1,2) -> (1,5) -> (3,4) ์—์„œ (1,5) ๊ฐ€ ์‹œ์ž‘์ ์ด ๋  ๋•Œ (1,2) ์™€๋Š” ํ–‰์ด ๊ฐ™์œผ๋ฏ€๋กœ (1,2)๋ฅผ ์ œ์™ธํ•˜๊ณ  ํƒ์ƒ‰ํ•˜๊ฒŒ ๋œ๋‹ค.
(์ฐธ๊ณ : https://minoflower.tistory.com/50)

import Foundation

let nmk = readLine()!.split(separator: " ").map{ Int($0)! }
let n = nmk[0]
let m = nmk[1]
let k = nmk[2]
var arr = [[Int]]()
for _ in 0..<n {
    arr.append(readLine()!.split(separator: " ").map{ Int($0)! })
}
var visited = Array(repeating: Array(repeating: false, count: m), count: n)
var answer = -999999

let dx = [-1, 1, 0, 0] // ์ƒํ•˜์ขŒ์šฐ
let dy = [0, 0, -1, 1]

private func dfs(px: Int, py: Int, depth: Int, maxNum: Int) {
    if depth == k {
        answer = max(maxNum, answer)
        return
    }

    for x in px..<n {
        let tempY = x == px ? py : 0
        for y in tempY..<m {
            if visited[x][y] {
                continue
            }

            var flag = true
            // ํ•ด๋‹น xy ์ƒํ•˜์ขŒ์šฐ์— ํ•˜๋‚˜๋ผ๋„ ๋ฐฉ๋ฌธํ•œ ์ด๋ ฅ์ด ์žˆ์œผ๋ฉด false
            for i in 0..<4 {
                let nx = x + dx[i]
                let ny = y + dy[i]

                if nx >= 0, ny >= 0, nx < n, ny < m {
                    if visited[nx][ny] {
                        flag = false
                    }
                }
            }

            // flag๊ฐ€ ์•„์ง true๋ฉด ์ƒํ•˜์ขŒ์šฐ ์•„๋ฌด๋ฐ๋„ ์•„์ง visited ํ•œ์ ์ด ์—†๋‹ค๋Š” ๋œป
            // ํ•ด๋‹น ์œ„์น˜ (xy)์— ๋ฐฉ๋ฌธํ•ด์„œ answer์— ๋”ํ•ด์ฃผ๊ณ  dfs ๋Œ๋ ค์ฃผ์ž
            if flag {
                visited[x][y] = true
                dfs(px: x, py: y, depth: depth + 1, maxNum: maxNum + arr[x][y])
                visited[x][y] = false
            }
        }
    }
}

dfs(px: 0, py: 0, depth: 0, maxNum: 0)
print(answer)

 

๋ฐ˜์‘ํ˜•