Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[전현수] - 알고스팟, A와 B 2, 입국심사, 회장뽑기 #186

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Feat: 알고스팟 추가
soopeach committed Oct 4, 2023
commit 88a4a1fa08a1091f92ac7864739474918525c876
89 changes: 89 additions & 0 deletions src/main/kotlin/hyunsoo/47week/알고스팟.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package hyunsoo.`47week`

/**
*
* <문제>
* [알고스팟](https://www.acmicpc.net/problem/1261)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_알고스팟` {

private data class Position(val x: Int, val y: Int)

private data class Bundle(val pos: Position, val cost: Int) {
constructor(x: Int, y: Int, cost: Int) : this(Position(x, y), cost)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오....! 유용해보이네요 코틀린 생성자도 다 까먹고 있었는데 복습해야겠어요

}

private val dirs = listOf(
Position(1, 0),
Position(0, 1),
Position(-1, 0),
Position(0, -1),
)

fun solution() {

val deque = ArrayDeque<Bundle>()

val (width, height) = readln().split(" ").map { it.toInt() }

val map = Array(height) {
readln().chunked(1).map {
if (it == "1") WALL else it.toInt()
}.toIntArray()

}

val visited = Array(height) {
BooleanArray(width)
}

deque.add(Bundle(0, 0, 0))
visited[0][0] = true

while (deque.isNotEmpty()) {

val (pos, cost) = deque.removeFirst()

map[pos.x][pos.y] = cost
dirs.forEach { dir ->

val nx = pos.x + dir.x
val ny = pos.y + dir.y

if (nx !in 0 until height ||
ny !in 0 until width ||
visited[nx][ny]
) return@forEach

visited[nx][ny] = true

when (map[nx][ny]) {
WALL -> {
deque.addLast(Bundle(nx, ny, cost + 1))
}

EMPTY -> {
deque.addFirst(Bundle(nx, ny, cost))
}
}
}

}

println(map[height - 1][width - 1])
}

companion object {
const val EMPTY = 0
const val WALL = -1
}
}

fun main() {
전현수_알고스팟().solution()
}