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

[장희직] - 케빈 베이컨의 6단계 법칙, 전구와 스위치, CCW, 함께 블록 쌓기 #172

Merged
merged 4 commits into from
Sep 11, 2023
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
34 changes: 34 additions & 0 deletions src/main/kotlin/heejik/44week/CCW.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package heejik.`44week`


/**
https://wogud6792.tistory.com/11
https://www.youtube.com/watch?v=3GsBaTqxcjM&t=12s
**/

class CCW {

data class Vector(
val x :Int,
val y: Int
)
fun solve() {
val (p1X, p1Y) = readln().split(' ').map { it.toInt() }
val (p2X, p2Y) = readln().split(' ').map { it.toInt() }
val (p3X, p3Y) = readln().split(' ').map { it.toInt() }

val vectorP1P2 = Vector(p2X - p1X, p2Y - p1Y)
val vectorP1P3 = Vector(p3X - p1X, p3Y - p1Y)
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

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

너무 자연스러워서 진짜 있는 클래스인 줄 알았네요


(vectorP1P2.x * vectorP1P3.y - vectorP1P3.x * vectorP1P2.y).run {
if (this < 0) print(-1)
if (this == 0) print(0)
if (this > 0) print(1)
}
Comment on lines +23 to +27
Copy link
Contributor

Choose a reason for hiding this comment

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

이거 아주 좋다고 생각했슴당

}
}


fun main() {
CCW().solve()
}
41 changes: 41 additions & 0 deletions src/main/kotlin/heejik/44week/전구와 스위치.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package heejik.`44week`


import kotlin.math.min

class `전구와 스위치` {
fun solve(state: MutableList<Int>, wantState: List<Int>, n: Int): Int {
val case1 = change(state, wantState, n, 0)
state[0] = (state[0] + 1) % 2
state[1] = (state[1] + 1) % 2
Comment on lines +9 to +10
Copy link
Member

Choose a reason for hiding this comment

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

요 칭구도 좋네요!

val case2 = change(state, wantState, n, 1)

return min(case1, case2)
}

fun change(_state: MutableList<Int>, wantState: List<Int>, n: Int, _count: Int): Int {
val state = mutableListOf<Int>()
_state.forEach { state.add(it) }

var count = _count
state.forEachIndexed { index, bulb ->
if (index == 0) return@forEachIndexed
if (state[index - 1] == wantState[index - 1]) return@forEachIndexed
state[index - 1] = (state[index - 1] + 1) % 2
state[index] = (state[index] + 1) % 2
count++
if (index == n - 1) return@forEachIndexed
state[index + 1] = (state[index + 1] + 1) % 2
}
return (if (state == wantState) count else Int.MAX_VALUE)
}
}

fun main() {
val n = readln().toInt()
val state = readln().map { it.digitToInt() }
val wantState = readln().map { it.digitToInt() }
val answer = `전구와 스위치`().solve(state.toMutableList(), wantState, n)

print(if (answer == Int.MAX_VALUE) -1 else answer)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package heejik.`44week`

import kotlin.properties.Delegates

class `케빈 베이컨의 6단계 법칙` {

var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
lateinit var relations: List<MutableList<Int>>

fun solve() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}
relations = List(size = n + 1) { mutableListOf() }

repeat(m) {
readln().split(' ').map { it.toInt() }.run {
val a = this[0]
val b = this[1]

relations[a].add(b)
relations[b].add(a)
}
}

val answers = MutableList(size = n + 1) { Int.MAX_VALUE }

for (man in 1..n) {
answers[man] = bfs(man)
}
val minCount = answers.min()
println(answers)
Copy link
Contributor

Choose a reason for hiding this comment

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

ㅇ ㅔ ? 디버깅용이었나유?

Copy link
Member Author

Choose a reason for hiding this comment

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

어 이게 왜 같이 올라갔지....?

println(answers.indexOfFirst { it == minCount })
Copy link
Contributor

Choose a reason for hiding this comment

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

오 indexOfFirst 좋네요

Copy link
Member

Choose a reason for hiding this comment

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

요거 참 좋네요

}

private fun bfs(standard: Int): Int {
var totalCount = 0
val findRelations = mutableSetOf<Int>()

val queue = ArrayDeque<Pair<Int, Int>>()
queue.add(Pair(standard, 0))

while (queue.isNotEmpty()) {
val (man, count) = queue.removeFirst()
if (findRelations.add(man)) {
totalCount += count
relations[man].forEach {
if (it !in findRelations) queue.add(Pair(it, count + 1))
}
}
}
Comment on lines +42 to +53
Copy link
Member

Choose a reason for hiding this comment

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

아 저처럼 초기 친구들 안 구하고 이렇게 해도 되겠군요...ㅋㅋㅋㅋㅋㅋㅋㅋ

return totalCount
}
}


fun main() {
`케빈 베이컨의 6단계 법칙`().solve()
}
36 changes: 36 additions & 0 deletions src/main/kotlin/heejik/44week/함께 블록 쌓기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package heejik.`44week`

class `함께 블록 쌓기` {

fun solve() {
val (n, m, h) = readln().split(' ').map { it.toInt() }
val dp = MutableList(size = h + 1) { 0 }
val tmpStored = MutableList(size = h + 1) { 0 }
Comment on lines +7 to +8
Copy link
Member

Choose a reason for hiding this comment

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

1차원 dp 테이블 + 중복을 방지하기 위한 tmp저장소까지.. 다음에 비슷한 방식으로 시도해보겠습니다!

dp[0] = 1

repeat(n) {
val heights = readln().split(' ').map { it.toInt() }
Copy link
Member

Choose a reason for hiding this comment

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

입력받자마자 처리하는게 멋있네요 😎


for (i in 0..h) {
if (dp[i] > 0) {
heights.forEach { height ->
if (i + height > h) return@forEach
tmpStored[i + height] += dp[i]
}
}
}
tmpStored.forEachIndexed { index, i ->
dp[index] += i
dp[index] %= 10007
}
tmpStored.fill(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

헉...fill을 완전 잊고 있었네요 담엔 배열 초기화에 참고하겠습니다!

}

println(dp[h])
}
}


fun main() {
`함께 블록 쌓기`().solve()
}