Skip to content

Commit

Permalink
Merge pull request #172 from wellFoundedDevelopers/heejik/44week
Browse files Browse the repository at this point in the history
[장희직] - 케빈 베이컨의 6단계 법칙, 전구와 스위치, CCW, 함께 블록 쌓기
  • Loading branch information
jhg3410 authored Sep 11, 2023
2 parents f901d51 + eaaed57 commit a60ece9
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 0 deletions.
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)

(vectorP1P2.x * vectorP1P3.y - vectorP1P3.x * vectorP1P2.y).run {
if (this < 0) print(-1)
if (this == 0) print(0)
if (this > 0) print(1)
}
}
}


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
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)
println(answers.indexOfFirst { it == minCount })
}

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))
}
}
}
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 }
dp[0] = 1

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

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

println(dp[h])
}
}


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

0 comments on commit a60ece9

Please sign in to comment.