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

[장희직] - 게임, 연산자 끼워넣기, 디스크 컨트롤러, A와 B #177

Merged
merged 4 commits into from
Sep 18, 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
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 `A와 B` {

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())
Comment on lines +16 to +17
Copy link
Contributor

Choose a reason for hiding this comment

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

스트링빌더 아이디어 굿이네요,,,delete해주면 될걸 머리 싸매고 인덱스 계산을 했다니 ㅠㅠ

}

private fun change(): Int {
while (origin.length != goal.length) {
if (goal.last() == 'A') {
goal.deleteAt(goal.lastIndex)
Copy link
Member

Choose a reason for hiding this comment

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

그러게요.. deleteAt 야무지네요

} else {
goal.deleteAt(goal.lastIndex)
goal.reverse()
}
}
Comment on lines +21 to +28
Copy link
Member

Choose a reason for hiding this comment

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

오.. 깔끔깔끔


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

fun main() {
`A와 B`().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
)
Comment on lines +7 to +15
Copy link
Member

Choose a reason for hiding this comment

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

요거 클래스 안에서 선언하셔도 됩니다!


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
}
Comment on lines +88 to +91
Copy link
Member

Choose a reason for hiding this comment

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

이렇게 갱신해줄 수도 있군요

} 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
}
}
Comment on lines +83 to +101
Copy link
Member

Choose a reason for hiding this comment

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

다시 탐색할 가치를 확인하고 다시 queue에 넣어주는 방법은 생각지도 못했는데 대단하십니다

}
}
}
}
}

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]
Comment on lines +22 to +26
Copy link
Member

Choose a reason for hiding this comment

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

병희님이 말씀하신 구조 분해를 써도 괜찮을 것 같습니다!


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--
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.

동의합니다..!!

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