Skip to content

Commit

Permalink
Merge pull request #268 from wellFoundedDevelopers/hyunsoo/64week
Browse files Browse the repository at this point in the history
[전현수] - 치즈, 가장 긴 증가하는 부분 수열 4, 내리막 길, 삼각 달팽이
  • Loading branch information
soopeach authored May 26, 2024
2 parents 05cb14b + 050f01f commit 3d4765e
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 0 deletions.
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()
}
80 changes: 80 additions & 0 deletions src/main/kotlin/hyunsoo/64week/내리막 길.kt
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()
}
71 changes: 71 additions & 0 deletions src/main/kotlin/hyunsoo/64week/삼각 달팽이.kt
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),
)

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)
}
}
117 changes: 117 additions & 0 deletions src/main/kotlin/hyunsoo/64week/치즈.kt
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]--
}
}
}
}

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

fun main() {
전현수_치즈().solution()
}

0 comments on commit 3d4765e

Please sign in to comment.