Skip to content

Commit

Permalink
Merge pull request #221 from wellFoundedDevelopers/hyunsoo/54week
Browse files Browse the repository at this point in the history
[전현수] - 항체 인식, 토마토, 우체국, 문자열 게임2
  • Loading branch information
soopeach authored Mar 18, 2024
2 parents 7098019 + 1ea3478 commit 28b8650
Show file tree
Hide file tree
Showing 4 changed files with 342 additions and 0 deletions.
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

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")
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

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()
}

0 comments on commit 28b8650

Please sign in to comment.