From fd3477f6fef99db431feb7ea1d87601f413108c7 Mon Sep 17 00:00:00 2001 From: acw Date: Mon, 24 Oct 2022 20:59:51 +0900 Subject: [PATCH 1/5] 4week acw --- .../\352\267\274\354\206\220\354\213\244.kt" | 54 ++++++++ .../\354\247\200\353\246\204\352\270\270.kt" | 76 +++++++++++ ...61\353\213\210\353\260\224\355\200\264.kt" | 122 ++++++++++++++++++ ...00\353\252\250\354\260\276\352\270\260.kt" | 39 ++++++ 4 files changed, 291 insertions(+) create mode 100644 "src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" create mode 100644 "src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" create mode 100644 "src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" create mode 100644 "src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" diff --git "a/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" new file mode 100644 index 0000000..61f5be6 --- /dev/null +++ "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" @@ -0,0 +1,54 @@ +package `4week`.acw + +class `acw근손실`{ + lateinit var exerciseKits:List + lateinit var visit:Array + var cnt=0 + + + + fun makeComb(arr:MutableList,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 + makeComb(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} + makeComb(mutableListOf(),500,0,N,K) + + println(cnt) + + } +} +fun main() { + val sol = `acw근손실`() + sol.solution() +} + diff --git "a/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" new file mode 100644 index 0000000..2fe25c7 --- /dev/null +++ "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" @@ -0,0 +1,76 @@ +package `4week`.acw + + +import java.util.* + +class `acw지름길`{ + lateinit var distance:Array + var roads= arrayListOf>() + + fun solution(){ + val (K,D)=readln().split(" ").map{it.toInt()} + distance=Array(10001){it} + + var now=0 + var nowCost=0 + + for(i in 1..K){ + val (s,e,c)=readln().split(" ").map{it.toInt()} + + if(e>D){ + continue + } + + if(c>=(e-s)){ + continue + } + + roads.add(Triple(s,e,c)) + } + + while(roads.isNotEmpty()){ + var now=roads.first() + var cost=now.second-now.first-now.third + var nowRange=now.first..now.second + val arr= LinkedList>() + + for(i in 1 until roads.size){ + val (s,e,c)=roads[i] + if(s in nowRange || e in nowRange){ + arr.add(Triple(s,e,c)) + } + } + arr.add(now) + for(i in 0 until arr.size){ + var costNow=arr[i].second-arr[i].first-arr[i].third + for(j in i+1 until arr.size){ + + if(arr[i].second<=arr[j].first || arr[i].first>=arr[j].second){ + costNow+=(arr[j].second-arr[j].first-arr[j].third) + } + } + if(costNow>cost){ + cost=costNow + } + + }//겹치는 것들 중 구성할 수 있는 최대 값 구성 + distance[D]-=cost + + + roads.removeAll(arr) + + + } + + + + + println(distance[D]) + + + } +} +fun main(){ + val sol =`acw지름길`() + sol.solution() +} diff --git "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" new file mode 100644 index 0000000..5cc3afd --- /dev/null +++ "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" @@ -0,0 +1,122 @@ +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>() + + + fun addTurn(w:Int,turnD:Int){ + + + + 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 + println("$w $wheelNowLeft $wheelNowRight") + + 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 + 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,d)) + visit[w]=true + + + addTurn(w,d) + println(turn) + turnWheel() + for( i in wheel){ + println(i) + } + visit=Array(4){false} + } + + + var answer=0 + for(i in 0 until 4){ + if((wheel[i] and (1 shr 7))==0){ + answer+=(2.0).pow(i).toInt() + } + } + + + println(answer) + + + + } +} +fun main(){ + val sol=`acw톱니바퀴`() + sol.solution() +} diff --git "a/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" new file mode 100644 index 0000000..88c2b76 --- /dev/null +++ "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" @@ -0,0 +1,39 @@ +package `4week`.acw + +class `acw트리의부모찾기`{ + lateinit var treeMap:Array> + lateinit var parent:Array + var N=0 + + fun dfs(p:Int){ + 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) + } + + dfs(1) + for(i in 2..N){ + println(parent[i]) + } + + } +} +fun main(){ + val sol=`acw트리의부모찾기`() + sol.solution() +} From d0f913e70f543e68a382f09851cf59c4ada43c10 Mon Sep 17 00:00:00 2001 From: acw Date: Wed, 26 Oct 2022 04:30:57 +0900 Subject: [PATCH 2/5] =?UTF-8?q?=EC=A7=80=EB=A6=84=EA=B8=B8=20=ED=95=B4?= =?UTF-8?q?=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\352\267\274\354\206\220\354\213\244.kt" | 44 +++---- .../\354\247\200\353\246\204\352\270\270.kt" | 74 +++++------ ...61\353\213\210\353\260\224\355\200\264.kt" | 116 +++++++++--------- ...00\353\252\250\354\260\276\352\270\260.kt" | 37 +++--- 4 files changed, 128 insertions(+), 143 deletions(-) diff --git "a/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" index 61f5be6..728b295 100644 --- "a/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" +++ "b/src/4week/acw/\352\267\274\354\206\220\354\213\244.kt" @@ -1,52 +1,52 @@ package `4week`.acw -class `acw근손실`{ - lateinit var exerciseKits:List - lateinit var visit:Array - var cnt=0 +class `acw근손실` { + lateinit var exerciseKits: List + lateinit var visit: Array + var cnt = 0 - - fun makeComb(arr:MutableList,power:Int,day:Int,N:Int,K:Int){ - var powerNow=power - if(arr.isNotEmpty()){ - powerNow=power+arr.last()-K + fun makePermutation(arr: MutableList, power: Int, day: Int, N: Int, K: Int) { + //순열구성하기 + var powerNow = power + if (arr.isNotEmpty()) { + powerNow = power + arr.last() - K } - if(powerNow<500) - { + if (powerNow < 500) { return } - if(day==N){ + if (day == N) { cnt++ return } - for(i in 0 until N){ - if(visit[i]){ + for (i in 0 until N) { + if (visit[i]) { continue } arr.add(exerciseKits[i]) - visit[i]=true - makeComb(arr,powerNow,day+1,N,K) + visit[i] = true + makePermutation(arr, powerNow, day + 1, N, K) arr.remove(exerciseKits[i]) - visit[i]=false + visit[i] = false } } - fun solution(){ - val (N,K)=readln().split(" ").map { it.toInt() } - exerciseKits=readln().split(" ").map { it.toInt() } - visit=Array(N){false} - makeComb(mutableListOf(),500,0,N,K) + 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() diff --git "a/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" index 2fe25c7..a84e21f 100644 --- "a/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" +++ "b/src/4week/acw/\354\247\200\353\246\204\352\270\270.kt" @@ -3,74 +3,56 @@ package `4week`.acw import java.util.* -class `acw지름길`{ - lateinit var distance:Array - var roads= arrayListOf>() +class `acw지름길` { + lateinit var distance: Array + var roads = PriorityQueue(Comparator> { a, b -> + a.first - b.first + }) - fun solution(){ - val (K,D)=readln().split(" ").map{it.toInt()} - distance=Array(10001){it} - var now=0 - var nowCost=0 + 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){ + for (i in 1..K) { + val (s, e, c) = readln().split(" ").map { it.toInt() } + + if (e > D) { continue } + //목표보다 높은 지점을 향하는 지름길은 받지 않기 - if(c>=(e-s)){ + if (c >= (e - s)) { continue } + //지름길이 cost가 더 클경우도 받지 않기 - roads.add(Triple(s,e,c)) + roads.add(Triple(s, e, c)) } - while(roads.isNotEmpty()){ - var now=roads.first() - var cost=now.second-now.first-now.third - var nowRange=now.first..now.second - val arr= LinkedList>() - - for(i in 1 until roads.size){ - val (s,e,c)=roads[i] - if(s in nowRange || e in nowRange){ - arr.add(Triple(s,e,c)) - } - } - arr.add(now) - for(i in 0 until arr.size){ - var costNow=arr[i].second-arr[i].first-arr[i].third - for(j in i+1 until arr.size){ - - if(arr[i].second<=arr[j].first || arr[i].first>=arr[j].second){ - costNow+=(arr[j].second-arr[j].first-arr[j].third) + 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 } } - if(costNow>cost){ - cost=costNow - } - - }//겹치는 것들 중 구성할 수 있는 최대 값 구성 - distance[D]-=cost - - - roads.removeAll(arr) - - + // 지름길을 순회하며 더 최소값일 경우 그 뒤의 node들에 대해서 값 갱신 + } } - println(distance[D]) } } -fun main(){ - val sol =`acw지름길`() + +fun main() { + val sol = `acw지름길`() sol.solution() } diff --git "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" index 5cc3afd..deddd9b 100644 --- "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" +++ "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" @@ -4,65 +4,65 @@ package `4week`.acw import kotlin.math.pow //시계방향은 오른쪽shift 반시계는 왼쪽 shift -const val RIGHT=5 -const val LEFT=1 +const val RIGHT = 5 +const val LEFT = 1 -class `acw톱니바퀴`{ - var wheel=Array(4){0} - var visit=Array(4){false} - var turn= arrayListOf>() +class `acw톱니바퀴` { + var wheel = Array(4) { 0 } + var visit = Array(4) { false } + var turn = arrayListOf>() - fun addTurn(w:Int,turnD:Int){ + fun addTurn(w: Int, turnD: Int) { - - 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 + 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 println("$w $wheelNowLeft $wheelNowRight") - 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 > 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) + 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() + fun turnWheel() { + + while (turn.isNotEmpty()) { + val (w, d) = turn.removeFirst() - if(d==-1){ + if (d == -1) { //반시계 - val last=wheel[w] and (1 shl 7) + val last = wheel[w] and (1 shl 7) - wheel[w]=wheel[w] shl 1 - if(last!=0){ + wheel[w] = wheel[w] shl 1 + if (last != 0) { wheel[w]++ } - }else{ + } else { //시계 - val first=wheel[w] and 1 + val first = wheel[w] and 1 - wheel[w]=wheel[w] shr 1 + wheel[w] = wheel[w] shr 1 - if(first!=0){ - wheel[w]+=1 shl 7 + if (first != 0) { + wheel[w] += 1 shl 7 } } @@ -72,40 +72,40 @@ class `acw톱니바퀴`{ } - fun solution(){ - repeat(4){ - var input=readln() - var m=128 - var num=0 - for(i in input){ - num+=(i.digitToInt()*m) - m/=2 + 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 + wheel[it] = num } - val turnCnt=readln().toInt() - repeat(turnCnt){ - val (w,d)=readln().split(" ").map{it.toInt()} - turn.add(Pair(w,d)) - visit[w]=true + val turnCnt = readln().toInt() + repeat(turnCnt) { + val (w, d) = readln().split(" ").map { it.toInt() } + turn.add(Pair(w, d)) + visit[w] = true - addTurn(w,d) + addTurn(w, d) println(turn) turnWheel() - for( i in wheel){ + for (i in wheel) { println(i) } - visit=Array(4){false} + visit = Array(4) { false } } - var answer=0 - for(i in 0 until 4){ - if((wheel[i] and (1 shr 7))==0){ - answer+=(2.0).pow(i).toInt() + var answer = 0 + for (i in 0 until 4) { + if ((wheel[i] and (1 shr 7)) == 0) { + answer += (2.0).pow(i).toInt() } } @@ -113,10 +113,10 @@ class `acw톱니바퀴`{ println(answer) - } } -fun main(){ - val sol=`acw톱니바퀴`() + +fun main() { + val sol = `acw톱니바퀴`() sol.solution() } diff --git "a/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" index 88c2b76..61e7300 100644 --- "a/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" +++ "b/src/4week/acw/\355\212\270\353\246\254\354\235\230\353\266\200\353\252\250\354\260\276\352\270\260.kt" @@ -1,12 +1,14 @@ package `4week`.acw -class `acw트리의부모찾기`{ - lateinit var treeMap:Array> - lateinit var parent:Array - var N=0 - - fun dfs(p:Int){ - for(child in treeMap[p]) { +class `acw트리의부모찾기` { + lateinit var treeMap: Array> + lateinit var parent: Array + var N = 0 + + fun dfs(p: Int) { + //p로 전달받은 노드는 부모란 뜻이니까 연결된 노드들은 자식으로 처리해주기 + //dfs로 이어가면서 같은 처리 해주기 + for (child in treeMap[p]) { parent[child] = p treeMap[child].remove(p) dfs(child) @@ -14,26 +16,27 @@ class `acw트리의부모찾기`{ } + fun solution() { + N = readln().toInt() + treeMap = Array(N + 1) { mutableListOf() } + parent = Array(N + 1) { 0 } - 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() } + 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){ + for (i in 2..N) { println(parent[i]) } } } -fun main(){ - val sol=`acw트리의부모찾기`() + +fun main() { + val sol = `acw트리의부모찾기`() sol.solution() } From 49945cc258465c9fbae1d7619a55be4e1f66e888 Mon Sep 17 00:00:00 2001 From: acw Date: Wed, 26 Oct 2022 05:17:45 +0900 Subject: [PATCH 3/5] =?UTF-8?q?=ED=86=B1=EB=8B=88=EB=B0=94=ED=80=B4=20=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...61\353\213\210\353\260\224\355\200\264.kt" | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" index deddd9b..b7d00e6 100644 --- "a/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" +++ "b/src/4week/acw/\355\206\261\353\213\210\353\260\224\355\200\264.kt" @@ -8,20 +8,22 @@ const val RIGHT = 5 const val LEFT = 1 class `acw톱니바퀴` { + var wheel = Array(4) { 0 } var visit = Array(4) { false } var turn = arrayListOf>() 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 - println("$w $wheelNowLeft $wheelNowRight") - if (w - 1 > 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)) @@ -30,7 +32,7 @@ class `acw톱니바퀴` { } if (w + 1 < 4) { - val rightWheelsLeft = if (wheel[w - 1] and (1 shl LEFT) != 0) 1 else 0 + 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)) @@ -50,7 +52,7 @@ class `acw톱니바퀴` { //반시계 val last = wheel[w] and (1 shl 7) - wheel[w] = wheel[w] shl 1 + wheel[w] = (wheel[w] shl 1) and 255 if (last != 0) { wheel[w]++ } @@ -80,7 +82,7 @@ class `acw톱니바퀴` { for (i in input) { num += (i.digitToInt() * m) m /= 2 - } + }//숫자로 바꿔서 저장 wheel[it] = num } @@ -88,23 +90,19 @@ class `acw톱니바퀴` { val turnCnt = readln().toInt() repeat(turnCnt) { val (w, d) = readln().split(" ").map { it.toInt() } - turn.add(Pair(w, d)) - visit[w] = true + turn.add(Pair(w - 1, d)) + visit[w - 1] = true - addTurn(w, d) - println(turn) - turnWheel() - for (i in wheel) { - println(i) - } - visit = Array(4) { false } + 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 shr 7)) == 0) { + if ((wheel[i] and (1 shl 7)) != 0) { answer += (2.0).pow(i).toInt() } } From ba1cad0390cb1e89f799d84eedb743f128605584 Mon Sep 17 00:00:00 2001 From: acw Date: Wed, 26 Oct 2022 06:12:12 +0900 Subject: [PATCH 4/5] =?UTF-8?q?=ED=9D=AC=EC=9D=B4=EC=8B=A4=EB=B0=B0?= =?UTF-8?q?=EC=A0=95=20=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...30\354\213\244\353\260\260\354\240\225.kt" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" diff --git "a/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" "b/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" new file mode 100644 index 0000000..5af8b19 --- /dev/null +++ "b/src/4week/acw/\355\232\214\354\235\230\354\213\244\353\260\260\354\240\225.kt" @@ -0,0 +1,55 @@ +package `4week`.acw + + +import java.util.PriorityQueue + +class `acw회의실배정` { + val conferance = PriorityQueue>( + 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() +} From 157670d3f94251710bb5d608f965d150b512a62a Mon Sep 17 00:00:00 2001 From: acw Date: Mon, 31 Oct 2022 14:04:24 +0900 Subject: [PATCH 5/5] =?UTF-8?q?=EC=88=98=EA=B2=8C=EC=9E=84=20=EC=A0=9C?= =?UTF-8?q?=EC=99=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...34\353\247\214\353\223\244\352\270\260.kt" | 89 +++++++++ ...04\352\270\260\354\203\201\354\226\264.kt" | 178 ++++++++++++++++++ ...74\353\223\234\355\212\270\353\246\254.kt" | 48 +++++ ...64\354\210\230\355\225\230\352\270\260.kt" | 44 +++++ 4 files changed, 359 insertions(+) create mode 100644 "src/5week/acw/\353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.kt" create mode 100644 "src/5week/acw/\354\225\204\352\270\260\354\203\201\354\226\264.kt" create mode 100644 "src/5week/acw/\354\277\274\353\223\234\355\212\270\353\246\254.kt" create mode 100644 "src/5week/acw/\355\235\231\352\270\270\353\263\264\354\210\230\355\225\230\352\270\260.kt" diff --git "a/src/5week/acw/\353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.kt" "b/src/5week/acw/\353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.kt" new file mode 100644 index 0000000..1be475e --- /dev/null +++ "b/src/5week/acw/\353\257\270\353\241\234\353\247\214\353\223\244\352\270\260.kt" @@ -0,0 +1,89 @@ +package `5week`.acw + + +const val WEST = 0 +const val SOUTH = 1 +const val EAST = 2 +const val NORTH = 3 + +class `acw미로만들기` { + var map = Array(101) { Array(101) { "#" } } + + fun solution() { + readln() + val mv = readln() + var directionNow = SOUTH + + var ny = 50 + var nx = 50 + + map[ny][nx] = "." + + for (i in mv) { + when (i) { + 'R' -> { + directionNow-- + if (directionNow == -1) { + directionNow = NORTH + } + + } + + 'L' -> { + directionNow = (directionNow + 1) % 4 + } + + 'F' -> { + when (directionNow) { + WEST -> { + nx-- + } + + EAST -> { + nx++ + } + + SOUTH -> { + ny++ + } + + NORTH -> { + ny-- + } + } + map[ny][nx] = "." + } + } + } + + var maxX = 50 + var maxY = 50 + var minX = 50 + var minY = 50 + + for (i in 0..100) { + for (j in 0..100) { + if (map[i][j] == ".") { + maxX = maxX.coerceAtLeast(j) + maxY = maxY.coerceAtLeast(i) + minX = minX.coerceAtMost(j) + minY = minY.coerceAtMost(i) + } + } + } + + for (i in minY..maxY) { + for (j in minX..maxX) { + print(map[i][j]) + } + println() + } + + + } +} + +fun main() { + val sol = `acw미로만들기`() + sol.solution() +} diff --git "a/src/5week/acw/\354\225\204\352\270\260\354\203\201\354\226\264.kt" "b/src/5week/acw/\354\225\204\352\270\260\354\203\201\354\226\264.kt" new file mode 100644 index 0000000..897beee --- /dev/null +++ "b/src/5week/acw/\354\225\204\352\270\260\354\203\201\354\226\264.kt" @@ -0,0 +1,178 @@ +package `5week`.acw + + +class `acw아기상어` { + lateinit var m: Array> + lateinit var visits: Array> + + + var q = arrayListOf>() + + val dx = arrayOf(0, -1, 1, 0) + val dy = arrayOf(-1, 0, 0, 1) + var answer = 0 + var sizeOfShark = 2 + var fishEaten = 0 + var foods = 0 + + fun solution() { + var x = 0; + var y = 0//상어의 위치 + val N = readln().toInt() + m = Array(N) { Array(N) { 0 } } + visits = Array(N) { Array(N) { false } } + + for (i in 0 until N) { + m[i] = readln().split(" ").map { it.toInt() }.toTypedArray() + } + + for (i in 0 until N) { + for (j in 0 until N) { + if (m[i][j] == 9) { + y = i + x = j + } else if (m[i][j] > 0) { + foods++ + } + } + }//상어 위치와 음식 개수 세기 + + q.add(Triple(y, x, 0)) + visits[y][x] = true + m[y][x] = 0 + + var nx = 0; + var ny = 0; + var cost = 0 + + var ex = 100; + var ey = 100; + var costExpected = 100 + //상어가 먹을 물고기의 위치를 임시 저장할 변수 + + while (foods > 0) { + if (q.isEmpty()) { + //자기보다 몸집이 큰 물고기때문에 bfs도중 q가 비어버릴때 + if (ey != 100) { + //만약 q가 비었는데 비기 전 먹을 수 있는 물고기의 위치가 있다면 먹고나서 상어의 사이즈가 커지면서 bfs를 더 돌릴가능성이 생기므로 먹고 다시 bfs를 돌려준다. + y = ey + x = ex + cost = costExpected + + + m[y][x] = 0 + foods-- + fishEaten++ + + if (fishEaten == sizeOfShark) { + sizeOfShark++ + fishEaten = 0 + //상어 size up + } + + q = arrayListOf() + q.add(Triple(y, x, 0)) + + visits = Array(N) { Array(N) { false } } + visits[y][x] = true + + answer += (cost) + + ey = 100 + ex = 100 + costExpected = 100 + } else { + break + } + } else { + y = q.first().first + x = q.first().second + cost = q.first().third + q.removeFirst() + + if (ey != 100 && costExpected != cost) { + //bfs depth가 바뀌었는데 ey에 값이 기록되어있을 경우에는 먹을 수 있는 물고기가 있다는 뜻이므로 먹어준다. + //그리고 먹어준 자리부터 다시 탐색을 시작한다. + y = ey + x = ex + cost = costExpected + + + m[y][x] = 0 + foods-- + fishEaten++ + + if (fishEaten == sizeOfShark) { + sizeOfShark++ + fishEaten = 0 + //상어 size up + } + + q = arrayListOf() + + visits = Array(N) { Array(N) { false } } + visits[y][x] = true + + answer += (cost) + cost = 0 + + ey = 100 + ex = 100 + costExpected = 100 + //bfs다시 돌리기 위해서 초기값 다시 set + } + + if (m[y][x] in 1 until sizeOfShark) { + //먹을 수 있는 물고기라면 일단 그 물고기의 위치와 cost를 ey ex costExpected에 기록해둔다. + if (y < ey) { + //가장 위/왼쪽에 있는 물고기를 먹으라 했으므로 + ey = y + ex = x + costExpected = cost + } else if (y == ey && x < ex) { + ey = y + ex = x + costExpected = cost + } + + } + + for (i in 0 until 4) { + nx = x + dx[i] + ny = y + dy[i] + + if (nx < 0 || ny < 0 || nx >= N || ny >= N) { + continue + } + if (visits[ny][nx]) { + continue + } + + if (m[ny][nx] > sizeOfShark) { + continue + } else if (m[ny][nx] <= sizeOfShark || m[ny][nx] >= 0) { + q.add(Triple(ny, nx, cost + 1)) + visits[ny][nx] = true + } + + + } + + + } + + + } + + + + println(answer) + + + } +} + +fun main() { + val sol = `acw아기상어`() + sol.solution() +} \ No newline at end of file diff --git "a/src/5week/acw/\354\277\274\353\223\234\355\212\270\353\246\254.kt" "b/src/5week/acw/\354\277\274\353\223\234\355\212\270\353\246\254.kt" new file mode 100644 index 0000000..cd71349 --- /dev/null +++ "b/src/5week/acw/\354\277\274\353\223\234\355\212\270\353\246\254.kt" @@ -0,0 +1,48 @@ +package `5week`.acw + + +class `acw쿼드트리` { + lateinit var video: Array> + var answer = "" + + fun divide(y: Int, x: Int, n: Int) { + var firstNum = video[y][x] + + for (i in y until y + n) { + for (j in x until x + n) { + if (video[i][j] != firstNum) { + val nextSize = n / 2 + answer += "(" + divide(y, x, nextSize) + divide(y, x + nextSize, nextSize) + divide(y + nextSize, x, nextSize) + divide(y + nextSize, x + nextSize, nextSize) + answer += ")" + return + } + } + } + answer += firstNum + + } + + + fun solution() { + var N = readln().toInt() + video = Array(N) { Array(N) { 0 } } + + repeat(N) { n -> + video[n] = readln().chunked(1).map { it.toInt() }.toTypedArray() + } + + divide(0, 0, N) + + println(answer) + + } +} + +fun main() { + val sol = `acw쿼드트리`() + sol.solution() +} \ No newline at end of file diff --git "a/src/5week/acw/\355\235\231\352\270\270\353\263\264\354\210\230\355\225\230\352\270\260.kt" "b/src/5week/acw/\355\235\231\352\270\270\353\263\264\354\210\230\355\225\230\352\270\260.kt" new file mode 100644 index 0000000..b904476 --- /dev/null +++ "b/src/5week/acw/\355\235\231\352\270\270\353\263\264\354\210\230\355\225\230\352\270\260.kt" @@ -0,0 +1,44 @@ +package `5week`.acw + + +import java.util.PriorityQueue + + +class `acw흙길보수하기` { + fun solution() { + val (N, L) = readln().split(" ").map { it.toInt() } + var bridgeEnd = 1 + var answer = 0 + val puddle = PriorityQueue> { a, b -> + a.first - b.first + } + + repeat(N) { + val (s, e) = readln().split(" ").map { it.toInt() } + puddle.add(Pair(s, e)) + } + + while (puddle.isNotEmpty()) { + var (s, e) = puddle.poll() + if (bridgeEnd > s) { + s = bridgeEnd + }//현재 판자보다 웅덩이의 시작점이 빠를경우 시작점을 판자끝부분으로 바꿔주기 + while (s < e) { + //시작점부터 end까지 cover할 수 있도록 판자사용하기 + s += L + answer++ + } + bridgeEnd = s + //판자 end부분 update + } + + println(answer) + + + } +} + +fun main() { + val sol = `acw흙길보수하기`() + sol.solution() +}