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

[전현수] - 최소 스패닝 트리, 경쟁적 전염, Coins, 카드 섞기 #258

Merged
merged 4 commits into from
May 13, 2024
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
60 changes: 60 additions & 0 deletions src/main/kotlin/hyunsoo/62week/Coins.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package hyunsoo.`62week`

import kotlin.math.max

/**
*
* <문제>
* [Coins](https://www.acmicpc.net/problem/3067)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_Coins` {

fun solution() {

val testCaseCnt = readln().toInt()
val answerList = mutableListOf<Int>()

repeat(testCaseCnt) {

val n = readln().toInt()
val coins = listOf(0) + readln().split(" ").map { it.toInt() }
val targetMoney = readln().toInt()

val dp = Array(n + 1) {
IntArray(targetMoney + 1)
}

dp[0][0] = 1

// 현재 코인의 인덱스
for (i in 1..n) {
// 목표 금액
for (j in 0..targetMoney) {

dp[i][j] = dp[i - 1][j]

if (j >= coins[i]) {
dp[i][j] += dp[i][j - coins[i]]
}
}

}

answerList.add(dp[n][targetMoney])

}

answerList.forEach {
println(it)
}
}
}

fun main() {
전현수_Coins().solution()
}
80 changes: 80 additions & 0 deletions src/main/kotlin/hyunsoo/62week/경쟁적 전염.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package hyunsoo.`62week`

import java.util.*

/**
*
* <문제>
* [경쟁적 전염](https://www.acmicpc.net/problem/18405)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_경쟁적_전염` {

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

private data class VirusData(val pos: Position, val num: Int, val time: Int)

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

private val board = mutableListOf<MutableList<Int>>()
private val virusNumList = mutableListOf<VirusData>()
fun solution() {

val (n, k) = readln().split(" ").map { it.toInt() }

repeat(n) { rowIndex ->
board.add(readln().split(" ").mapIndexed { colIndex, numString ->
numString.toInt().apply {
if (this != 0) virusNumList.add(
VirusData(Position(rowIndex, colIndex), this, 0)
)
}
} as MutableList)
}

val (time, targetX, targetY) = readln().split(" ").map { it.toInt() }

val queue: Queue<VirusData> = LinkedList()

virusNumList.sortedBy { it.num }.forEach {
queue.add(it)
}

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.

bfs로 풀 수 있군요!! 👍


val (curPos, curNum, curTime) = queue.poll()
Copy link
Member

Choose a reason for hiding this comment

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

curTimequeue 의 원소에 넣으면, 잠시 저장할 tmp 같은 변수가 필요가 없군요!!👍


if (time <= curTime) break
Copy link
Contributor

Choose a reason for hiding this comment

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

continue말고 바로 break해도 되는거였네요,,


dirs.forEach {
val nx = curPos.x + it.x
val ny = curPos.y + it.y

if (nx !in 0 until n ||
ny !in 0 until n ||
board[nx][ny] != 0
) return@forEach

board[nx][ny] = curNum
queue.add(
VirusData(Position(nx, ny), curNum, curTime + 1)
)
}
}

println(board[targetX - 1][targetY - 1])
}
}

fun main() {
전현수_경쟁적_전염().solution()
}
62 changes: 62 additions & 0 deletions src/main/kotlin/hyunsoo/62week/최소 스패닝 트리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package hyunsoo.`62week`

/**
*
* <문제>
* [최소 스패닝 트리](https://www.acmicpc.net/problem/1197)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_최소_스패닝_트리` {

private data class NodeInfo(val start: Int, val end: Int, val cost: Int)

fun solution() {

var sum = 0
val nodeInfoList = mutableListOf<NodeInfo>()
val (v, e) = readln().split(" ").map { it.toInt() }
val parentInfo = IntArray(v + 1) {
it
}

repeat(e) {
val (start, end, cost) = readln().split(" ").map { it.toInt() }
nodeInfoList.add(NodeInfo(start, end, cost))
}

nodeInfoList.sortedBy { it.cost }.forEach {

val (start, end, cost) = it
if (start.hasSameParent(end, parentInfo)) return@forEach

sum += cost
union(start, end, parentInfo)
}

println(sum)
}

private fun findParent(target: Int, parentInfo: IntArray): Int {
if (target == parentInfo[target]) return target
return findParent(parentInfo[target], parentInfo)
}

private fun Int.hasSameParent(other: Int, parentInfo: IntArray) =
findParent(this, parentInfo) == findParent(other, parentInfo)

private fun union(a: Int, b: Int, parentInfo: IntArray) {
val aParent = findParent(a, parentInfo)
val bParent = findParent(b, parentInfo)

if (aParent < bParent) parentInfo[bParent] = aParent
else parentInfo[aParent] = bParent
}
}

fun main() {
전현수_최소_스패닝_트리().solution()
}
65 changes: 65 additions & 0 deletions src/main/kotlin/hyunsoo/62week/카드 섞기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package hyunsoo.`62week`

/**
*
* <문제>
* [카드 섞기](https://www.acmicpc.net/problem/1091)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_카드_섞기` {

fun solution() {

val n = readln().toInt()
val init = readln().split(" ").map { it.toInt() }.toIntArray()
val how = readln().split(" ").map { it.toInt() }
var cnt = 0

val cur = IntArray(n)

init.forEachIndexed { index, num ->
cur[index] = num
}

val temp = IntArray(n)

while (true) {

if (cnt != 0 && init.contentEquals(cur)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

또 고차함수 하나 배워갑니다👍

println(-1)
return
}

if (cur.isValid()) {
println(cnt)
return
}

how.forEachIndexed { index, pos ->
temp[pos] = cur[index]
}

temp.forEachIndexed { index, num ->
cur[index] = num
}

cnt++

}
}

private fun IntArray.isValid(): Boolean {
this.forEachIndexed { index, num ->
if (index % 3 != num) return false
}
return true
}
}

fun main() {
전현수_카드_섞기().solution()
}