-
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
[전현수] - 치즈, 가장 긴 증가하는 부분 수열 4, 내리막 길, 삼각 달팽이 #268
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,65 @@ | ||
package hyunsoo.`64week` | ||
|
||
/** | ||
* | ||
* <문제> | ||
* [가장 긴 증가하는 부분 수열 4](https://www.acmicpc.net/problem/14002) | ||
* | ||
* - 아이디어 | ||
* | ||
* - 트러블 슈팅 | ||
* | ||
*/ | ||
class `전현수_가장_긴_증가하는_부분_수열_4` { | ||
|
||
fun solution() { | ||
val n = readln().toInt() | ||
val array = readln().split(" ").map { it.toInt() } | ||
val dp = Array(n) { | ||
intArrayOf() | ||
} | ||
|
||
var answerIndex = 0 | ||
var maxLength = 0 | ||
|
||
dp[0] = intArrayOf(array[0]) | ||
for (cur in 0 until n) { | ||
|
||
dp[cur] = intArrayOf(array[cur]) | ||
|
||
var lisIndex = cur | ||
var lisSize = 1 | ||
|
||
for (target in 0 until cur) { | ||
|
||
// 앞에 수열의 마지막 값보다 내가 더 크고 | ||
if (array[target] < array[cur]) { | ||
// 갱신할 부분 수열의 길이가 더 길 때 | ||
if (lisSize < dp[target].size + 1) { | ||
lisSize = dp[target].size | ||
lisIndex = target | ||
} | ||
} | ||
} | ||
|
||
if (lisIndex != cur) { | ||
dp[cur] = dp[lisIndex] + array[cur] | ||
} | ||
|
||
if (maxLength < dp[cur].size) { | ||
answerIndex = cur | ||
maxLength = dp[cur].size | ||
} | ||
} | ||
|
||
println(maxLength) | ||
dp[answerIndex].forEach { | ||
print("$it ") | ||
} | ||
|
||
} | ||
} | ||
|
||
fun main() { | ||
전현수_가장_긴_증가하는_부분_수열_4().solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package hyunsoo.`64week` | ||
|
||
/** | ||
* | ||
* <문제> | ||
* [내리막 길](https://www.acmicpc.net/problem/1520) | ||
* | ||
* - 아이디어 | ||
* | ||
* - 트러블 슈팅 | ||
* | ||
*/ | ||
class `전현수_내리막_길` { | ||
|
||
private data class Position(val x: Int, val y: Int) | ||
|
||
private val dirs = listOf( | ||
Position(0, 1), | ||
Position(0, -1), | ||
Position(1, 0), | ||
Position(-1, 0), | ||
) | ||
|
||
private var n = 0 | ||
private var m = 0 | ||
private val board = mutableListOf<MutableList<Int>>() | ||
private lateinit var dp: Array<IntArray> | ||
|
||
fun solution() { | ||
init() | ||
dfs(Position(0, 0)) | ||
println(dp[0][0]) | ||
} | ||
|
||
private fun init() { | ||
readln().split(" ").map { it.toInt() } | ||
.apply { | ||
n = this.first() | ||
m = this.last() | ||
} | ||
|
||
dp = Array(n) { | ||
IntArray(m) { -1 } | ||
} | ||
|
||
repeat(n) { | ||
board.add(readln().split(" ").map { it.toInt() } as MutableList) | ||
} | ||
} | ||
|
||
private fun dfs(pos: Position): Int { | ||
|
||
// 목적지라면 | ||
if (pos.x == n - 1 && pos.y == m - 1) { | ||
return 1 | ||
} | ||
|
||
// 이미 탐색한 곳이라면 | ||
if (dp[pos.x][pos.y] != -1) return dp[pos.x][pos.y] | ||
|
||
dp[pos.x][pos.y] = 0 | ||
dirs.forEach { | ||
val nx = pos.x + it.x | ||
val ny = pos.y + it.y | ||
|
||
if (nx !in 0 until n || | ||
ny !in 0 until m || | ||
board[pos.x][pos.y] <= board[nx][ny] | ||
) return@forEach | ||
|
||
dp[pos.x][pos.y] += dfs(Position(nx, ny)) | ||
} | ||
|
||
return dp[pos.x][pos.y] | ||
} | ||
} | ||
|
||
fun main() { | ||
전현수_내리막_길().solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package hyunsoo.`64week` | ||
|
||
/** | ||
* | ||
* <문제> | ||
* [삼각 달팽이](https://school.programmers.co.kr/learn/courses/30/lessons/68645) | ||
* | ||
* - 아이디어 | ||
* | ||
* - 트러블 슈팅 | ||
* | ||
*/ | ||
class `전현수_삼각_달팽이` { | ||
|
||
private data class Position(val x: Int, val y: Int) | ||
|
||
private val dirs = listOf( | ||
Position(1, 0), | ||
Position(0, 1), | ||
Position(-1, -1), | ||
Comment on lines
+18
to
+20
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 solution(n: Int): List<Int> { | ||
|
||
val array = Array(n) { | ||
IntArray(n) { 0 } | ||
} | ||
|
||
var x = -1 | ||
var y = 0 | ||
var dirIndex = 0 | ||
var curNum = 1 | ||
|
||
// 방향 전환할 때 까지 움직여야할 횟수 | ||
var changeNumCnt = n | ||
// 움직인 횟수 | ||
var moveCnt = 0 | ||
|
||
while (curNum <= n.getCnt()) { | ||
|
||
val curDir = dirs[dirIndex] | ||
x += curDir.x | ||
y += curDir.y | ||
array[x][y] = curNum++ | ||
moveCnt++ | ||
|
||
if (moveCnt == changeNumCnt) { | ||
dirIndex++ | ||
dirIndex %= 3 | ||
moveCnt = 0 | ||
changeNumCnt-- | ||
} | ||
|
||
} | ||
return array.map { row -> | ||
row.filter { it != 0 } | ||
}.flatten() | ||
} | ||
|
||
private fun Int.getCnt(): Int { | ||
var target = 0 | ||
for (i in 1..this) target += i | ||
return target | ||
} | ||
} | ||
|
||
fun main() { | ||
전현수_삼각_달팽이().solution(6).apply { | ||
println(this) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package hyunsoo.`64week` | ||
|
||
import java.util.* | ||
|
||
/** | ||
* | ||
* <문제> | ||
* [치즈](https://www.acmicpc.net/problem/2638) | ||
* | ||
* - 아이디어 | ||
* | ||
* - 트러블 슈팅 | ||
* | ||
*/ | ||
class `전현수_치즈` { | ||
|
||
private data class Position(val x: Int, val y: Int) | ||
|
||
private val board = mutableListOf<MutableList<Int>>() | ||
|
||
private val dirs = listOf( | ||
Position(0, 1), | ||
Position(0, -1), | ||
Position(1, 0), | ||
Position(-1, 0), | ||
) | ||
|
||
fun solution() { | ||
|
||
val (n, m) = readln().split(" ").map { it.toInt() } | ||
|
||
repeat(n) { | ||
val row = readln().split(" ").map { | ||
if (it == "1") 3 else 0 | ||
} as MutableList | ||
board.add(row) | ||
} | ||
|
||
var cnt = 0 | ||
|
||
while (board.flatten().any { it != 0 }) { | ||
|
||
val queue: Queue<Position> = LinkedList() | ||
val outsideAirStack = Stack<Position>() | ||
val visited = Array(n) { | ||
BooleanArray(m) | ||
} | ||
|
||
queue.add(Position(0, 0)) | ||
visited[0][0] = true | ||
|
||
while (queue.isNotEmpty()) { | ||
|
||
val cur = queue.poll() | ||
|
||
dirs.forEach { | ||
val nx = it.x + cur.x | ||
val ny = it.y + cur.y | ||
|
||
if (nx !in 0 until n || | ||
ny !in 0 until m || | ||
visited[nx][ny] | ||
) return@forEach | ||
|
||
if (board[nx][ny] == 0) { | ||
val next = Position(nx, ny) | ||
outsideAirStack.add(next) | ||
queue.add(next) | ||
visited[nx][ny] = true | ||
} | ||
} | ||
} | ||
|
||
check(outsideAirStack) | ||
board.melt() | ||
|
||
cnt++ | ||
|
||
} | ||
|
||
println(cnt) | ||
} | ||
|
||
private fun check(stack: Stack<Position>) { | ||
while (stack.isNotEmpty()) { | ||
val (i, j) = stack.pop() | ||
val cur = board[i][j] | ||
if (cur == 0) { | ||
|
||
dirs.forEach { | ||
val nx = i + it.x | ||
val ny = j + it.y | ||
|
||
if (nx !in board.indices || | ||
ny !in board.first().indices | ||
) return@forEach | ||
|
||
if (0 < board[nx][ny]) board[nx][ny]-- | ||
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. 3으로 쌓아두고 하나씩 빼는 방식도 좋네요! |
||
} | ||
} | ||
} | ||
} | ||
|
||
private fun MutableList<MutableList<Int>>.melt() { | ||
for (i in this.indices) { | ||
for (j in this.first().indices) { | ||
val cur = this[i][j] | ||
if (cur == 1) this[i][j] = 0 | ||
else if (cur == 2) this[i][j] = 3 | ||
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() { | ||
전현수_치즈().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.
요렇게 3방향 정해놓고 푸는게 깔끔해보였습니다!!