Skip to content

Commit

Permalink
Merge pull request #183 from wellFoundedDevelopers/heejik/46week
Browse files Browse the repository at this point in the history
[장희직] - 숨바꼭질 3, 농장 관리, 빌런 호석
  • Loading branch information
jhg3410 authored Oct 4, 2023
2 parents f28ffdf + 277eb0f commit d2d05fa
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/main/kotlin/heejik/46week/농장 관리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package heejik.`46week`

import kotlin.properties.Delegates

class `농장 관리` {

data class Pos(
val x: Int,
val y: Int
)

val dx = listOf(1, -1, 0, 0, 1, 1, -1, -1)
val dy = listOf(0, 0, 1, -1, 1, -1, 1, -1)

val board = mutableListOf<MutableList<Int>>()
var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
fun solve() {
var answer = 0

readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}

repeat(n) {
board.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

while (true) {
val maxHeight = board.maxOf { it.max() }
if (maxHeight == 0) break

var x = 0
var y = 0

for (i in 0 until n){
for (j in 0 until m) {
if (board[i][j] == maxHeight) {
x = i
y = j
}
}
}
bfs(Pos(x,y))
answer++
}
println(answer)
}

fun bfs(_pos: Pos) {
val queue = ArrayDeque<Pair<Pos,Int>>()
queue.add(_pos to board[_pos.x][_pos.y])
board[_pos.x][_pos.y] = 0

while (queue.isNotEmpty()) {
val (pos, preHeight) = queue.removeFirst()
repeat(dx.size) { i ->
val nx = pos.x + dx[i]
val ny = pos.y + dy[i]
if (nx in 0 until n && ny in 0 until m) {
if (board[nx][ny] != 0 && board[nx][ny] <= preHeight) {
queue.add(Pos(nx, ny) to board[nx][ny])
board[nx][ny] = 0
}
}
}
}
}
}

fun main() {
`농장 관리`().solve()
}
70 changes: 70 additions & 0 deletions src/main/kotlin/heejik/46week/빌런 호석.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package heejik.`46week`

import kotlin.properties.Delegates

class `빌런 호석` {

val reverseCount = listOf(
listOf(0, 4, 3, 3, 4, 3, 2, 3, 1, 2),
listOf(4, 0, 5, 3, 2, 5, 6, 1, 5, 4),
listOf(3, 5, 0, 2, 5, 4, 3, 4, 2, 3),
listOf(3, 3, 2, 0, 3, 2, 3, 2, 2, 1),
listOf(4, 2, 5, 3, 0, 3, 4, 3, 3, 2),
listOf(3, 5, 4, 2, 3, 0, 1, 4, 2, 1),
listOf(2, 6, 3, 3, 4, 1, 0, 5, 1, 2),
listOf(3, 1, 4, 2, 3, 4, 5, 0, 4, 3),
listOf(1, 5, 2, 2, 3, 2, 1, 4, 0, 1),
listOf(2, 4, 3, 1, 2, 1, 2, 3, 1, 0),
)

var answer by Delegates.notNull<Int>()
var max by Delegates.notNull<Int>()
var k by Delegates.notNull<Int>()
var count by Delegates.notNull<Int>()
var _origin by Delegates.notNull<Int>()


fun solve() {
setting()
findAnswer().also {
println(it)
}
}

private fun setting() {
readln().split(' ').map { it.toInt() }.run {
max = this[0]
k = this[1]
count = this[2]
_origin = this[3]
}
}

private fun findAnswer(): Int {
var answer = 0
for (_target in 1..max) {
var cnt = 0
var origin = _origin
var target = _target
if (target == origin) continue

repeat(k) {
cnt += reverseCount[origin % 10][target % 10]
origin /= 10
target /= 10
}


if (cnt <= count) answer++
}

return answer
}
}


fun main() {
`빌런 호석`().solve()
// println(35 / 10)
// println(0 % 10)
}
61 changes: 61 additions & 0 deletions src/main/kotlin/heejik/46week/숨바꼭질 3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package heejik.`46week`

import kotlin.math.min
import kotlin.properties.Delegates

class `숨바꼭질 3` {


val visited = MutableList(100001) { Int.MAX_VALUE }
var n by Delegates.notNull<Int>()
var k by Delegates.notNull<Int>()
fun solve() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
k = this[1]
}

bfs(n).also {
println(it)
}
}

fun bfs(start: Int): Int {
val queue = ArrayDeque<Pair<Int, Int>>()
queue.add(start to 0)
visited[start] = 0

var answer = Int.MAX_VALUE

while (queue.isNotEmpty()) {
val (number, time) = queue.removeFirst()
if (number == k) {
answer = min(answer, time)
}

var multiNumber = number * 2
while (multiNumber <= 100000) {
if (visited[multiNumber] < time) break
queue.addFirst(multiNumber to time)
visited[multiNumber] = time
multiNumber *= 2
}

val upNumber = number + 1
val downNumber = number - 1
if (upNumber in 0..100000 && visited[upNumber] > time + 1) {
visited[upNumber] = time + 1
queue.addLast(upNumber to time + 1)
}
if (downNumber in 0..100000 && visited[downNumber] > time + 1) {
visited[downNumber] = time + 1
queue.addLast(downNumber to time + 1)
}
}
return answer
}
}

fun main() {
`숨바꼭질 3`().solve()
}

0 comments on commit d2d05fa

Please sign in to comment.