Skip to content
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

[Week5] 안찬우 : 미로만들기, 아기상어, 쿼드트리, 흙길보수하기 #25

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/4week/acw/근손실.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package `4week`.acw

class `acw근손실` {
lateinit var exerciseKits: List<Int>
lateinit var visit: Array<Boolean>
var cnt = 0


fun makePermutation(arr: MutableList<Int>, power: Int, day: Int, N: Int, K: Int) {
//순열구성하기
var powerNow = power
if (arr.isNotEmpty()) {
powerNow = power + arr.last() - K
}

if (powerNow < 500) {
return
}

if (day == N) {
cnt++
return
}

for (i in 0 until N) {
if (visit[i]) {
continue
}

arr.add(exerciseKits[i])
visit[i] = true
makePermutation(arr, powerNow, day + 1, N, K)
arr.remove(exerciseKits[i])
visit[i] = false
}
}


fun solution() {
val (N, K) = readln().split(" ").map { it.toInt() }
exerciseKits = readln().split(" ").map { it.toInt() }
visit = Array(N) { false }
makePermutation(mutableListOf(), 500, 0, N, K)

println(cnt)

}
}

fun main() {
val sol = `acw근손실`()
sol.solution()
}

58 changes: 58 additions & 0 deletions src/4week/acw/지름길.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package `4week`.acw


import java.util.*

class `acw지름길` {
lateinit var distance: Array<Int>
var roads = PriorityQueue(Comparator<Triple<Int, Int, Int>> { a, b ->
a.first - b.first
})


fun solution() {
val (K, D) = readln().split(" ").map { it.toInt() }
distance = Array(10001) { it }


for (i in 1..K) {
val (s, e, c) = readln().split(" ").map { it.toInt() }

if (e > D) {
continue
}
//목표보다 높은 지점을 향하는 지름길은 받지 않기

if (c >= (e - s)) {
continue
}
//지름길이 cost가 더 클경우도 받지 않기

roads.add(Triple(s, e, c))
}

while (roads.isNotEmpty()) {
val (s, e, c) = roads.poll()
if (distance[e] - distance[s] > c) {
distance[e] = distance[s] + c
for (j in e + 1..D) {
if (distance[j - 1] + 1 < distance[j]) {
distance[j] = distance[j - 1] + 1
}
}
// 지름길을 순회하며 더 최소값일 경우 그 뒤의 node들에 대해서 값 갱신
}
}



println(distance[D])


}
}

fun main() {
val sol = `acw지름길`()
sol.solution()
}
120 changes: 120 additions & 0 deletions src/4week/acw/톱니바퀴.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package `4week`.acw


import kotlin.math.pow

//시계방향은 오른쪽shift 반시계는 왼쪽 shift
const val RIGHT = 5
const val LEFT = 1

