본문 바로가기
코딩연습

[백준] 알파벳 (1987)(Kotlin)

by 유줘니 2020. 4. 7.

원본 문제 : https://www.acmicpc.net/problem/1987

문제 참고 : https://hyeooona825.tistory.com/65

 

<첫번째>

import java.io.BufferedReader
import java.io.InputStreamReader

var R: Int = 0
var C: Int = 0

var check: BooleanArray = BooleanArray(26)
var map: Array<IntArray> = arrayOf()
var visited: Array<BooleanArray> = arrayOf()

val dx: IntArray = intArrayOf(1, -1, 0, 0)
val dy: IntArray = intArrayOf(0, 0, 1, -1)

fun main() = with(BufferedReader(InputStreamReader(System.`in`))) {

    val input = readLine().split(" ")
    R = input[0].toInt()
    C = input[1].toInt()

    map = Array(R) { IntArray(C) }
    visited = Array(R) { BooleanArray(C) }

    for (i in 0 until R) {
        val tmp = readLine()
        for (j in 0 until C) {
            map[i][j] = tmp[j] - 'A'
        }
    }

    visited[0][0] = true
    check[map[0][0]] = true
    println(dfs(0, 0))

}

private fun dfs(x: Int, y: Int): Int {

    var cnt = 0
    for (i in 0 until 4) {

        val nextR = x + dx[i]
        val nextC = y + dy[i]

        if (nextR >= 0 && nextR < R && nextC >= 0 && nextC < C && !visited[nextR][nextC]) {
            if (!check[map[nextR][nextC]]) {
                visited[nextR][nextC] = true
                check[map[nextR][nextC]] = true
                cnt = Math.max(cnt, dfs(nextR, nextC))
                visited[nextR][nextC] = false
            }
        }
    }
    check[map[x][y]] = false
    visited[x][y] = false
    return cnt + 1
}

'코딩연습' 카테고리의 다른 글

[백준] N-Queen (9663)(Kotlin)  (0) 2020.03.18
[백준] 로또 (6603)(Kotlin)  (0) 2020.02.26
[백준] 퇴사 (14501)(Kotlin)  (0) 2020.02.26
[백준] 회의실배정 (1931)(Kotlin)  (0) 2020.02.20
[백준] 동전 0 (11047)(Kotlin)  (0) 2020.02.20

댓글