Skip to content

Commit

Permalink
Merge pull request #124 from wellFoundedDevelopers/byeonghee/28week
Browse files Browse the repository at this point in the history
[소병희] 연구소, 미세먼지 안녕!, 친구비, 드래곤 커브
  • Loading branch information
bngsh authored Mar 26, 2023
2 parents 65e3fa3 + fa3db3e commit ab226cb
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/kotlin/byeonghee/week28/드래곤커브.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package byeonghee.week28

import java.io.*
import java.util.*
import kotlin.math.pow

class 소병희_드래곤커브 {

companion object {
val dr = arrayOf(0, -1, 0, 1)
val dc = arrayOf(1, 0, -1, 0)

fun solve(): Unit = with(BufferedReader(InputStreamReader(System.`in`))) {
val n = readLine().toInt()
val isDragon = Array(101) { BooleanArray(101) }
val curves = LinkedList<Int>()
var answer = 0

var r = 0
var c = 0
var d = 0
var g = 0

curves.add(0)
repeat(n) {
readLine().split(" ").let {
c = it[0].toInt()
r = it[1].toInt()
d = it[2].toInt()
g = 2.0.pow(it[3].toInt()).toInt()
}

while(curves.size < g) {
for(i in curves.size - 1 downTo 0) {
curves.add((curves[i] + 1) % 4)
}
}

isDragon[r][c] = true
for(i in 0 until g) {
r += dr[(d + curves[i]) % 4]
c += dc[(d + curves[i]) % 4]
isDragon[r][c] = true
}
}

for(i in 0 until 100) for(j in 0 until 100) {
if (isDragon[i][j] && isDragon[i][j + 1] && isDragon[i + 1][j] && isDragon[i + 1][j + 1]) answer++
}

print(answer)
}
}
}

fun main() {
소병희_드래곤커브.solve()
}
96 changes: 96 additions & 0 deletions src/main/kotlin/byeonghee/week28/미세먼지안녕.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package byeonghee.week28

import java.io.StreamTokenizer

class 소병희_미세먼지안녕 {

companion object {
val dr = arrayOf(-1, 0, 1, 0)
val dc = arrayOf(0, 1, 0, -1)

fun solve() = StreamTokenizer(System.`in`.bufferedReader()).run {
fun input(): Int {
nextToken()
return nval.toInt()
}

val r = input()
val c = input()
val t = input()
var purifier = 0
val rRange = Array(2) { 0 .. 0 }
val room = Array(r) { IntArray(c) { input() } }
val nroom = Array(r) { IntArray(c) }
val iniroom = Array(r) { IntArray(c) }

for(i in 0 until r) {
if(room[i][0] == -1) {
purifier = i
room[i][0] = 0
room[i + 1][0] = 0
rRange[0] = 0 .. i
rRange[1] = i + 1 until r
break
}
}

fun copy(a: Array<IntArray>, b: Array<IntArray>) {
for(i in 0 until r) for(j in 0 until c) {
a[i][j] = b[i][j]
}
}

var spreadCnt : Int
var spreadAmt : Int
var ni = 0
var nj = 0
var flip = 1

repeat(t) {
copy(nroom, iniroom)
for(i in 0 until r) for(j in 0 until c) {
if (room[i][j] <= 0) continue

spreadCnt = 0
spreadAmt = room[i][j] / 5
for(d in 0 until 4) {
ni = i + dr[d]
nj = j + dc[d]
if (ni !in 0 until r || nj !in 0 until c) continue
if (nj == 0 && ni in purifier .. purifier + 1) continue
spreadCnt++
nroom[ni][nj] += spreadAmt
}
nroom[i][j] += room[i][j] - spreadCnt * spreadAmt
}

for(p in 0 .. 1) {
ni = purifier + p
nj = 0
for(d in 0 until 4) {
while(ni + dr[d] * flip in rRange[p] && nj + dc[d] in 0 until c) {
nroom[ni][nj] = nroom[ni + dr[d] * flip][nj + dc[d]]
ni += dr[d] * flip
nj += dc[d]
}
}
nroom[purifier + p][1] = 0
nroom[purifier + p][0] = 0
flip *= -1
}

copy(room, nroom)
}

var answer = 0
for(i in 0 until r) for(j in 0 until c) {
answer += room[i][j]
}
print(answer)
}
}
}

fun main() {
소병희_미세먼지안녕.solve()
}
82 changes: 82 additions & 0 deletions src/main/kotlin/byeonghee/week28/연구소.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package byeonghee.week28

import java.io.StreamTokenizer

class 소병희_연구소 {

companion object {
const val BLANK = 0
const val WALL = 1
const val VIRUS = 2

val dr = arrayOf(0, 1, 0, -1)
val dc = arrayOf(1, 0, -1, 0)

fun solve() = StreamTokenizer(System.`in`.bufferedReader()).run {

fun input() : Int {
nextToken()
return nval.toInt()
}

val n = input()
val m = input()
val lab = Array(n) { IntArray(m) }
var preSafe = n * m - 3
var answer = 0
var nr : Int
var nc : Int

for(i in 0 until n) for(j in 0 until m) {
lab[i][j] = input()
if (lab[i][j] == WALL) preSafe--
}

fun spreadVirus() {
val visited = Array(n) { BooleanArray(m) }
val q = ArrayDeque<Pair<Int, Int>>()
var safeArea = preSafe

for(r in 0 until n) for(c in 0 until m) {
if (lab[r][c] == VIRUS) q.add(Pair(r, c))
}

while(q.isNotEmpty()) {
val (r, c) = q.removeFirst()
if (visited[r][c]) continue

visited[r][c] = true
if (--safeArea == 0) break
for(d in 0 until 4) {
nr = r + dr[d]
nc = c + dc[d]
if (nr !in 0 until n || nc !in 0 until m) continue
if (visited[nr][nc] || lab[nr][nc] == WALL) continue
q.add(Pair(nr, nc))
}
}

answer = answer.coerceAtLeast(safeArea)
}

fun putUpWall(i: Int, rest: Int) {
if (rest == 0) return spreadVirus()

if (lab[i / m][i % m] == BLANK) {
lab[i / m][i % m] = WALL
putUpWall(i + 1, rest - 1)
lab[i / m][i % m] = BLANK
}

if (i + rest < n * m) putUpWall(i + 1, rest)
}

putUpWall(0, 3)
print(answer)
}
}
}

fun main() {
소병희_연구소.solve()
}
50 changes: 50 additions & 0 deletions src/main/kotlin/byeonghee/week28/친구비.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package byeonghee.week28

import java.io.*

class 소병희_친구비 {

companion object {
fun solve() : Unit = with(BufferedReader(InputStreamReader(System.`in`))) {

val (n, m, k) = readLine().split(" ").map { it.toInt() }
val cost = IntArray(n + 1)
val parent = IntArray(n + 1) { it }
var answer = 0

fun getParent(_x: Int): Int {
var x = _x
while(parent[x] != x) {
x = parent[x]
}
return x
}

var idx = 1
readLine().split(" ").forEach {
cost[idx++] = it.toInt()
}

repeat(m) {
val (p1, p2) = readLine().split(" ").map { getParent(it.toInt()) }
if (p1 != p2) {
parent[p2] = p1
cost[p1] = cost[p1].coerceAtMost(cost[p2])
}
}

for(i in 1 .. n) {
if (parent[i] == i) answer += cost[i]
if (answer > k) {
print("Oh no")
kotlin.system.exitProcess(0)
}
}
print(answer)
}
}
}

fun main() {
소병희_친구비.solve()
}

0 comments on commit ab226cb

Please sign in to comment.