Skip to content

Commit

Permalink
Merge pull request #222 from wellFoundedDevelopers/byeonghee/54week
Browse files Browse the repository at this point in the history
[소병희] - 항체 인식, 토마토, 우체국, 문자열 게임 2
  • Loading branch information
bngsh authored Mar 18, 2024
2 parents 28b8650 + fb69c2c commit dcfc949
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/main/kotlin/byeonghee/week54/DFS와 BFS.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package byeonghee.week54

class 소병희_DFSBFS {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val (n, m, v) = readLine().split(" ").map { it.toInt() }
val adj = Array(n+1) { IntArray(n+1) }
val sb = StringBuilder()

repeat(m) {
val (a, b) = readLine().split(" ").map { it.toInt() }
adj[a][b] = 1
adj[b][a] = 1
}

fun dfs(p: Int, visited: BooleanArray) {
sb.append(p)
sb.append(" ")

for(i in 1 .. n) {
if (adj[p][i] == 1 && visited[i].not()) {
visited[i] = true
dfs(i, visited)
}
}
}

fun bfs(_p: Int, visited: BooleanArray) {
val q = ArrayDeque<Int>()
q.add(_p)

while(q.isNotEmpty()) {
val p = q.removeFirst()
sb.append(p)
sb.append(" ")
for(i in 1 .. n) {
if (adj[p][i] == 1 && visited[i].not()) {
visited[i] = true
q.add(i)
}
}
}
}

dfs(v, BooleanArray(n+1).apply { this[v] = true })
sb.appendLine()
bfs(v, BooleanArray(n+1).apply { this[v] = true })

println(sb)
}
}
}

fun main() {
소병희_DFSBFS.solve()
}
39 changes: 39 additions & 0 deletions src/main/kotlin/byeonghee/week54/문자열 게임 2.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package byeonghee.week54

class 소병희_문자열게임2 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val tc = readLine().toInt()
val sb = StringBuilder()

for(t in 0 until tc) {
val letter = Array(26) { ArrayDeque<Int>(10_000)}

readLine().forEachIndexed { i, c ->
letter[c - 'a'].add(i)
}

val k = readLine().toInt()
var minDist = 20_000
var maxDist = 0
for(list in letter) {
if (list.size < k) continue
for(i in k-1 until list.size) {
val dist = list[i] - list[i-k+1] + 1
if (dist < minDist) minDist = dist
if (dist > maxDist) maxDist = dist
}
}

if (maxDist == 0) sb.appendLine(-1)
else sb.appendLine("$minDist $maxDist")
}

println(sb)
}
}
}

fun main() {
소병희_문자열게임2.solve()
}
36 changes: 36 additions & 0 deletions src/main/kotlin/byeonghee/week54/스택 수열.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package byeonghee.week54

class 소병희_스택수열 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
val stack = ArrayDeque<Int>(n)
val sb = StringBuilder()
var numToPush = 1

repeat(n) {
val x = readLine().toInt()

while (numToPush <= x) {
stack.addLast(numToPush++)
sb.appendLine("+")
}
if (stack.last() == x) {
stack.removeLast()
sb.appendLine("-")
}

else {
println("NO")
return@with
}
}

println(sb)

}
}

fun main() {
소병희_스택수열.solve()
}}
41 changes: 41 additions & 0 deletions src/main/kotlin/byeonghee/week54/우체국.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package byeonghee.week54

import java.util.PriorityQueue
import kotlin.math.abs

class 소병희_우체국 {
companion object {
fun solve() = with(System.`in`.bufferedReader()) {
val n = readLine().toInt()
var total = 0L
val pq = PriorityQueue<LongArray> { a, b -> (a[0] - b[0]).toInt() }

repeat(n) { i ->
val (x, a) = readLine().split(" ").map { it.toLong() }
pq.add(longArrayOf(x, a))
total += a
}

var people = 0L
var diff = total
var answer = 0L

while(pq.isNotEmpty()) {
val (x, a) = pq.poll()

if (abs(total - a - people * 2) < diff) {
diff = abs(total - a - people * 2)
answer = x
people += a
}
else break
}

println(answer)
}
}
}

fun main() {
소병희_우체국.solve()
}
53 changes: 53 additions & 0 deletions src/main/kotlin/byeonghee/week54/토마토.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package byeonghee.week54

class 소병희_토마토 {
companion object {
const val TOMATO = 1
const val UNRIPE = 0
const val EMPTY = -1

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

fun solve() = with(System.`in`.bufferedReader()) {
val (m, n) = readLine().split(" ").map { it.toInt() }
val box = Array(n) { IntArray(m) }
val q = ArrayDeque<IntArray>(n * m)
var days = 0
var unripe = 0

repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
box[i][j] = v.toInt()
when (box[i][j]) {
TOMATO -> q.add(intArrayOf(i, j))
UNRIPE -> unripe++
}
}
}

while(q.isNotEmpty()) {
val (r, c) = q.removeFirst()
for(d in 0 until 4) {
val nr = r + dr[d]
val nc = c + dc[d]
if (nr !in 0 until n || nc !in 0 until m) continue
if (box[nr][nc] != 0) continue

box[nr][nc] = box[r][c] + 1
q.add(intArrayOf(nr, nc))
unripe--
}

days = box[r][c]
}

if (unripe > 0) println(-1)
else println(days-1)
}
}
}

fun main() {
소병희_토마토.solve()
}
75 changes: 75 additions & 0 deletions src/main/kotlin/byeonghee/week54/항체 인식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package byeonghee.week54

class 소병희_항체인식 {
companion object {
val dr = intArrayOf(-1, 0, 1, 0)
val dc = intArrayOf(0, 1, 0, -1)

fun solve() = with(System.`in`.bufferedReader()) {
val (n, m) = readLine().split(" ").map { it.toInt() }
val before = Array(n) { IntArray(m) }
val after = Array(n) { IntArray(m) }
val visited = Array(n) { BooleanArray(m) }
val q = ArrayDeque<IntArray>(n * m)
var shot = false
var answer = "YES"
var origin = 0
var change = -1

repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
before[i][j] = v.toInt()
}
}

repeat(n) { i ->
readLine().split(" ").forEachIndexed { j, v ->
after[i][j] = v.toInt()
}
}

outer@ for(i in 0 until n) inner@ for(j in 0 until m) {
if (visited[i][j]) continue@inner
if (shot && before[i][j] != after[i][j]) {
answer = "NO"
break@outer
}

origin = before[i][j].also { visited[i][j] = true }
change = after[i][j].also { visited[i][j] = true }
q.add(intArrayOf(i, j))

while(q.isNotEmpty()) {
val (r, c) = q.removeFirst()
for(d in 0 until 4) {
val nr = r + dr[d]
val nc = c + dc[d]
if (nr !in 0 until n || nc !in 0 until m) continue
if (visited[nr][nc]) continue
if (before[nr][nc] == origin && after[nr][nc] == change) {
visited[nr][nc] = true
q.add(intArrayOf(nr, nc))
}
else if (before[nr][nc] != origin) {
continue
}
else {
answer = "NO"
break@outer
}
}
}

if (origin != change) {
shot = true
}
}

println(answer)
}
}
}

fun main() {
소병희_항체인식.solve()
}

0 comments on commit dcfc949

Please sign in to comment.