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

[장희직] 표현 가능한 이진트리, 절댓값 힙, 치즈, 치킨 배달 #113

Merged
merged 9 commits into from
Mar 12, 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
2 changes: 1 addition & 1 deletion src/main/kotlin/heejik/24week/도서관.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class 도서관 {
while (books.isNotEmpty()) {
if (books.size >= m) {
val holdingBooks = books.subList(0, m)
distance += holdingBooks.maxOf { it.position.absoluteValue }
distance += holdingBooks.first().position
books.removeAll(holdingBooks)
} else {
distance += books.maxOf { it.position.absoluteValue }
Expand Down
10 changes: 4 additions & 6 deletions src/main/kotlin/heejik/25week/감시.kt
Original file line number Diff line number Diff line change
Expand Up @@ -81,20 +81,18 @@ class 감시 {
}
}
}
rec(0, setOf(), start = 0)
rec(0, setOf(), idx = 0)
}


private fun rec(cnt: Int, monitoredPos: Set<Pos>, start: Int) {
private fun rec(cnt: Int, monitoredPos: Set<Pos>, idx: Int) {
if (cnt == cctvPos.size) {
answer = min(answer, blindSpotSize - monitoredPos.size)
return
}

for (i in start until cctvPos.size) {
for (way in CCTV.values()[room[cctvPos[i].x][cctvPos[i].y] - 1].ways) {
rec(cnt + 1, (monitoredPos + monitor(cctvPos[i], way)), start = i + 1)
}
for (way in CCTV.values()[room[cctvPos[idx].x][cctvPos[idx].y] - 1].ways) {
rec(cnt + 1, (monitoredPos + monitor(cctvPos[idx], way)), idx = idx + 1)
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/main/kotlin/heejik/25week/선수과목.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class 선수과목 {
}
}

semester ++
semester++

while (learnedSubject.size != 0) {
val tmpLearned = mutableListOf<Int>()
Expand All @@ -57,10 +57,8 @@ class 선수과목 {
}
learnedSubject.clear()
tmpLearned.forEach {
if (prerequisiteCount[it] == 0) {
subjectBySemester[it] = semester
learnedSubject.add(it)
}
subjectBySemester[it] = semester
learnedSubject.add(it)
}
semester++
}
Expand Down
68 changes: 68 additions & 0 deletions src/main/kotlin/heejik/26week/맥주 마시면서 걸어가기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package heejik.`26week`

import kotlin.math.abs

class `맥주 마시면서 걸어가기` {

private fun String.toPos() : Pos{
return this.split(' ').map { it.toInt() }.run {
Pos(first(), last())
}
}

data class Pos(
val x: Int,
val y: Int
) {
operator fun minus(other: Pos): Int {
return abs(x - other.x) + abs(y - other.y)
}
}

fun getTestCaseCount() {
val testCaseCount = readln().toInt()
repeat(testCaseCount) {
canGoFestival().run {
println(if (this) "happy" else "sad")
}
}
}

fun canGoFestival(): Boolean {
val n = readln().toInt()
val conveniencePoses = mutableListOf<Pos>()
var homePos = readln().toPos()

repeat(n) {
conveniencePoses.add(readln().toPos())
}
var festivalPos = readln().toPos()

return start(homePos, festivalPos, conveniencePoses)
}

fun start(homePos: Pos, festivalPos: Pos, conveniencePoses: MutableList<Pos>): Boolean {
val visited = MutableList(conveniencePoses.size) { false }
val queue = ArrayDeque<Pos>()
queue.add(homePos)

while (queue.isNotEmpty()) {
val pos = queue.removeFirst()
if (pos - festivalPos <= 1000) {
return true
}
conveniencePoses.forEachIndexed { index, conveniencePos ->
if (pos - conveniencePos <= 1000 && visited[index].not()) {
queue.add(conveniencePos)
visited[index] = true
}
}
}

return false
}
}

fun main() {
`맥주 마시면서 걸어가기`().getTestCaseCount()
}
26 changes: 26 additions & 0 deletions src/main/kotlin/heejik/26week/절댓값 힙.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package heejik.`26week`

import java.util.PriorityQueue
import kotlin.math.absoluteValue

class `절댓값 힙` {

val pq = PriorityQueue(compareBy<Int> { it.absoluteValue }.thenComparator { a, b -> a - b })
fun solve() {
val n = readln().toInt()
repeat(n) {
val x = readln().toInt()
if (x == 0) {
pq.poll().run {
println(this ?: 0)
}
} else {
pq.add(x)
}
Comment on lines +13 to +19
Copy link
Member

Choose a reason for hiding this comment

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

구조가 이해되기 쉽고 좋네요!

}
}
}

fun main() {
`절댓값 힙`().solve()
}
78 changes: 78 additions & 0 deletions src/main/kotlin/heejik/26week/치즈.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package heejik.`26week`

import kotlin.properties.Delegates

class 치즈 {

data class Pos(
val x: Int,
val y: Int
)

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)

var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
val board = mutableListOf<MutableList<Int>>()
fun solve() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}

repeat(n) {
board.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

val answers = mutableListOf<Int>()

while (true) {
val answer = bfs(pos = Pos(0, 0))
if (answer == 0) {
break
}
answers.add(answer)
}
println(answers.size)
println(answers.last())
}

fun bfs(pos: Pos): Int {
var cnt = 0
val queue = ArrayDeque<Pos>()
queue.add(pos)
while (queue.isNotEmpty()) {
val (x, y) = queue.removeFirst()
for (i in dx.indices) {
val nx = x + dx[i]
val ny = y + dy[i]
if ((nx !in 0 until n) or (ny !in 0 until m)) continue
Copy link
Member

Choose a reason for hiding this comment

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

or... 배워갑니다..!!

if (board[nx][ny] == 1) {
board[nx][ny] = -1
Copy link
Contributor

Choose a reason for hiding this comment

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

0이든 1이든 -1로 바꿔준 후에 bfs끝나고 -1을 0으로 바꿔주면 visited가 필요 없군여

cnt++
}
Comment on lines +51 to +54
Copy link
Member

Choose a reason for hiding this comment

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

이걸 bfs로 돌면서 체크할 수 있다는걸 전혀 몰랐네요,,, 👍

if (board[nx][ny] == 0) {
queue.add(Pos(nx, ny))
}
board[nx][ny] = -1
}
}

board.forEachIndexed { x, ints ->
ints.forEachIndexed { y, i ->
if (i == -1) {
board[x][y] = 0
}
}
}


return cnt
}
}

fun main() {
치즈().solve()

}
80 changes: 80 additions & 0 deletions src/main/kotlin/heejik/26week/치킨 배달.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package heejik.`26week`

import kotlin.math.absoluteValue
import kotlin.math.min
import kotlin.properties.Delegates

class `치킨 배달` {

data class Pos(
val x: Int,
val y: Int
) {
operator fun minus(other: Pos) = (x - other.x).absoluteValue + (y - other.y).absoluteValue

}

var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()

var answer = Int.MAX_VALUE
val city = mutableListOf<MutableList<Int>>()
val chickenPoses = mutableListOf<Pos>()
val homePoses = mutableListOf<Pos>()

fun solve() {
setting()
pickChickenStores(start = 0)

println(answer)
}

private fun setting() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}

repeat(n) {
city.add(readln().split(' ').map { it.toInt() }.toMutableList())
}

city.forEachIndexed { x, row ->
row.forEachIndexed { y, i ->
if (i == 2) {
chickenPoses.add(Pos(x, y))
}
if (i == 1) {
homePoses.add(Pos(x, y))
}
}
}
}

