Skip to content

Commit

Permalink
Merge pull request #177 from wellFoundedDevelopers/heejik/45week
Browse files Browse the repository at this point in the history
[장희직] - 게임, 연산자 끼워넣기, 디스크 컨트롤러, A와 B
jhg3410 authored Sep 18, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents b2cdd2d + 281e60f commit d5de136
Showing 3 changed files with 218 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/kotlin/heejik/45week/A와 B.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package heejik.`45week`

class `AB` {

private lateinit var origin: StringBuilder
private lateinit var goal: StringBuilder

fun solve() {
setting()
change().run {
println(this)
}
}

private fun setting() {
origin = StringBuilder(readln())
goal = StringBuilder(readln())
}

private fun change(): Int {
while (origin.length != goal.length) {
if (goal.last() == 'A') {
goal.deleteAt(goal.lastIndex)
} else {
goal.deleteAt(goal.lastIndex)
goal.reverse()
}
}

return if (origin.contentEquals(goal)) 1 else 0
}
}

fun main() {
`AB`().solve()
}
110 changes: 110 additions & 0 deletions src/main/kotlin/heejik/45week/게임.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package heejik.`45week`

import kotlin.math.max
import kotlin.math.min
import kotlin.properties.Delegates

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

data class Area(
var type: Int, // 0 -> 안전, 1 -> 위험, 2-> 죽음
var lostLife: Int
)

class 게임 {

var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()
val board = MutableList(501) { MutableList(501) { Area(type = 0, lostLife = 0) } }
val visited = MutableList(501) { MutableList(501) { false } }


fun play() {
setting()
move(_pos = Pos(x = 0, y = 0))
println(if (board[500][500].lostLife == Int.MAX_VALUE) -1 else board[500][500].lostLife)
}

private fun setting() {
board[500][500].lostLife = Int.MAX_VALUE

n = readln().toInt()
repeat(n) {
val (x1, y1, x2, y2) = readln().split(' ').map { it.toInt() }
val minX = min(x1, x2)
val maxX = max(x1, x2)
val minY = min(y1, y2)
val maxY = max(y1, y2)

for (i in minX..maxX) {
for (j in minY..maxY) {
board[i][j].type = 1
}
}
}

m = readln().toInt()
repeat(m) {
val (x1, y1, x2, y2) = readln().split(' ').map { it.toInt() }
val minX = min(x1, x2)
val maxX = max(x1, x2)
val minY = min(y1, y2)
val maxY = max(y1, y2)

for (i in minX..maxX) {
for (j in minY..maxY) {
board[i][j].type = 2
}
}
}
}

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)
private fun move(_pos: Pos) {
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]
val originLostLife = board[x][y].lostLife

if (nx in 0 until 501 && ny in 0 until 501) {
val nextType = board[nx][ny].type
val nextLostLife = board[nx][ny].lostLife

if (nextType == 2) continue
else if (nextType == 1) {
if (visited[nx][ny].not()) {
visited[nx][ny] = true
queue.add(Pos(nx, ny))
board[nx][ny].lostLife = originLostLife + 1
} else if (originLostLife + 1 < nextLostLife) {
queue.add(Pos(nx, ny))
board[nx][ny].lostLife = originLostLife + 1
}
} else if (nextType == 0) {
if (visited[nx][ny].not()) {
visited[nx][ny] = true
queue.add(Pos(nx, ny))
board[nx][ny].lostLife = originLostLife
} else if (originLostLife < nextLostLife) {
queue.add(Pos(nx, ny))
board[nx][ny].lostLife = originLostLife
}
}
}
}
}
}
}

fun main() {
게임().play()
}
72 changes: 72 additions & 0 deletions src/main/kotlin/heejik/45week/연산자 끼워넣기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package heejik.`45week`

import kotlin.math.max
import kotlin.math.min
import kotlin.properties.Delegates

class `연산자 끼워넣기` {

var n by Delegates.notNull<Int>()
lateinit var numbers: List<Int>
lateinit var operators: List<Int>
var maxAnswer = Int.MIN_VALUE
var minAnswer = Int.MAX_VALUE
var plusCount = 0
var minusCount = 0
var multiCount = 0
var divideCount = 0

fun solve() {
n = readln().toInt()
numbers = readln().split(' ').map { it.toInt() }
operators = readln().split(' ').map { it.toInt() }
plusCount = operators[0]
minusCount = operators[1]
multiCount = operators[2]
divideCount = operators[3]

cal(result = numbers[0], count = 1)
println(maxAnswer)
println(minAnswer)

}


private fun cal(result: Int, count: Int) {
if (count == n) {
maxAnswer = max(maxAnswer, result)
minAnswer = min(minAnswer, result)
return
}

if (plusCount > 0) {
plusCount--
cal(result = result + numbers[count], count = count + 1)
plusCount++
}
if (minusCount > 0) {
minusCount--
cal(result = result - numbers[count], count = count + 1)
minusCount++
}
if (multiCount > 0) {
multiCount--
cal(result = result * numbers[count], count = count + 1)
multiCount++
}
if (divideCount > 0) {
divideCount--
if (result < 0) {
cal(result = ((result * -1) / numbers[count]) * -1, count + 1)
} else {
cal(result = (result) / numbers[count], count + 1)
}
divideCount++
}
}
}


fun main() {
`연산자 끼워넣기`().solve()
}

0 comments on commit d5de136

Please sign in to comment.