๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฐฑ์ค€

[๋ฐฑ์ค€] Swift 14503 ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ ์‹œ๋ฎฌ๋ ˆ์ด์…˜, DFS

by Jouureee 2022. 4. 18.

๋ฌธ์ œ :

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

 

14503๋ฒˆ: ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ

๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ฒญ์†Œํ•˜๋Š” ์˜์—ญ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ๋กœ๋ด‡ ์ฒญ์†Œ๊ธฐ๊ฐ€ ์žˆ๋Š” ์žฅ์†Œ๋Š” N×M ํฌ๊ธฐ์˜ ์ง์‚ฌ๊ฐํ˜•์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด

www.acmicpc.net

 

ํ’€์ด :

์ฒ˜์Œ์— ํ˜„์žฌ ์œ„์น˜ ํ‘œ์‹œํ•˜๋Š”๋ฐ arr์— 1๊ฐ’์„ ๋„ฃ์–ด์„œ ๊ณ„์† ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์„œ 1์‹œ๊ฐ„๋™์•ˆ ํ—ค๋งธ๋‹ค .. ๊ทธ๋ฆฌ๊ณ  ๋™, ์„œ ๊ตฌ๋ถ„์„ ๋ฐ˜๋Œ€๋กœ ํ•ด์„œ ๋˜ ํ—ค๋งธ๋‹ค ..

๋ฐฉํ–ฅ ๊ฐ’๋งŒ ์ž˜ ๋„ฃ์–ด์ค€๋‹ค๋ฉด DFS ํ‘ธ๋Š” ๋ฐฉ์‹๋Œ€๋กœ ํ’€๋ฉด ๋ ๊ฑฐ ๊ฐ™๋‹ค !

์™ผ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•˜๋ ค๋ฉด ๋ถ -> ์„œ // ์„œ -> ๋‚จ // ๋‚จ -> ๋™ // ๋™ -> ๋ถ์œผ๋กœ ์ด๋™ํ•˜๋ฉด ๋œ๋‹ค. 

๋”ฐ๋ผ์„œ 0 -> 3, 3 -> 2, 2-> 1, 1 -> 0์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

func leftTurn(_ now: Int, _ i: Int) -> Int {
   return (now + 3 - i) % 4 // ๋ถ - 0, ๋™ - 1, ๋‚จ - 2, ์„œ - 3
}

๊ทธ๋ฆฌ๊ณ  ๋˜ ํ›„์ง„ํ• ๋•Œ ๋’ท์ชฝ์ด ์–ด๋””์ธ์ง€๋„ ๊ณ„์‚ฐํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ ํ˜„์žฌ ๋ฐ”๋ผ๋ณด๊ณ  ์žˆ๋Š” ๋ฐฉํ–ฅ์—์„œ ํ›„์ง„ํ•˜๊ฒŒ ๋˜๋ฉด

ํ˜„์žฌ : ํ›„์ง„ 

๋‚จ์ชฝ -> ๋ถ์ชฝ

์„œ์ชฝ -> ๋™์ชฝ

 

๋ถ์ชฝ -> ๋‚จ์ชฝ

๋™์ชฝ -> ์„œ์ชฝ

์œผ๋กœ ์ด๋™ํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ๊ตฌํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

let backIdx = now > 1 ? now - 2: now + 2

 

Swift ์ฝ”๋“œ :

//
//  main.swift
//  SOMA๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป
//
//  Created by JoSoJeong on 2022/04/18.
//

import Foundation

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

var line = readLine()!.split(separator: " ").map { Int(String($0))! }
var n = line[0]
var m = line[1]

var line2 = readLine()!.split(separator: " ").map { Int(String($0))! }
var r = line2[0]
var c = line2[1]
var dir = line2[2]

var cnt = 1 // ํ˜„์žฌ๊นŒ์ง€ cnt
var arr: [[Int]] = Array(repeating: [], count: n)
var visit: [[Bool]] = Array(repeating: Array(repeating: false, count: m), count: n)

for i in 0..<n {
    let l = readLine()!.split(separator: " ").map { Int(String($0))! }
    arr[i] = l
}

//ํ˜„์žฌ ์œ„์น˜ ๋ฐฉ๋ฌธ
visit[r][c] = true


func leftTurn(_ now: Int, _ i: Int) -> Int {
   return (now + 3 - i) % 4 // ๋ถ - 0, ๋™ - 1, ๋‚จ - 2, ์„œ - 3
}

func dfs_clean(_ cnt: Int, _ now: Int, _ x: Int, _ y: Int) {
    for i in 0..<4{
        let nextD = leftTurn(now, i)
        let nextX = dx[nextD] + x
        let nextY = dy[nextD] + y
        if (nextX >= n || nextX < 0 || nextY >= m || nextY < 0) { continue }
        if (visit[nextX][nextY] == true || arr[nextX][nextY] == 1) { continue }
        visit[nextX][nextY] = true
        dfs_clean(cnt + 1, nextD, nextX, nextY)
    }
    
    //4๋ฒˆ ์—ฐ์† ์‹คํ–‰์„ ๋๋‚ด๊ณ 
    //๋ฐ”๋กœ ๋’ค๊ฐ€ ๋ฒฝ์ด๋ฉด ๋ฉˆ์ถค
    // ์•„๋‹ˆ๋ฉด ํ›„์ง„
    
    let backIdx = now > 1 ? now - 2: now + 2
    let backX = dx[backIdx] + x
    let backY = dy[backIdx] + y

    if backX < n || backX > 0 || backY < m || backY > 0  {
        if arr[backX][backY] == 1 {
            print(cnt)
            exit(0)
        }else {
            dfs_clean(cnt, now, backX, backY) //ํ›„์ง„ ์‹œ ๋ฐฉํ–ฅ์€ ๋ฐ”๋€Œ์ง€ x
        }
    }
}

dfs_clean(cnt, dir, r, c)

๋Œ“๊ธ€