-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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() | ||
} |
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. appendLine 배워갑니다! |
||
} | ||
|
||
println(sb) | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
소병희_문자열게임2.solve() | ||
} |
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() | ||
}} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 힌트 안보시고 직접 생각해서 푸신게... 천재같습니다 진짜로 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 인죵 천재! |
||
else break | ||
} | ||
|
||
println(answer) | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
소병희_우체국.solve() | ||
} |
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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-- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 익은 개수를 탐색 마다 확인해주어 별도의 완탐을 줄인 것이 좋습니다!! |
||
} | ||
|
||
days = box[r][c] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} |
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() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
c - 'a'
👍👍