-
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
[장희직] 표현 가능한 이진트리, 절댓값 힙, 치즈, 치킨 배달 #113
Changes from all commits
3bb771e
796b75f
c136c97
9ddb9a0
b15973b
df2e330
443fab3
d2fa18f
758a979
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,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() | ||
} |
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) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun main() { | ||
`절댓값 힙`().solve() | ||
} |
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 | ||
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. or... 배워갑니다..!! |
||
if (board[nx][ny] == 1) { | ||
board[nx][ny] = -1 | ||
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. 0이든 1이든 -1로 바꿔준 후에 bfs끝나고 -1을 0으로 바꿔주면 visited가 필요 없군여 |
||
cnt++ | ||
} | ||
Comment on lines
+51
to
+54
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로 돌면서 체크할 수 있다는걸 전혀 몰랐네요,,, 👍 |
||
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() | ||
|
||
} |
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() | ||
} |
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 | ||
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. 헐 로그... |
||
} | ||
Comment on lines
+65
to
+67
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. 와 높이 구하기 대박이네유 |
||
} | ||
|
||
|
||
fun main() { | ||
val numbers = longArrayOf(63,111,95) | ||
`표현 가능한 이진트리`().solution(numbers) | ||
} |
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.
구조가 이해되기 쉽고 좋네요!