Skip to content

Commit

Permalink
Merge pull request #269 from wellFoundedDevelopers/jimin/64week
Browse files Browse the repository at this point in the history
[이지민] - 가장 긴 증가하는 부분 수열 4, 치즈, 내리막 길
  • Loading branch information
jeeminimini authored Jun 2, 2024
2 parents dc542e5 + 071a101 commit 99cf4ee
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package jimin.`64week`

class `가장 긴 증가하는 부분 수열 4` {
fun solve() {
val n = readln().toInt()
val numbers = readln().split(" ").map { it.toInt() }
val dp = MutableList<MutableList<Int>>(n) { mutableListOf() }
dp[0] = mutableListOf(0)
var maxNum = mutableListOf(0)

for (i in 1 until n) {
var maxi = mutableListOf<Int>()
for (j in i - 1 downTo 0) {
if (numbers[i] > numbers[j]) {
if (dp[j].size > maxi.size) {
maxi = dp[j].toMutableList()
}
}
}
maxi.add(i)
dp[i] = maxi

if (dp[i].size > maxNum.size) {
maxNum = dp[i]
}
}

println(maxNum.size)
maxNum.forEach {
print("${numbers[it]} ")
}
}
}

fun main() {
`가장 긴 증가하는 부분 수열 4`().solve()
}
42 changes: 42 additions & 0 deletions src/main/kotlin/jimin/64week/내리막 길.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jimin.`64week`

import java.util.PriorityQueue

class `내리막 길` {
fun solve() {
val (n, m) = readln().split(" ").map { it.toInt() }
val ground = mutableListOf<MutableList<Int>>()

for (i in 0 until n) {
ground.add(readln().split(" ").map { it.toInt() } as MutableList<Int>)
}

val dx = mutableListOf(0, 0, 1, -1)
val dy = mutableListOf(1, -1, 0, 0)
val pq = PriorityQueue<Pair<Int, Int>> { a, b ->
if (ground[a.first][a.second] > ground[b.first][b.second]) -1 else 1
}
pq.add(0 to 0)
val visited = MutableList(n) { MutableList(m) { 0 } }
visited[0][0] = 1

while (pq.isNotEmpty()) {
val (nx, ny) = pq.poll()

for (i in 0 until 4) {
if (nx + dx[i] in 0 until n && ny + dy[i] in 0 until m && ground[nx + dx[i]][ny + dy[i]] < ground[nx][ny]) {
if (visited[nx + dx[i]][ny + dy[i]] == 0) {
pq.add(nx + dx[i] to ny + dy[i])
}
visited[nx + dx[i]][ny + dy[i]] += visited[nx][ny]
}
}
}

println(visited[n - 1][m - 1])
}
}

fun main() {
`내리막 길`().solve()
}
91 changes: 91 additions & 0 deletions src/main/kotlin/jimin/64week/치즈.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package jimin.`64week`


class 치즈 {
private val IN = 0
private val CHEESE = 1
private val OUT = 2

fun solve() {
val (n, m) = readln().split(" ").map { it.toInt() }
val ground = mutableListOf<MutableList<Int>>()
for (i in 0 until n) {
ground.add(readln().split(" ").map { it.toInt() } as MutableList<Int>)
}

var time = -1
var isOver = false
while (!isOver) {
val visited = MutableList(n) { MutableList(m) { false } }
isOver = true
for (i in 0 until n) {
for (j in 0 until m) {
if (!visited[i][j]) {
if (i == 0 && j == 0) {
ground[i][j] = OUT
bfs(i, j, n, m, ground, visited, OUT)
} else if (ground[i][j] == CHEESE) {
isOver = false
bfs(i, j, n, m, ground, visited, CHEESE)
}
}
}
}

time += 1
}

println(time)

}

fun bfs(x: Int, y: Int, n: Int, m: Int, ground: MutableList<MutableList<Int>>, visited: MutableList<MutableList<Boolean>>, type: Int) {
val dx = mutableListOf(0, 0, 1, -1)
val dy = mutableListOf(1, -1, 0, 0)
val queue = ArrayDeque<Pair<Int, Int>>()
queue.add(x to y)
val outs = mutableListOf<Pair<Int, Int>>()

while (queue.isNotEmpty()) {
val (nx, ny) = queue.removeFirst()

for (i in 0 until 4) {
if (nx + dx[i] in 0 until n && ny + dy[i] in 0 until m && !visited[nx + dx[i]][ny + dy[i]]) {
if (type == OUT){
if(ground[nx + dx[i]][ny + dy[i]] != CHEESE) {
queue.addLast(nx + dx[i] to ny + dy[i])
visited[nx + dx[i]][ny + dy[i]] = true
ground[nx + dx[i]][ny + dy[i]] = type
}
}else if (type == CHEESE) {
if(ground[nx + dx[i]][ny + dy[i]] == CHEESE) {
queue.addLast(nx + dx[i] to ny + dy[i])
visited[nx + dx[i]][ny + dy[i]] = true
}
}
}
}

if (type == CHEESE) {
var out = 0
for (i in 0 until 4) {
if (nx + dx[i] in 0 until n && ny + dy[i] in 0 until m && ground[nx + dx[i]][ny + dy[i]] == OUT) {
out += 1
}
}
if (out >= 2){
outs.add(nx to ny)
}
}
}

outs.forEach {
ground[it.first][it.second] = OUT
}

}
}

fun main() {
치즈().solve()
}

0 comments on commit 99cf4ee

Please sign in to comment.