-
Notifications
You must be signed in to change notification settings - Fork 0
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
[Week3] 전현수: 단풍잎 이야기, 킹, 크로스워드, 랜선 자르기 #14
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package `3week`.hyunsoo | ||
|
||
/** | ||
* <문제> | ||
* [단풍잎 이야기](https://www.acmicpc.net/problem/16457) | ||
* | ||
* 키셋팅을 하면 원하는 키를 눌러서 원하는 스킬을 쓸 수 있음. | ||
* 리유나는 레벨 225, 라가는 레벨이 202 | ||
* 라가가 키보드에 있는 키를 n개 부숨 | ||
* 리유나는 그래도 퀘스트를 해야함. 2n개의 스킬들 중 n개를 적절히 배치해서 해야함 | ||
* | ||
* m개의 퀘스트가 주어지고 k개의 스킬을 사용해야함. | ||
* 스킬을 사용할 수 없으면 퀘스트 수행 불가 | ||
* | ||
* 입/출력 | ||
* - 첫째 줄 | ||
* - 키의 개수 n, 퀘스트의 개수 m, 퀘스트 당 사용해야하는 스킬의 수 k | ||
* - 둘째 줄 | ||
* - 각각의 퀘스트에서 사용해야하는 스킬들이 나옴. | ||
* | ||
* 아이디어 | ||
* - 완탐 드가자 | ||
* | ||
* | ||
*/ | ||
|
||
class `전현수_단풍잎_이야기`{ | ||
|
||
val questDataMap = mutableListOf<List<Int>>() | ||
val pickedKey = mutableListOf<Int>() | ||
var canClearQuestCnt = 0 | ||
|
||
fun solution() { | ||
|
||
val (keyCnt, questCnt, skillPerQuest) = readln().split(" ").map { it.toInt() } | ||
val skillList = (1..2 * keyCnt).toList() | ||
|
||
repeat(questCnt) { | ||
val quests = readln().split(" ").map { it.toInt() } | ||
questDataMap.add(quests) | ||
} | ||
Comment on lines
+38
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add를 반복적으로 하는것 보다 애초에 초기화를 동시에 적용하게 repeat 대신에 Array 사용하고 바로 questDataMap 에 반영하는 구조는 어떠신가요? questDataMap = Array(questCnt) {
readln().split(" ").map { it.toInt() }
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 그러면 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 리스트보다... Array에 입력과 동시에 초기화.. 좋은 것 같습니다!! |
||
keySet(0, keyCnt, 0, skillList) | ||
println(canClearQuestCnt) | ||
} | ||
|
||
fun keySet(cnt: Int, depth: Int, startWith: Int, skillList: List<Int>) { | ||
if (cnt == depth) { | ||
|
||
var curCanClearQuestCnt = 0 | ||
questDataMap.forEach { needKeyList -> | ||
if (needKeyList.all { it in pickedKey }) { | ||
curCanClearQuestCnt++ | ||
} | ||
} | ||
Comment on lines
+50
to
+54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분 count 함수로 쓰면 ++ 연산 없이 될것같아요 val curCanClearQuestCnt = questDataMap.count { it.all { it in pickedKey } } |
||
if (curCanClearQuestCnt >= canClearQuestCnt) { | ||
canClearQuestCnt = curCanClearQuestCnt | ||
} | ||
|
||
return | ||
} | ||
|
||
for (index in startWith until skillList.size) { | ||
pickedKey.add(skillList[index]) | ||
keySet(cnt + 1, depth, index + 1, skillList) | ||
pickedKey.removeAt(pickedKey.lastIndex) | ||
} | ||
} | ||
} | ||
|
||
fun main(){ | ||
val myClass = `전현수_단풍잎_이야기`() | ||
myClass.solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package `3week`.hyunsoo | ||
|
||
/** | ||
* <문제> | ||
* [랜선 자르기](https://www.acmicpc.net/problem/1654) | ||
* | ||
* N개의 랜던을 만들어야함 | ||
* K개의 랜선이 존재함. | ||
* 모두 N개의 같은 길이의 랜선으로 만들고 싶어! | ||
* | ||
* 아이디어 | ||
* - 이분 탐색 | ||
* - 1 이 start / 최대가 end | ||
*/ | ||
|
||
class `전현수_랜선_자르기`{ | ||
|
||
fun solution() { | ||
|
||
val (haveCnt, needCnt) = readln().split(" ").map { it.toInt() } | ||
|
||
val lanList = mutableListOf<Long>() | ||
|
||
repeat(haveCnt) { | ||
val lanInfo = readln().toLong() | ||
lanList.add(lanInfo) | ||
} | ||
Comment on lines
+22
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정이 필요없는 타입인것 같은데 초기화하면서 넣어주는 방식은 어떨까요? val lanList = Array(haveCnt) {
readln().toLong()
} |
||
|
||
var start = 1L | ||
var end = lanList.maxOrNull()!! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. max 함수가 필요하면 |
||
var maxLan = start | ||
|
||
while (start <= end) { | ||
|
||
val mid = (start + end) / 2 | ||
|
||
var canGetLanCnt = 0L | ||
// 조건 | ||
lanList.forEach { lan -> | ||
canGetLanCnt += lan / mid | ||
} | ||
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sumOf 사용해보시는건 어떤가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 오... 그러네요 고차함수를 쓸 생각을 못했네요...!!! |
||
|
||
if (canGetLanCnt >= needCnt) { | ||
start = mid + 1 | ||
maxLan = mid | ||
} else { | ||
end = mid - 1 | ||
} | ||
|
||
} | ||
println(maxLan) | ||
} | ||
|
||
} | ||
|
||
fun main(){ | ||
val myClass = `전현수_랜선_자르기`() | ||
myClass.solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package `3week`.hyunsoo | ||
|
||
import java.util.* | ||
import java.util.ArrayDeque | ||
|
||
/** | ||
* <문제> | ||
* [크로스워드](https://www.acmicpc.net/problem/1706) | ||
* | ||
* 깊이우선 탐색 혹은 너비우선 탐색을 이용해서 모든 낱말 확인하기 | ||
* - 단어 판정 | ||
* - 길이가 1이상인 것 | ||
* - 시작 지점 혹은 #이면 탐색 시작 | ||
*/ | ||
|
||
class `전현수_크로스워드`{ | ||
|
||
|
||
|
||
val myQueue: Queue<Int> = ArrayDeque() | ||
fun solution() { | ||
|
||
val puzzleData = mutableListOf<MutableList<Char>>() | ||
val madeWordList = mutableListOf<String>() | ||
|
||
val (r, c) = readln().split(" ").map { it.toInt() } | ||
|
||
repeat(r) { | ||
val puzzleRow = readln().chunked(1).map { it.first() }.toMutableList() | ||
puzzleData.add(puzzleRow) | ||
} | ||
Comment on lines
+23
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. puzzleData는 수정작업이 없는것 같은데 아래와 같이 수정해서 초기화 하면 좋을것 같아요! val puzzleData = Array(r) {
readln().chunked(1).map { it.first() }.toMutableList()
} |
||
|
||
val duplicatedPuzzle = puzzleData.deepCopy() | ||
|
||
for (i in 0 until r) { | ||
for (j in 0 until c) { | ||
|
||
makeWordVertical(i, j, duplicatedPuzzle)?.let { | ||
if (it.length > 1) madeWordList.add(it) | ||
} | ||
makeWordHorizontal(i, j, puzzleData)?.let { | ||
if (it.length > 1) madeWordList.add(it) | ||
} | ||
Comment on lines
+38
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분 아래 함수로 바꿔서 연산해보면 중복을 묶어서 처리할 수 있을것 같아요 arrayOf(
makeWord(i, j, duplicatedPuzzle) { next, x, y ->
if (!(next >= size || get(next)[y] == '#')) {
val temp: Char = get(next)[y]
this[next][y] = '#'
temp
} else null
},
makeWord(i, j, puzzleData) { next, x, y ->
if (!(next >= first().size || get(x)[next] == '#')) {
val temp: Char = get(x)[next]
this[x][next] = '#'
temp
} else null
}
).filter { (it?.length?: 0) > 1 }.let {
madeWordList.addAll(it as List<String>)
} |
||
|
||
} | ||
} | ||
|
||
println(madeWordList.minOrNull()) | ||
|
||
} | ||
|
||
fun makeWordHorizontal(x: Int, y: Int, puzzleData: MutableList<MutableList<Char>>): String? { | ||
|
||
if (puzzleData[x][y] == '#') return null | ||
|
||
val wordBuilder = StringBuilder() | ||
wordBuilder.append(puzzleData[x][y]) | ||
|
||
puzzleData[x][y] = '#' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. '#' 같은 값을 BLANK라는 값으로 companion object에 const val 로 선언해뒀으면 깔끔하게 되었을 것 같아요! 전체적으로요 |
||
myQueue.add(y) | ||
|
||
while (myQueue.isNotEmpty()) { | ||
val ny = myQueue.poll() + 1 | ||
|
||
if (ny >= puzzleData.first().size || | ||
puzzleData[x][ny] == '#' | ||
) return wordBuilder.toString() | ||
|
||
wordBuilder.append(puzzleData[x][ny]) | ||
puzzleData[x][ny] = '#' | ||
myQueue.add(ny) | ||
} | ||
|
||
return wordBuilder.toString() | ||
} | ||
|
||
fun makeWordVertical(x: Int, y: Int, puzzleData: MutableList<MutableList<Char>>): String? { | ||
|
||
if (puzzleData[x][y] == '#') return null | ||
|
||
val wordBuilder = StringBuilder() | ||
wordBuilder.append(puzzleData[x][y]) | ||
|
||
puzzleData[x][y] = '#' | ||
myQueue.add(x) | ||
|
||
while (myQueue.isNotEmpty()) { | ||
val nx = myQueue.poll() + 1 | ||
|
||
if (nx >= puzzleData.size || | ||
puzzleData[nx][y] == '#' | ||
) return wordBuilder.toString() | ||
|
||
wordBuilder.append(puzzleData[nx][y]) | ||
puzzleData[nx][y] = '#' | ||
myQueue.add(nx) | ||
} | ||
|
||
return wordBuilder.toString() | ||
} | ||
Comment on lines
+52
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분 함수 너무 중복적인데 공통화해서 함수형으로 추가해보는건 어떤가요? private fun makeWord(x: Int, y: Int, puzzleData: Array<MutableList<Char>>, apply: Array<MutableList<Char>>.(next: Int, x: Int, y: Int) -> Char?): String? {
if (puzzleData[x][y] == '#') return null
val wordBuilder = StringBuilder()
wordBuilder.append(puzzleData[x][y])
puzzleData[x][y] = '#'
myQueue.add(x)
while (myQueue.isNotEmpty()) {
val next = myQueue.poll() + 1
puzzleData.apply(next, x, y)?.let { wordBuilder.append(it) }?: break
myQueue.add(next)
}
return wordBuilder.toString()
} |
||
|
||
fun <T> MutableList<MutableList<T>>.deepCopy(): MutableList<MutableList<T>> { | ||
val newList = mutableListOf<MutableList<T>>() | ||
this.forEach { | ||
val mutableList = it.toMutableList() | ||
newList.add(mutableList) | ||
} | ||
return newList | ||
} | ||
Comment on lines
+102
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이렇게 바꿔보시는건 어떤가요? private fun <T> Array<MutableList<T>>.deepCopy(): Array<MutableList<T>> {
return map {
it.toMutableList()
}.toTypedArray()
} |
||
|
||
} | ||
Comment on lines
+16
to
+111
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전체적으로 정리해보면 class `전현수_크로스워드`{
companion object {
const val BLANK = '#'
}
val myQueue: Queue<Int> = ArrayDeque()
fun solution() {
val madeWordList = mutableListOf<String>()
val (r, c) = readln().split(" ").map { it.toInt() }
val puzzleData = Array(r) {
readln().chunked(1).map { it.first() }.toMutableList()
}
val duplicatedPuzzle = puzzleData.deepCopy()
for (i in 0 until r) {
for (j in 0 until c) {
arrayOf(
makeWord(i, j, duplicatedPuzzle) { next, x, y ->
if (!(next >= size || get(next)[y] == BLANK)) {
val temp: Char = get(next)[y]
this[next][y] = BLANK
temp
} else null
},
makeWord(i, j, puzzleData) { next, x, y ->
if (!(next >= first().size || get(x)[next] == BLANK)) {
val temp: Char = get(x)[next]
this[x][next] = BLANK
temp
} else null
}
).filter { (it?.length?: 0) > 1 }.let {
madeWordList.addAll(it as List<String>)
}
}
}
println(madeWordList.minOrNull())
}
private fun makeWord(x: Int, y: Int, puzzleData: Array<MutableList<Char>>, apply: Array<MutableList<Char>>.(next: Int, x: Int, y: Int) -> Char?): String? {
if (puzzleData[x][y] == BLANK) return null
val wordBuilder = StringBuilder()
wordBuilder.append(puzzleData[x][y])
puzzleData[x][y] = BLANK
myQueue.add(x)
while (myQueue.isNotEmpty()) {
val next = myQueue.poll() + 1
puzzleData.apply(next, x, y)?.let { wordBuilder.append(it) }?: break
myQueue.add(next)
}
return wordBuilder.toString()
}
private fun <T> Array<MutableList<T>>.deepCopy(): Array<MutableList<T>> {
return map {
it.toMutableList()
}.toTypedArray()
}
} |
||
|
||
fun main(){ | ||
val myClass = `전현수_크로스워드`() | ||
myClass.solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package `3week`.hyunsoo | ||
|
||
/** | ||
* <문제> | ||
* [킹](https://www.acmicpc.net/problem/1063) | ||
* | ||
* 8*8 크기의 체스판에 킹이 있음. | ||
* 알파벳은 열 / 숫자는 행을 상 | ||
* 체스판에는 돌이 있고 킹이 돌한테 움직이면 돌은 킹과 동일한 방향으로 이동해야함. | ||
* 만약 킹이나 돌이 체스판 밖으로 이동하는 경우는 무시하기 | ||
* | ||
* 아이디어 | ||
* - 객체지향적으로 구현을 해보자..!!! | ||
*/ | ||
|
||
class `전현수_킹`{ | ||
|
||
data class Position(val x: Int, val y: Int) | ||
|
||
data class GameCharacter(var x: Int = 0, var y: Int = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 객체 정리해서 아래 기능 추가해보면 묶을 부분 묶어서 정리할 수 있지 않을까요? data class GameCharacter(var x: Int = 0, var y: Int = 0) {
operator fun plus(pos: Position) = run {
GameCharacter(x + pos.x, y + pos.y)
}
fun contain(xRange: IntRange, yRange: IntRange) = x in xRange && y in yRange
constructor(pos: Position) : this(pos.x, pos.y)
override fun toString(): String {
return "${Alphabet.values()[x]}${(y) + 1}"
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 객체지향적으로 만든다고 만들었는데 데이터 클래스만 만들어둔 꼴이었네요...!!! 동호님이 수정해주신 방식이 훨씬 이쁘고 좋은 것 같아요! |
||
|
||
enum class Dir(val position: Position) { | ||
R(Position(1, 0)), | ||
L(Position(-1, 0)), | ||
B(Position(0, -1)), | ||
T(Position(0, 1)), | ||
RT(Position(1, 1)), | ||
LT(Position(-1, 1)), | ||
RB(Position(1, -1)), | ||
LB(Position(-1, -1)), | ||
} | ||
Comment on lines
+22
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이넘 활용하신 부분 굉장히 좋네요! |
||
|
||
enum class Alphabet { | ||
A, B, C, D, E, F, G, H | ||
} | ||
|
||
fun solution() { | ||
|
||
val (kingLocation, stoneLocation, moveCnt) = readln().split(" ") | ||
|
||
val king = GameCharacter() | ||
coordinateToPosition(kingLocation).apply { | ||
king.x = this.x | ||
king.y = this.y | ||
} | ||
|
||
val stone = GameCharacter() | ||
coordinateToPosition(stoneLocation).apply { | ||
stone.x = this.x | ||
stone.y = this.y | ||
} | ||
Comment on lines
+41
to
+51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위에 선언한 생성자 덕에 자동으로 var king = GameCharacter(coordinateToPosition(kingLocation))
var stone = GameCharacter(coordinateToPosition(stoneLocation)) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 와 생성자를 이용해서 이렇게 할 수도 있군요...... 대박이네요 |
||
|
||
repeat(moveCnt.toInt()) { | ||
val command = readln() | ||
lateinit var dir: Dir | ||
when (command) { | ||
Comment on lines
+55
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dir을 lateinit 지정 없이 그냥 when에서 초기화해도 되지 않나요? |
||
(Dir.R).toString() -> { | ||
dir = Dir.R | ||
} | ||
|
||
(Dir.L).toString() -> { | ||
dir = Dir.L | ||
} | ||
|
||
(Dir.B).toString() -> { | ||
dir = Dir.B | ||
} | ||
|
||
(Dir.T).toString() -> { | ||
dir = Dir.T | ||
} | ||
|
||
(Dir.RT).toString() -> { | ||
dir = Dir.RT | ||
} | ||
|
||
(Dir.LT).toString() -> { | ||
dir = Dir.LT | ||
} | ||
|
||
(Dir.RB).toString() -> { | ||
dir = Dir.RB | ||
} | ||
|
||
(Dir.LB).toString() -> { | ||
dir = Dir.LB | ||
} | ||
} | ||
Comment on lines
+55
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 긁적긁적... 이 코드 == val dir: Dir = Dir.valueOf(command) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 허허..... |
||
|
||
val nx = dir.position.x | ||
val ny = dir.position.y | ||
Comment on lines
+90
to
+91
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oprator 재정의해서! val nKing = king + dir.position |
||
|
||
// 킹이 이동할 수 없는 위치면 명령 무시 | ||
if ((king.x + nx in 0..7).not() || | ||
(king.y + ny in 0..7).not() | ||
) { | ||
return@repeat | ||
} | ||
Comment on lines
+94
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분도 contain 함수로 교체가능하죠? if (!nKing.contain(0..7, 0..7)) {
return@repeat
} |
||
// 킹이 이동할 위치에 돌이 있다면 | ||
if (king.x + nx == stone.x && | ||
king.y + ny == stone.y | ||
Comment on lines
+100
to
+101
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 같은 객체의 data class면 자동으로 항목 비교가 되게끔! king == stone |
||
) { | ||
// 돌이 움직일 수 있는 위치라면 | ||
if (stone.x + nx in 0..7 && | ||
stone.y + ny in 0..7 | ||
) { | ||
// 돌 이동 후 킹 이동 | ||
stone.x += nx | ||
stone.y += ny | ||
king.x += nx | ||
king.y += ny | ||
} | ||
// 킹이 이동할 위치에 돌이 없다면 바로 이동 | ||
} else { | ||
king.x += nx | ||
king.y += ny | ||
} | ||
Comment on lines
+100
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이부분도 위랑 동일하게 공통화 할 수 있겠죠 if (nKing == stone) {
val nStone = stone + dir.position
// 돌이 움직일 수 있는 위치라면
if (nStone.contain(0..7, 0..7)) {
// 돌 이동 후 킹 이동
stone = nStone
king = nKing
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king = nKing
} |
||
|
||
} | ||
println(positionToCoordinate(king)) | ||
println(positionToCoordinate(stone)) | ||
Comment on lines
+120
to
+121
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 출력은 toString에 정의했으니 그냥 출력하면됩니다 println(king)
println(stone) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (minor) 개행은 한번에 하면 더 좋겠죠? println("$king\n$stone") |
||
} | ||
|
||
fun coordinateToPosition(coordinate: String): Position { | ||
val (xPos, yPos) = coordinate.chunked(1) | ||
return Position( | ||
Alphabet.values().indexOf(Alphabet.valueOf(xPos)), | ||
yPos.toInt() - 1 | ||
) | ||
} | ||
|
||
fun positionToCoordinate(character: GameCharacter): String { | ||
return "${(Alphabet.values()[character.x])}${(character.y) + 1}" | ||
} | ||
|
||
} | ||
Comment on lines
+16
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 전체적으로 정리하면 class `전현수_킹` {
data class Position(val x: Int, val y: Int)
data class GameCharacter(var x: Int = 0, var y: Int = 0) {
operator fun plus(pos: Position) = run {
GameCharacter(x + pos.x, y + pos.y)
}
fun contain(xRange: IntRange, yRange: IntRange) = x in xRange && y in yRange
constructor(pos: Position) : this(pos.x, pos.y)
override fun toString(): String {
return "${Alphabet.values()[x]}${(y) + 1}"
}
}
enum class Dir(val position: Position) {
R(Position(1, 0)),
L(Position(-1, 0)),
B(Position(0, -1)),
T(Position(0, 1)),
RT(Position(1, 1)),
LT(Position(-1, 1)),
RB(Position(1, -1)),
LB(Position(-1, -1))
}
enum class Alphabet {
A, B, C, D, E, F, G, H
}
fun solution() {
val (kingLocation, stoneLocation, moveCnt) = readln().split(" ")
var king = GameCharacter(coordinateToPosition(kingLocation))
var stone = GameCharacter(coordinateToPosition(stoneLocation))
repeat(moveCnt.toInt()) {
val command = readln()
val dir: Dir = Dir.valueOf(command)
val nKing = king + dir.position
// 킹이 이동할 수 없는 위치면 명령 무시
if (!nKing.contain(0..7, 0..7)) {
return@repeat
}
// 킹이 이동할 위치에 돌이 있다면
if (nKing == stone) {
val nStone = stone + dir.position
// 돌이 움직일 수 있는 위치라면
if (nStone.contain(0..7, 0..7)) {
// 돌 이동 후 킹 이동
stone = nStone
king = nKing
}
// 킹이 이동할 위치에 돌이 없다면 바로 이동
} else {
king = nKing
}
}
println("$king\n$stone")
}
fun coordinateToPosition(coordinate: String): Position {
val (xPos, yPos) = coordinate.chunked(1)
return Position(
Alphabet.values().indexOf(Alphabet.valueOf(xPos)),
yPos.toInt() - 1
)
}
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 훨씬 더 객체 지향적으로... 너무 이쁘고 깔끔하네요 코드 |
||
|
||
fun main(){ | ||
val myClass = `전현수_킹`() | ||
myClass.solution() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
skillPerQuest
변수 사용 안하고 있네요!각각의 항목들에 어떤 용도인지 명시해주는 부분 좋은것 같아요 👍🏼