private fun pickChickenStores(stores: List<Pos> = listOf(), start: Int) {
if (stores.size == m) {
answer = min(answer, getChickenDistance(stores))
return
}
for (i in start until chickenPoses.size) {
pickChickenStores(stores.plus(chickenPoses[i]), start = i + 1)
}
}

private fun getChickenDistance(stores: List<Pos>): Int {
var sumOfDistance = 0
homePoses.forEach { homePos ->
var minOfDistanceByHome = Int.MAX_VALUE
stores.forEach { chickenPos ->
minOfDistanceByHome = min(minOfDistanceByHome, homePos - chickenPos)
}
sumOfDistance += minOfDistanceByHome
}

return sumOfDistance
}
}

fun main() {
`치킨 배달`().solve()
}
74 changes: 74 additions & 0 deletions src/main/kotlin/heejik/26week/표현 가능한 이진트리.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package heejik.`26week`

import kotlin.math.ln
import kotlin.math.pow

class `표현 가능한 이진트리` {
val answers = mutableListOf<Int>()
var isAdded = false
fun solution(numbers: LongArray): MutableList<Int> {
numbers.forEach {
decimalToBinary(it).setFullBinaryTree().checkBinaryTree()
}
return answers
}

private fun decimalToBinary(number: Long): StringBuilder {
var quotient = number
val binary = StringBuilder()
while (quotient > 0) {
binary.insert(0, quotient % 2)
quotient /= 2
}
return binary
}

private fun StringBuilder.setFullBinaryTree(): StringBuilder {
while ((this.length and this.length + 1) != 0) {
this.insert(0, "0")
}
return this
}

private fun StringBuilder.checkBinaryTree() {
val treeHeight = getHeightOfTree(this.length)
val rootNode = 2.0.pow(treeHeight - 1).toInt() - 1
val offset = treeHeight - 2

isAdded = false
val preAnswerSize = answers.size
checkBinaryTreeBySubTree(rootNode, offset)
if (preAnswerSize == answers.size) answers.add(1)

}


private fun StringBuilder.checkBinaryTreeBySubTree(rootNode: Int, offset: Int) {
if (offset < 0) return
val distanceOfChildTree = 2.0.pow(offset).toInt()
if (this[rootNode] == '0') {
if ((this[rootNode - distanceOfChildTree] != '0') or (this[rootNode + distanceOfChildTree] != '0')) {
if (isAdded.not()) {
answers.add(0)
isAdded = true
}
return
}
}


checkBinaryTreeBySubTree(rootNode - distanceOfChildTree, offset - 1)
checkBinaryTreeBySubTree(rootNode + distanceOfChildTree, offset - 1)
}


fun getHeightOfTree(n: Int): Int {
return (ln(n.toDouble()) / ln(2.0)).toInt() + 1
Copy link
Member

Choose a reason for hiding this comment

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

헐 로그...

}
Comment on lines +65 to +67
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() {
val numbers = longArrayOf(63,111,95)
`표현 가능한 이진트리`().solution(numbers)
}