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

[전현수] - 괄호 회전하기, 후위 표기식, 캐슬 디펜스 #272

Merged
merged 4 commits into from
Jun 2, 2024
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
69 changes: 69 additions & 0 deletions src/main/kotlin/hyunsoo/65week/괄호 회전하기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package hyunsoo.`65week`

import java.util.*
/**
*
* <문제>
* [괄호 회전하기](https://school.programmers.co.kr/learn/courses/30/lessons/76502)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_괄호_희전하기` {

private val opens = listOf('(', '[', '{')

fun solution(s: String): Int {
var answer = 0

val target = s + s
Copy link
Member

Choose a reason for hiding this comment

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

이렇게 붙여주는거 괜찮네요!!


var start = 0
repeat(s.length) {
if (target.slice(start until start + s.length).isAlright()) answer++
start++
}

return answer
}

private fun String.isAlright(): Boolean {

val stack = Stack<Char>()

this.forEachIndexed { index, ch ->

if (ch in opens) {
stack.add(ch)
}
else {

if (stack.isEmpty()) return false

when (stack.peek()) {
'(' -> {
if (ch == ')') stack.pop()
else return false
}
'[' -> {
if (ch == ']') stack.pop()
else return false
}
'{' -> {
if (ch == '}') stack.pop()
else return false
}
}
}
}

return stack.isEmpty()

}
}

fun main() {
전현수_괄호_희전하기().solution("[](){}")
}
132 changes: 132 additions & 0 deletions src/main/kotlin/hyunsoo/65week/캐슬 디펜스.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package hyunsoo.`65week`

import java.util.LinkedList
import java.util.Queue

/**
*
* <문제>
* [캐슬 디펜스](https://www.acmicpc.net/problem/17135)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_캐슬_디펜스` {

private data class Position(val x: Int, val y: Int)

private data class Bundle(val pos: Position, val range: Int)

private var n = 0
private var m = 0
private var range = 0

private val board = mutableListOf<MutableList<Int>>()

private val pickedNumList = mutableListOf<Int>()

var answer = 0

// 좌 상 우
private val dirs = listOf(
Position(0, -1),
Position(-1, 0),
Position(0, 1),
)
Comment on lines +32 to +37
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

Choose a reason for hiding this comment

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

공감합니다!


fun solution() {

readln().split(" ").map { it.toInt() }.apply {
n = this[0]
m = this[1]
range = this[2]
}

repeat(n) { rowIndex ->
val row = readln().split(" ").map { it.toInt() } as MutableList
board.add(row)
}

for (i in 0..m - 3) {
getCombinations(3, 0, i)
}

println(answer)
}

private fun getCombinations(depth: Int, cnt: Int, startIndex: Int) {

if (depth == cnt) {
check(pickedNumList)
}

for (index in startIndex until m) {
pickedNumList.add(index)
getCombinations(depth, cnt + 1, index + 1)
pickedNumList.removeLast()
}

}

private fun check(archersYList: List<Int>) {

val curBoard = board.deepCopy()

var curEliminatedCnt = 0

for (archerX in n - 1 downTo 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

적이 내려오는 게 아니라 궁수를 올리는 게 적을 내리는 연산을 할 필요도 없고 똑똑하네요


val targetSet = mutableSetOf<Position>()

archersYList.forEach { curArcherY ->

val queue: Queue<Bundle> = LinkedList()

queue.add(Bundle(Position(archerX, curArcherY), 1))

while (queue.isNotEmpty()) {

val (curPos, curRange) = queue.poll()

if (curBoard[curPos.x][curPos.y] == 1) {
targetSet.add(Position(curPos.x, curPos.y))
return@forEach
}

if (curRange < range) {
dirs.forEach dir@{
val nx = curPos.x + it.x
val ny = curPos.y + it.y


if (nx !in 0 until n ||
ny !in 0 until m
) return@dir

queue.add(Bundle(Position(nx, ny), curRange + 1))
}
}
}
Comment on lines +85 to +111
Copy link
Member

Choose a reason for hiding this comment

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

bfs 풀 생각은 못했는데 설명 들어서 좋았습니다!!

}

targetSet.forEach {
curBoard[it.x][it.y] = 0
curEliminatedCnt++
}
}

if (answer < curEliminatedCnt) answer = curEliminatedCnt

}

private fun MutableList<MutableList<Int>>.deepCopy() =
this.map {
it.toMutableList()
}.toMutableList()
}

fun main() {
전현수_캐슬_디펜스().solution()
}
66 changes: 66 additions & 0 deletions src/main/kotlin/hyunsoo/65week/후위 표기식.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package hyunsoo.`65week`

import java.util.*

/**
*
* <문제>
* [후위 표기식](https://www.acmicpc.net/problem/1918)
*
* - 아이디어
*
* - 트러블 슈팅
*
*/
class `전현수_후위_표기식` {
Copy link
Member

Choose a reason for hiding this comment

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

덕분에 어떻게 풀어야하는지 배웠습니다!!


fun solution() {

val s = readln()
val sb = StringBuilder()
val stack = Stack<Char>()

s.forEach { ch ->
when (ch) {
'(' -> {
stack.push('(')
}

'*', '/' -> {
while (stack.isNotEmpty()) {
if (stack.peek() == '*' || stack.peek() == '/') sb.append(stack.pop())
else break
}
stack.push(ch)
}

'+', '-' -> {
while (stack.isNotEmpty()) {
if (stack.peek() == '(') break
sb.append(stack.pop())
}
stack.push(ch)
}

')' -> {
while (stack.isNotEmpty()) {
val fromStack = stack.pop()
if (fromStack == '(') break
sb.append(fromStack)
}
Comment on lines +25 to +50
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

Choose a reason for hiding this comment

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

확실히 조건이 여러 개니 스위치가 깔끔해 보이네요!👍

}

else -> {
sb.append(ch)
}
}
}

while (stack.isNotEmpty()) sb.append(stack.pop())
println(sb)
}
}

fun main() {
전현수_후위_표기식().solution()
}