class `acw톱니바퀴` {

var wheel = Array(4) { 0 }
var visit = Array(4) { false }
var turn = arrayListOf<Pair<Int, Int>>()


fun addTurn(w: Int, turnD: Int) {
//dfs로 들어가면서 회전해야할 것들 turn에 추가해주기


var wheelNowRight = if (wheel[w] and (1 shl RIGHT) != 0) 1 else 0
var wheelNowLeft = if (wheel[w] and (1 shl LEFT) != 0) 1 else 0

if (w - 1 >= 0) {
val leftWheelsRight = if (wheel[w - 1] and (1 shl RIGHT) != 0) 1 else 0

if (wheelNowLeft != leftWheelsRight && !visit[w - 1]) {
visit[w - 1] = true
turn.add(Pair(w - 1, turnD * -1))
addTurn(w - 1, turnD * -1)
}
}

if (w + 1 < 4) {
val rightWheelsLeft = if (wheel[w + 1] and (1 shl LEFT) != 0) 1 else 0
if (wheelNowRight != rightWheelsLeft && !visit[w + 1]) {
visit[w + 1] = true
turn.add(Pair(w + 1, turnD * -1))
addTurn(w + 1, turnD * -1)
}
}


}

fun turnWheel() {

while (turn.isNotEmpty()) {
val (w, d) = turn.removeFirst()

if (d == -1) {
//반시계
val last = wheel[w] and (1 shl 7)

wheel[w] = (wheel[w] shl 1) and 255
if (last != 0) {
wheel[w]++
}

} else {
//시계
val first = wheel[w] and 1

wheel[w] = wheel[w] shr 1

if (first != 0) {
wheel[w] += 1 shl 7
}
}


}

}


fun solution() {
repeat(4) {
var input = readln()
var m = 128
var num = 0
for (i in input) {
num += (i.digitToInt() * m)
m /= 2
}//숫자로 바꿔서 저장
wheel[it] = num

}

val turnCnt = readln().toInt()
repeat(turnCnt) {
val (w, d) = readln().split(" ").map { it.toInt() }
turn.add(Pair(w - 1, d))
visit[w - 1] = true


addTurn(w - 1, d)//회전해야할 것들 turn List에 추가해주기
turnWheel()// turn List에서 빼서 회전시켜주기
visit = Array(4) { false } // visit은 다시 사용해야함으로 다시 false로 초기화
}


var answer = 0
for (i in 0 until 4) {
if ((wheel[i] and (1 shl 7)) != 0) {
answer += (2.0).pow(i).toInt()
}
}


println(answer)


}
}

fun main() {
val sol = `acw톱니바퀴`()
sol.solution()
}
42 changes: 42 additions & 0 deletions src/4week/acw/트리의부모찾기.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package `4week`.acw

class `acw트리의부모찾기` {
lateinit var treeMap: Array<MutableList<Int>>
lateinit var parent: Array<Int>
var N = 0

fun dfs(p: Int) {
//p로 전달받은 노드는 부모란 뜻이니까 연결된 노드들은 자식으로 처리해주기
//dfs로 이어가면서 같은 처리 해주기
for (child in treeMap[p]) {
parent[child] = p
treeMap[child].remove(p)
dfs(child)
}
}


fun solution() {
N = readln().toInt()
treeMap = Array(N + 1) { mutableListOf() }
parent = Array(N + 1) { 0 }

for (i in 1 until N) {
val (a, b) = readln().split(" ").map { it.toInt() }
treeMap[a].add(b)
treeMap[b].add(a)
//일단 입력받을 경우 두 node에 대해 상호 연결하기
}

dfs(1)
for (i in 2..N) {
println(parent[i])
}

}
}

fun main() {
val sol = `acw트리의부모찾기`()
sol.solution()
}
55 changes: 55 additions & 0 deletions src/4week/acw/회의실배정.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package `4week`.acw


import java.util.PriorityQueue

class `acw회의실배정` {
val conferance = PriorityQueue<Pair<Int, Int>>(
Comparator { a, b ->
if (a.second - b.second == 0) {
a.first - b.first
} else {
a.second - b.second
}
}

)
// 종료시간이 빠른것을 기준으로 정렬한다.
// 종료시간이 같을 경우 시작시간이 더 빠른것을 위주로 정렬해야한다. -> 시작시간과 종료시간이 같은 회의가 있을 수 있기 때문
// ex) 5,6 / 6,6 이 있는데 6,6이 먼저 뽑힐 경우 56 /66이 모두 사용될 수 있음에도 불구하고 56은 선택되지 못한다.

fun solution() {
val N = readln().toInt()
var answer = 0
var lastConferance = Pair(0, 0)

repeat(N) {
val (s, e) = readln().split(" ").map { it.toInt() }
conferance.add(Pair(s, e))

}
lastConferance = conferance.poll()
answer++


while (conferance.isNotEmpty()) {
val nowConferance = conferance.poll()
if (nowConferance.first < lastConferance.second) {
continue
} else {
lastConferance = nowConferance
answer++
}

}
println(answer)


}

}

fun main() {
val sol = `acw회의실배정`()
sol.solution()
}
Loading