-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #177 from wellFoundedDevelopers/heejik/45week
[장희직] - 게임, 연산자 끼워넣기, 디스크 컨트롤러, A와 B
Showing
3 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
|
||
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() { | ||
`A와 B`().solve() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |