Skip to content

Commit

Permalink
Merge pull request #125 from wellFoundedDevelopers/heejik/28week
Browse files Browse the repository at this point in the history
[장희직] - 연구소, 친구비, 드래곤 커브, 미세먼지 안녕!
  • Loading branch information
jhg3410 authored Mar 26, 2023
2 parents d0591cc + 02178ca commit b3e0c3c
Show file tree
Hide file tree
Showing 4 changed files with 386 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/main/kotlin/heejik/28week/드래곤 커브.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package heejik.`28week`

import kotlin.properties.Delegates

class `드래곤 커브` {

enum class Move(val pos: Pos) {
RIGHT(Pos(0, 1)), UP(Pos(-1, 0)), LEFT(Pos(0, -1)), DOWN(Pos(1, 0));

fun rotate(): Move {
return Move.values().get((Move.values().indexOf(this) + 1) % 4)
}
}


data class Pos(
val x: Int,
val y: Int
)

var n by Delegates.notNull<Int>()
val board = MutableList(101) { MutableList(101) { false } }

fun solve() {
getInput()
printAnswer()
}

private fun getInput() {
n = readln().toInt()
repeat(n) {
val (y, x, d, g) = readln().split(' ').map { it.toInt() }
drawDragonCurve(x, y, d, g)
}
}

private fun drawDragonCurve(x: Int, y: Int, d: Int, g: Int) {
val preMoves = mutableListOf(Move.values().get(d))
val firstPos = Move.values().get(d).pos
board[x][y] = true
board[firstPos.x + x][firstPos.y + y] = true
var lastPos = Pos(firstPos.x + x, firstPos.y + y)
repeat(g) {
val tmpMoves = mutableListOf<Move>()
preMoves.forEach {
board[it.rotate().pos.x + lastPos.x][it.rotate().pos.y + lastPos.y] = true
lastPos = Pos(it.rotate().pos.x + lastPos.x, it.rotate().pos.y + lastPos.y)
tmpMoves.add(it.rotate())
}
preMoves.reverse()
preMoves.addAll(tmpMoves)
preMoves.reverse()
}
}

private fun printAnswer() {
var cnt = 0
(0 until 100).forEach first@{ x ->
(0 until 100).forEach second@{ y ->
if (board[x][y].not()) return@second
if (board[x + 1][y].not()) return@second
if (board[x][y + 1].not()) return@second
if (board[x + 1][y + 1].not()) return@second
cnt++
}
}

println(cnt)
}
}

fun main() {
`드래곤 커브`().solve()
}
140 changes: 140 additions & 0 deletions src/main/kotlin/heejik/28week/미세먼지 안녕!.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package heejik.`28week`

import java.io.BufferedReader
import java.io.InputStreamReader
import kotlin.properties.Delegates

class `미세먼지 안녕!` {

val br = BufferedReader(InputStreamReader(System.`in`))
data class Pos(
val x: Int,
val y: Int
)

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)

var r by Delegates.notNull<Int>()
var c by Delegates.notNull<Int>()
var t by Delegates.notNull<Int>()
val board = mutableListOf<MutableList<Int>>()
val airCleanerPos = mutableListOf<Pos>()

fun solve() {
getInput()
repeat(t) {
spread()
operateAirCleaner()
}
println(getDust())
}

private fun getInput() {
br.readLine().split(' ').map { it.toInt() }.run {
r = this[0]
c = this[1]
t = this[2]
}
repeat(r) { x ->
board.add(br.readLine().split(' ').map { it.toInt() }.run {
forEachIndexed { y, i ->
if (i == -1)
airCleanerPos.add(Pos(x, y))
}
toMutableList()
})
}
}

private fun spread() {
val afterBoard = MutableList(r) { MutableList(c) { 0 } }

board.forEachIndexed { x, row ->
row.forEachIndexed { y, mount ->
if (mount == -1) afterBoard[x][y] = -1
else if (mount != 0) {
val spreadPos = mutableListOf<Pos>()
for (i in dx.indices) {
val nx = x + dx[i]
val ny = y + dy[i]
if ((nx !in 0 until r) or (ny !in 0 until c)) continue
if (board[nx][ny] == -1) continue
spreadPos.add(Pos(nx, ny))
}
spreadPos.forEach { pos ->
afterBoard[pos.x][pos.y] += mount / 5
}
afterBoard[x][y] += mount - ((mount / 5) * spreadPos.size)
}
}
}
afterBoard.forEachIndexed { x, row ->
row.forEachIndexed { y, i ->
board[x][y] = i
}
}
}

private fun operateAirCleaner() {
val topCleaner = airCleanerPos.first()
var tmp = 0
var preValue = 0
for (y in 1 until c) {
tmp = board[topCleaner.x][y]
board[topCleaner.x][y] = preValue
preValue = tmp
}
for (x in topCleaner.x - 1 downTo 0) {
tmp = board[x][c - 1]
board[x][c - 1] = preValue
preValue = tmp
}

for (y in c - 2 downTo 0) {
tmp = board[0][y]
board[0][y] = preValue
preValue = tmp
}

for (x in 1 until topCleaner.x) {
tmp = board[x][0]
board[x][0] = preValue
preValue = tmp
}


val bottomCleaner = airCleanerPos.last()

preValue = 0
for (y in 1 until c) {
tmp = board[bottomCleaner.x][y]
board[bottomCleaner.x][y] = preValue
preValue = tmp
}
for (x in bottomCleaner.x + 1 until r) {
tmp = board[x][c - 1]
board[x][c - 1] = preValue
preValue = tmp
}

for (y in c - 2 downTo 0) {
tmp = board[r - 1][y]
board[r - 1][y] = preValue
preValue = tmp
}

for (x in r - 2 downTo bottomCleaner.x + 1) {
tmp = board[x][0]
board[x][0] = preValue
preValue = tmp

}
}

private fun getDust(): Int = board.sumOf { it.sum() } + 2
}

fun main() {
`미세먼지 안녕!`().solve()
}
111 changes: 111 additions & 0 deletions src/main/kotlin/heejik/28week/연구소.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package heejik.`28week`

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import kotlin.math.max
import kotlin.properties.Delegates

class 연구소 {

data class Pos(
val x: Int,
val y: Int
)

val dx = listOf(1, -1, 0, 0)
val dy = listOf(0, 0, 1, -1)

var n by Delegates.notNull<Int>()
var m by Delegates.notNull<Int>()

val board = mutableListOf<MutableList<Int>>()
val emptyPoses = mutableListOf<Pos>()
val virusPoses = mutableListOf<Pos>()
var safeAreaCount = 0

fun solve() {
getInput()
setNewWall(listOf())
println(safeAreaCount)
}

private fun getInput() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
}

repeat(n) { x ->
board.add(readln().split(' ').map { it.toInt() }.run {
forEachIndexed { y, it ->
if (it == 0) emptyPoses.add(Pos(x, y))
if (it == 2) virusPoses.add(Pos(x, y))
}
toMutableList()
})
}
}

private fun setNewWall(wallPos: List<Pos>, start: Int = 0) {
if (wallPos.size == 3) {
buildWall(wallPos = wallPos)
return
}

for (i in start until emptyPoses.size) {
setNewWall(wallPos.plus(emptyPoses[i]), i + 1)
}
}

private fun buildWall(wallPos: List<Pos>) {
wallPos.forEach { pos ->
board[pos.x][pos.y] = 1
}
spreadVirus()

wallPos.forEach { pos ->
board[pos.x][pos.y] = 0
}
}

private fun spreadVirus() {
val queue = ArrayDeque<Pos>()
virusPoses.forEach {
queue.add(it)
}

while (queue.isNotEmpty()) {
val pos = queue.removeFirst()
for (i in dx.indices) {
val nx = dx[i] + pos.x
val ny = dy[i] + pos.y
if ((nx !in 0 until n) or (ny !in 0 until m)) continue

if (board[nx][ny] == 0) {
queue.add(Pos(nx, ny))
board[nx][ny] = -1
}
}
}

getSafeAreaCount()
}

private fun getSafeAreaCount() {
var cnt = 0

board.forEachIndexed { x, row ->
row.forEachIndexed { y, it ->
if (it == 0) cnt++
if (it == -1) board[x][y] = 0
}
}
safeAreaCount = max(safeAreaCount, cnt)
}
}

fun main() {
연구소().solve()
}
61 changes: 61 additions & 0 deletions src/main/kotlin/heejik/28week/친구비.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package heejik.`28week`

import kotlin.math.min
import kotlin.properties.Delegates

class 친구비 {

private var n by Delegates.notNull<Int>()
private var m by Delegates.notNull<Int>()
private var k by Delegates.notNull<Int>()
private lateinit var cost: MutableList<Int>
private lateinit var parent: MutableList<Int>

fun solve() {
setting()
printAnswer()
}

private fun setting() {
readln().split(' ').map { it.toInt() }.run {
n = this[0]
m = this[1]
k = this[2]
}
cost = readln().split(' ').map { it.toInt() }.toMutableList()
parent = MutableList(n) { it }

repeat(m) {
val (v, w) = readln().split(' ').map { it.toInt() }.sorted()
union(v - 1, w - 1)
}
}

private fun printAnswer() {
var answer = 0

parent.map { find(it) }.toSet().forEach {
answer += cost[it]
}
println(if (answer > k) "Oh no" else answer)
}

private fun find(v: Int): Int {
if (parent[v] == v) return v
return find(parent[v])
}

private fun union(v: Int, w: Int) {
val p1 = find(v)
val p2 = find(w)

parent[p2] = p1
val minCost = min(cost[p2], cost[p1])
cost[p1] = minCost
}
}


fun main() {
친구비().solve()
}

0 comments on commit b3e0c3c

Please sign in to comment.