Skip to content

Commit

Permalink
Merge pull request #272 from wellFoundedDevelopers/hyunsoo/65week
Browse files Browse the repository at this point in the history
[전현수] - 괄호 회전하기, 후위 표기식, 캐슬 디펜스
  • Loading branch information
soopeach authored Jun 2, 2024
2 parents 76c682a + 08b9548 commit 6ac4d09
Show file tree
Hide file tree
Showing 3 changed files with 267 additions and 0 deletions.
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

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

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

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

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 `전현수_후위_표기식` {

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

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

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

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

0 comments on commit 6ac4d09

Please sign in to comment.