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

[전현수] - 항체 인식, 토마토, 우체국, 문자열 게임2 #221

Merged
merged 5 commits into from
Mar 18, 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
98 changes: 98 additions & 0 deletions src/main/kotlin/hyunsoo/54week/문자열 게임 2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package hyunsoo.`54week`

/**
*
* <문제>
* [문자열 게임 2](https://www.acmicpc.net/problem/20437)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_문자열_게임_2` {

val br = System.`in`.bufferedReader()

private data class CharWithIndex(val char: Char, val index: Int)

fun solution() {

val t = br.readLine().toInt()

repeat(t) {

var firstConditionResult = Int.MAX_VALUE
var secondConditionResult = -1

val w = br.readLine()
val k = br.readLine().toInt()

val alphabetCounter = IntArray(26)
val validCharList = mutableSetOf<Char>()

val validCharInfoList = mutableListOf<CharWithIndex>()

if (k == 1) {
println("1 1")
return@repeat
}

w.forEachIndexed { index, char ->
val curCharCnt = ++alphabetCounter[char.toSequence()]
if (k <= curCharCnt)
validCharList.add(char)
}

w.forEachIndexed { index, char ->
if (validCharList.contains(char)) {
validCharInfoList.add(CharWithIndex(char, index))
}
}

val sortedValidCharInfoList = validCharInfoList
.sortedWith(
compareBy<CharWithIndex> { it.char }
.thenBy { it.index }
)

for (i in sortedValidCharInfoList.indices) {

val (target, startIndex) = sortedValidCharInfoList[i]
var targetIncludedCnt = 1

for (j in i + 1 until sortedValidCharInfoList.size) {

val (endTarget, endIndex) = sortedValidCharInfoList[j]
if (target != endTarget) {
break
}
targetIncludedCnt++
if (k != targetIncludedCnt) continue

val curLength = endIndex - startIndex + 1

if (curLength < firstConditionResult) firstConditionResult = curLength
if (secondConditionResult < curLength) secondConditionResult = curLength

}
}

if (firstConditionResult == Int.MAX_VALUE || secondConditionResult == -1) {
println(-1)
} else {
println("$firstConditionResult $secondConditionResult")
}

}
}

private fun Char.toSequence(): Int {
return this.code - 97
}

}

fun main() {
전현수_문자열_게임_2().solution()
}
53 changes: 53 additions & 0 deletions src/main/kotlin/hyunsoo/54week/우체국.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package hyunsoo.`54week`

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

private data class VillageInfo(val villageNum: Long, val peopleCnt: Long)

private val villageInfoList = mutableListOf<VillageInfo>()

fun solution() {

var totalPeopleCnt = 0L
var prefixSumOfPeopleCnt = 0L
val n = readln().toInt()

repeat(n) {

val (villageNum, peopleCnt) = readln().split(" ").map { it.toLong() }
villageInfoList.add(VillageInfo(villageNum, peopleCnt))
totalPeopleCnt += peopleCnt

}

val mid = if (totalPeopleCnt % 2 == 0L) totalPeopleCnt / 2 else totalPeopleCnt / 2 + 1
Copy link
Contributor

Choose a reason for hiding this comment

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

이...홀수일 때 1 더하는 걸 알았더라면 식이 좀 더 짧아졌을텐데..

Copy link
Member

Choose a reason for hiding this comment

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

오 아예 mid를 만들어서 처리할 수 있군요!! 👍


villageInfoList
.sortedBy { it.villageNum }
.forEach {

val (vil, curPeopleCnt) = it

prefixSumOfPeopleCnt += curPeopleCnt

if (mid <= prefixSumOfPeopleCnt) {
println(vil)
return
}
}
}
}

fun main() {
전현수_우체국().solution()
}
87 changes: 87 additions & 0 deletions src/main/kotlin/hyunsoo/54week/토마토.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package hyunsoo.`54week`

import java.util.LinkedList
import java.util.Queue

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

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

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

private data class QueueData(val pos: Position, val day: Int)

private val box = mutableListOf<MutableList<Int>>()
private val startPositionList = mutableListOf<Position>()

fun solution() {

val queue: Queue<QueueData> = LinkedList()

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

repeat(m) { rowIndex ->
val row = readln().split(" ")
.mapIndexed { columnIndex, it ->
if (it == "1") startPositionList.add(Position(rowIndex, columnIndex))
it.toInt()
} as MutableList
box.add(row)
}

startPositionList.forEach {
queue.add(QueueData(it, 1))
}

while (queue.isNotEmpty()) {

val (curPos, curDay) = queue.poll()

dirs.forEach { dir ->

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

if (nx !in 0 until m ||
ny !in 0 until n
) return@forEach

if (box[nx][ny] == 0) {

box[nx][ny] = curDay + 1
queue.add(QueueData(Position(nx, ny), curDay + 1))

}
}
}

val result = box.flatten()

if (result.contains(0)) {
println(-1)
} else {
println(result.maxOf { it } - 1)
}

}

}

fun main() {
전현수_토마토().solution()
}
104 changes: 104 additions & 0 deletions src/main/kotlin/hyunsoo/54week/항체 인식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package hyunsoo.`54week`

import kotlin.system.exitProcess

/**
*
* <문제>
* [항체 인식](https://www.acmicpc.net/problem/22352)
*
* - 아이디어
* 영역 별 구분되어있는 모양이 같아야 함.
* 그리고 하나의 영역을 제외하고는 숫자가 같아야 함.
*
* - 트러블 슈팅
*
*/
class `전현수_항체_인식` {

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

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

private val origin = mutableListOf<MutableList<Int>>()
private val expected = mutableListOf<MutableList<Int>>()

fun solution() {

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

repeat(n) {
val row = readln().split(" ").map { it.toInt() }
origin.add(row as MutableList)
}

repeat(n) {
val row = readln().split(" ").map { it.toInt() }
expected.add(row as MutableList)
}

for (i in 0 until origin.size) {
for (j in 0 until origin.first().size) {

val curNumForOrigin = origin[i][j]
val curNumForExpected = expected[i][j]

if (curNumForOrigin != curNumForExpected) {

val visited = Array(n) {
BooleanArray(m)
}

dfs(curNumForOrigin, curNumForExpected, Position(i, j), origin, visited)

if (origin.flatten() == expected.flatten()) println("YES")
Copy link
Contributor

Choose a reason for hiding this comment

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

이 플래튼이 아주 감동적이었습니다 bfs도 한 조직만 돌면 되고

else println("NO")
exitProcess(0)
}
}
}

println("YES")

}

private fun dfs(
targetNum: Int,
expectNumber: Int,
targetPos: Position,
target: MutableList<MutableList<Int>>,
visited: Array<BooleanArray>,
) {

val curNum = target[targetPos.x][targetPos.y]

if (targetNum != curNum) return

visited[targetPos.x][targetPos.y] = true
target[targetPos.x][targetPos.y] = expectNumber
Copy link
Member

Choose a reason for hiding this comment

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

확실히 기존 배열을 업데이트 시키는 로직이 더 깔끔하네요!🙂


dirs.forEach { dir ->

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

if (nx !in 0 until target.size ||
ny !in 0 until target.first().size ||
visited[nx][ny]
) return@forEach

dfs(targetNum, expectNumber, Position(nx, ny), target, visited)
}

}

}

fun main() {
전현수_항체_인식().solution()
}