Skip to content

Commit

Permalink
Merge pull request #99 from wellFoundedDevelopers/byeonghee/23week
Browse files Browse the repository at this point in the history
[소병희] 센서, 로봇 청소기, 합분해, 보물섬
  • Loading branch information
bngsh authored Feb 19, 2023
2 parents 3d253bf + 6646713 commit 4ee98d8
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/main/kotlin/byeonghee/23week/로봇 청소기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package byeonghee.`23week`

import java.io.*

class 소병희_로봇청소기 {

companion object {
const val DIRTY = 0
const val WALL = 1
const val CLEAR = 2

val dr = arrayOf(-1, 0, 1, 0)
val dc = arrayOf(0, 1, 0, -1)

fun solve() : Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (n, m) = readLine().split(" ").map { it.toInt() }
var (r, c, d) = readLine().split(" ").map { it.toInt() }
val room = Array(n) { IntArray(m) }

for(r in 0 until n) {
readLine().split(" ").forEachIndexed { c, v ->
room[r][c] = v.toInt()
}
}

var stop = false
var back = false
var cleaned = 0
while(stop.not()) {
if (room[r][c] == DIRTY) {
cleaned++
room[r][c] = CLEAR
}

back = true
for (i in 0 until 4) {
d = (d + 3) % 4
if (room[r + dr[d]][c + dc[d]] == DIRTY) {
r += dr[d]
c += dc[d]
back = false
break
}
}
if (back.not()) continue

if (room[r - dr[d]][c - dc[d]] == WALL) {
stop = true
}
else {
r -= dr[d]
c -= dc[d]
}
}

println(cleaned)
}
}
}

fun main() {
소병희_로봇청소기.solve()
}
57 changes: 57 additions & 0 deletions src/main/kotlin/byeonghee/23week/보물섬.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package byeonghee.`23week`

import java.io.*

class 소병희_보물섬 {

companion object {
data class Trip(val r: Int, val c: Int, val dist: Int)

val dr = arrayOf(-1, 0, 1, 0)
val dc = arrayOf(0, 1, 0, -1)

fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (h, w) = readLine().split(" ").map { it.toInt() }
val treasureMap = Array(h + 2) { CharArray(w + 2) { 'W' } }
val queue = ArrayDeque<Trip>()
var answer = 0
var trip: Trip
var nxtR = 0
var nxtC = 0

for(r in 1 until h + 1) {
readLine().forEachIndexed { c, v ->
treasureMap[r][c + 1] = v
}
}

for(r in 1 until h + 2) for(c in 1 until w + 2) {
if (treasureMap[r][c] == 'W') continue
val visited = Array(h + 2) { BooleanArray(w + 2) }
queue.add(Trip(r, c, 0))

while(queue.isNotEmpty()) {
trip = queue.removeFirst()
if (visited[trip.r][trip.c]) continue

answer = maxOf(answer, trip.dist)
visited[trip.r][trip.c] = true

for(d in 0 until 4) {
nxtR = trip.r + dr[d]
nxtC = trip.c + dc[d]
if (treasureMap[nxtR][nxtC] == 'W') continue

queue.add(Trip(nxtR, nxtC, trip.dist + 1))
}
}
}

println(answer)
}
}
}

fun main() {
소병희_보물섬.solve()
}
38 changes: 38 additions & 0 deletions src/main/kotlin/byeonghee/23week/센서.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package byeonghee.`23week`

import java.io.*
import java.util.*

class 소병희_센서 {

companion object {
fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val n = readLine().toInt()
val k = readLine().toInt()
val sensors = readLine().split(" ").map { it.toInt() }.sorted()
var answer = sensors.last() - sensors.first()

if (n == 1) {
println(0)
return
}

val pq = PriorityQueue<Int>(n - 1, Collections.reverseOrder())

for(i in 1 until n) {
pq.add(sensors[i] - sensors[i - 1])
}

for(i in 0 until k - 1) {
if (pq.isEmpty()) break
answer -= pq.poll()
}

println(answer)
}
}
}

fun main() {
소병희_센서.solve()
}
25 changes: 25 additions & 0 deletions src/main/kotlin/byeonghee/23week/합분해.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package byeonghee.`23week`

import java.io.*

class 소병희_합분해 {

companion object {
const val MOD = 1_000_000_000

fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val (n, k) = readLine().split(" ").map { it.toInt() }
val dp = Array(n + 1) { IntArray(k) { 1 } }

for(r in 1 .. n) for(c in 1 until k) {
dp[r][c] = (dp[r - 1][c] + dp[r][c - 1]) % MOD
}

println(dp[n][k - 1])
}
}
}

fun main() {
소병희_합분해.solve()
}

0 comments on commit 4ee98d8

Please sign in to comment.