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, 입국심사, 회장뽑기 #187

Merged
merged 4 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
37 changes: 37 additions & 0 deletions src/main/kotlin/heejik/47week/A와 B 2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package heejik.`47week`

class `A와 B 2` {

fun solve() {
val s = readln()
val t = readln()
val queue = ArrayDeque<String>()
queue.add(t)

while (queue.isNotEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

재귀 대신 큐로 요렇게 푸는 방식도 있겠군요!!

val nowString = queue.removeFirst()
if (nowString.length == s.length) {
if (nowString == s) {
println(1)
return
}
continue
}

if (nowString.last() == 'A') {
queue.add(nowString.dropLast(1))
Copy link
Member

Choose a reason for hiding this comment

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

dropLast()깔끔하니 좋네요..!!

}

if (nowString.first() == 'B') {
queue.add(nowString.reversed().dropLast(1))
Copy link
Contributor

Choose a reason for hiding this comment

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

헉 reversed로 하면 따로 복사하는 함수 없이 재귀 돌릴 수 있었던거였네요

Copy link
Member Author

Choose a reason for hiding this comment

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

어! 아뇨 새로운 배열이 생성되긴 합니다!

val a = listOf(4,3,2)
val b = a.reversed()
println(a)
println(b)
  
output
[4, 3, 2]
[2, 3, 4]

}
}


println(0)
}
}

fun main() {
`A와 B 2`().solve()
}
64 changes: 64 additions & 0 deletions src/main/kotlin/heejik/47week/알고스팟.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package heejik.`47week`

import kotlin.properties.Delegates

class 알고스팟 {

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

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

var m by Delegates.notNull<Int>()
var n by Delegates.notNull<Int>()
private val maze = mutableListOf<List<Int>>()
lateinit var visited: List<MutableList<Boolean>>

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

repeat(m) {
maze.add(readln().toList().map { it.digitToInt() })
Copy link
Member

Choose a reason for hiding this comment

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

chunked안쓰고 digitToInt해도 되는군요 👍 👍

}

findAnswer().run { println(this) }
Copy link
Member

Choose a reason for hiding this comment

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

그냥 println(findAnswer())과 다른가요??

Copy link
Member Author

Choose a reason for hiding this comment

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

같슴다!!
findAnswer 이 중요한 친구고 그에 따른 출력은 부수적인 느낌이라 생각해서
print에 가려지는 게 싫어서 저렇게 했슴다!

}

private fun findAnswer(): Int {
val dequeue = ArrayDeque<Pair<Pos, Int>>()
dequeue.add(Pos(0, 0) to 0)

while (dequeue.isNotEmpty()) {
val (pos, count) = dequeue.removeFirst()

if (pos.x == m - 1 && pos.y == n - 1) {
return count
}

for (i in 0 until 4) {
val nx = pos.x + dx[i]
val ny = pos.y + dy[i]
if (nx in 0 until m && ny in 0 until n) {
if (visited[nx][ny].not()) {
if (maze[nx][ny] == 0) dequeue.addFirst(Pos(nx, ny) to count)
if (maze[nx][ny] == 1) dequeue.addLast(Pos(nx, ny) to count + 1)
visited[nx][ny] = true
}
}
}
}

return Int.MAX_VALUE
}
}

fun main() {
알고스팟().solve()
}
22 changes: 22 additions & 0 deletions src/main/kotlin/heejik/47week/입국심사.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package heejik.`47week`

class 입국심사 {
fun solution(n: Int, times: IntArray): Long {
var minTime : Long = 1L
var maxTime : Long = times.maxOrNull()!!.toLong() * n

while (minTime <= maxTime) {
val midTime = (minTime + maxTime) / 2

var count = 0L
times.forEach { time -> count += midTime / time }

if (count >= n) {
maxTime = midTime - 1
} else {
minTime = midTime + 1
}
}
return minTime
}
}
46 changes: 46 additions & 0 deletions src/main/kotlin/heejik/47week/회장뽑기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package heejik.`47week`

import kotlin.math.min

class 회장뽑기 {

fun solve() {
val n = readln().toInt()
val board = MutableList(n + 1) { MutableList(n + 1) { Int.MAX_VALUE } }
repeat(n + 1) {
board[it][it] = 0
}

while (true) {
val (a, b) = readln().split(' ').map { it.toInt() }
if (a == -1) break
board[a][b] = 1
board[b][a] = 1
}


for (k in 1..n) {
for (i in 1..n) {
for (j in 1..n) {
if (board[i][k] == Int.MAX_VALUE || board[k][j] == Int.MAX_VALUE) continue
board[i][j] = min(board[i][j], board[i][k] + board[k][j])
}
}
}
Comment on lines +22 to +29
Copy link
Member

Choose a reason for hiding this comment

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

플로이드 워셜 깔끔하네요!


val minScore = board.drop(1).minOf { it.filter { it != Int.MAX_VALUE }.max() }
Copy link
Member

Choose a reason for hiding this comment

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

고차함수 활용 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

maxValue 빼주신게 아주 꼼꼼하네요..예외케이스를 생각 못했습니다👀

val candidates = mutableListOf<Int>()

board.forEachIndexed { index, ints ->
if (ints.filter { it != Int.MAX_VALUE }.max() == minScore)
candidates.add(index)
Comment on lines +35 to +36
Copy link
Member

Choose a reason for hiding this comment

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

오 아예 후보자들을 리스트에 모아두면 나중에 호출할 때 편하네요!

}
println("$minScore ${candidates.count()}")
println(candidates.joinToString(" "))
}
}


fun main() {
회장뽑기().solve()
}