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 #222

Merged
merged 6 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
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 소병희_DFS와BFS {
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() {
소병희_DFS와BFS.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)
}
Comment on lines +12 to +14
Copy link
Member

Choose a reason for hiding this comment

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

c - 'a' 👍👍


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")
Copy link
Member

Choose a reason for hiding this comment

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

appendLine 배워갑니다!

}

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
}
Comment on lines +26 to +30
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
Member

Choose a reason for hiding this comment

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

인죵 천재!

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()) {
Copy link
Member

Choose a reason for hiding this comment

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

bfs 안에서 days 나 unripe 을 처리해주는 게 좋았어요! 👍👍

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--
Copy link
Member

Choose a reason for hiding this comment

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

익은 개수를 탐색 마다 확인해주어 별도의 완탐을 줄인 것이 좋습니다!!

}

days = box[r][c]
Copy link
Member

Choose a reason for hiding this comment

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

여기서 바로 days를 업데이트해주는 것이 효율적이겠네요!

}

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