From f91750d7232b53300db1eb82306150ebedfdff3c Mon Sep 17 00:00:00 2001 From: acw Date: Mon, 17 Oct 2022 20:51:44 +0900 Subject: [PATCH 1/2] 3week acw --- ...16\354\235\264\354\225\274\352\270\260.kt" | 57 +++++++++++ ...40\354\236\220\353\245\264\352\270\260.kt" | 55 +++++++++++ ...34\354\212\244\354\233\214\353\223\234.kt" | 79 +++++++++++++++ "src/3week/acw/\355\202\271.kt" | 97 +++++++++++++++++++ 4 files changed, 288 insertions(+) create mode 100644 "src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" create mode 100644 "src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" create mode 100644 "src/3week/acw/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" create mode 100644 "src/3week/acw/\355\202\271.kt" diff --git "a/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" "b/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" new file mode 100644 index 0000000..bc5b4b2 --- /dev/null +++ "b/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" @@ -0,0 +1,57 @@ +package `3week`.acw + +class 단풍잎이야기{ + lateinit var quests:Array> + lateinit var comb:Array + var answer=0 + fun makeComb(arr:MutableList,targetNum:Int,count:Int,start:Int){ + if(count==targetNum){ + + for(i in 1 until 2*targetNum+1){ + if(comb[i]){ + arr.add(i) + } + } + answer=answer.coerceAtLeast(countQuests(arr)) + + arr.clear() + + return + } + + for(i in start .. 2*targetNum){ + comb[i]=true + makeComb(arr,targetNum,count+1,i+1) + comb[i]=false + } + } + fun countQuests(arr:List):Int{ + var count=0 + loop1@for(i in quests){ + for(j in i){ + if(!arr.contains(j)) { + continue@loop1 + } + } + + count++ + } + return count + } + fun solution(){ + val (n,m,k)=readln().split(" ").map{it.toInt()} + quests=Array(m){readln().split(" ").map{it.toInt()}.toTypedArray()} + comb=Array(2*n+1){false } + + makeComb(mutableListOf(),n,0,1) + + println(answer) + + + } +} +fun main() { + val sol = 단풍잎이야기() + sol.solution() +} + diff --git "a/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" "b/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" new file mode 100644 index 0000000..c179350 --- /dev/null +++ "b/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" @@ -0,0 +1,55 @@ +package `3week`.acw + +import java.util.PriorityQueue +class 랜선자르기{ + var answer=0L + var test=PriorityQueue() + fun binarySearch(start:Long,end:Long,targetNum:Int){ + + if(start>end){ + return + } + + var s:Long=start.toLong() + var e:Long=end.toLong() + var count=0L + val now=((s+e)/2) + for(i in test){ + count+=(i/now) + + } + + if(count + + lateinit var size: List + + val direction = listOf>( + Pair(0, -1), + Pair(0, 1), + Pair(1, 0), + Pair(-1, 0) + ) + + var words = PriorityQueue() + + fun isIsolate(y:Int,x:Int):Boolean{ + for(i in 0 until 4){ + val ny=y+direction[i].first + val nx=x+direction[i].second + + if(ny<0 || nx<0 || ny>=size[0] ||nx>=size[1]){ + continue + } + + if(puzzle[ny][nx] !in 'a'..'z'){ + return false + } + } + + return true + } + + fun solution() { + + size = readln().split(" ").map { it.toInt() } + + puzzle = Array(size[0]){ readln() } + + //visit = Array(size[0]) { Array(size[1]) { false } } + + repeat(size[0]){ + puzzle[it].split("#").map { + if(it.length>1){ + words.add(it) + } + } + + }//가로 + + repeat(size[1]){ + var word="" + for(i in 0 until size[0]){ + word+=puzzle[i][it] + } + word.split("#").map { + if(it.length>1){ + words.add(it) + } + } + }//세로 + + for(i in 0 until size[0]){ + for(j in 0 until size[1]){ + isIsolate(i,j) + } + } + + + println(words.first()) + + } +} +fun main(){ + val sol = 크로스워드() + sol.solution() +} diff --git "a/src/3week/acw/\355\202\271.kt" "b/src/3week/acw/\355\202\271.kt" new file mode 100644 index 0000000..f717f82 --- /dev/null +++ "b/src/3week/acw/\355\202\271.kt" @@ -0,0 +1,97 @@ +package `3week`.acw + +const val king=3 +const val stone=2 +class 킹{ + lateinit var input:List + var chessMap=Array(8){Array(8){0} } + + val direction= hashMapOf>( + Pair("R",Pair(0,1)), + Pair("L",Pair(0,-1)), + Pair("B",Pair(1,0)), + Pair("T",Pair(-1,0)), + Pair("RT",Pair(-1,1)), + Pair("LT",Pair(-1,-1)), + Pair("RB",Pair(1,1)), + Pair("LB",Pair(1,-1)) + ) + + + fun validate(y:Int,x:Int):Boolean{ + return !(y<0 || x<0 || y>7 || x>7) + } + + fun solution(){ + input=readln().split(" ") + + var kingX=input[0][0]-'A' + var kingY=8-input[0][1].digitToInt() + + var stoneX=input[1][0]-'A' + var stoneY=8-input[1][1].digitToInt() + + val N=input[2].toInt() + + chessMap[kingY][kingX]=king + //king + + chessMap[stoneY][stoneX]=stone + //stone + + for( i in 0 until N){ + val mv=readln() + + val ny=kingY+direction[mv]!!.first + val nx=kingX+direction[mv]!!.second + if(!validate(ny,nx)){ + continue + }//유효성 check + + if(chessMap[ny][nx]==stone){ + val nsy=stoneY+direction[mv]!!.first + val nsx=stoneX+direction[mv]!!.second + + if(!validate(nsy,nsx)){ + continue + }//돌이 자리를 비켜주지 못할 때 + else{ + chessMap[stoneY][stoneX]=0 + chessMap[kingY][kingX]=0 + + chessMap[nsy][nsx]=stone + chessMap[ny][nx]=king + + kingY=ny + kingX=nx + + stoneY=nsy + stoneX=nsx + //이동시켜주기 + + continue + } + + }//가는 자리에 돌이 있을 때 + + + chessMap[kingY][kingX]=0 + chessMap[ny][nx]=king + kingY=ny + kingX=nx + //아무문제 없으면 king만 이동 + + } + + + + println("${(kingX+'A'.toInt()).toChar()}${8-kingY}\n${(stoneX+'A'.toInt()).toChar()}${8-stoneY}") + + } +} + + +fun main(){ + val sol = 킹() + sol.solution() +} \ No newline at end of file From 3dfea46be00f9941e26632fb6f2819785589dd6a Mon Sep 17 00:00:00 2001 From: ChanwooAn <55792553+ChanwooAn@users.noreply.github.com> Date: Tue, 8 Nov 2022 17:00:33 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor=20:=20dongho=EB=8B=98=20code=20rev?= =?UTF-8?q?iew=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...16\354\235\264\354\225\274\352\270\260.kt" | 23 ++------- ...40\354\236\220\353\245\264\352\270\260.kt" | 48 ++++++++----------- ...34\354\212\244\354\233\214\353\223\234.kt" | 37 +++++++------- 3 files changed, 44 insertions(+), 64 deletions(-) diff --git "a/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" "b/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" index bc5b4b2..bc106d9 100644 --- "a/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" +++ "b/src/3week/acw/\353\213\250\355\222\215\354\236\216\354\235\264\354\225\274\352\270\260.kt" @@ -3,8 +3,9 @@ package `3week`.acw class 단풍잎이야기{ lateinit var quests:Array> lateinit var comb:Array - var answer=0 - fun makeComb(arr:MutableList,targetNum:Int,count:Int,start:Int){ + private var answer=0 + + private fun makeComb(arr:MutableList, targetNum:Int, count:Int, start:Int){ if(count==targetNum){ for(i in 1 until 2*targetNum+1){ @@ -12,10 +13,9 @@ class 단풍잎이야기{ arr.add(i) } } - answer=answer.coerceAtLeast(countQuests(arr)) + answer=answer.coerceAtLeast(quests.count{quest->arr.containsAll(quest.toList())}) arr.clear() - return } @@ -25,21 +25,9 @@ class 단풍잎이야기{ comb[i]=false } } - fun countQuests(arr:List):Int{ - var count=0 - loop1@for(i in quests){ - for(j in i){ - if(!arr.contains(j)) { - continue@loop1 - } - } - count++ - } - return count - } fun solution(){ - val (n,m,k)=readln().split(" ").map{it.toInt()} + val (n,m)=readln().split(" ").map{it.toInt()} quests=Array(m){readln().split(" ").map{it.toInt()}.toTypedArray()} comb=Array(2*n+1){false } @@ -54,4 +42,3 @@ fun main() { val sol = 단풍잎이야기() sol.solution() } - diff --git "a/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" "b/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" index c179350..0e4cfc0 100644 --- "a/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" +++ "b/src/3week/acw/\353\236\234\354\204\240\354\236\220\353\245\264\352\270\260.kt" @@ -1,52 +1,44 @@ package `3week`.acw - import java.util.PriorityQueue -class 랜선자르기{ - var answer=0L - var test=PriorityQueue() - fun binarySearch(start:Long,end:Long,targetNum:Int){ - if(start>end){ - return - } +class 랜선자르기 { + var answer = 0L + lateinit var test : PriorityQueue - var s:Long=start.toLong() - var e:Long=end.toLong() - var count=0L - val now=((s+e)/2) - for(i in test){ - count+=(i/now) + private fun binarySearch(start: Long, end: Long, targetNum: Int) { + if (start > end) { + return } + val now = ((start + end) / 2) + val count = test.sumOf { it / now } - if(count().apply{ + addAll(Array(K){ readln().toLong() }) } - binarySearch(1,test.last(),N) - - println(answer) - + binarySearch(1, test.last(), N) + println(answer) } } + fun main() { val sol = 랜선자르기() sol.solution() diff --git "a/src/3week/acw/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" "b/src/3week/acw/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" index 1bf2378..27b5041 100644 --- "a/src/3week/acw/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" +++ "b/src/3week/acw/\355\201\254\353\241\234\354\212\244\354\233\214\353\223\234.kt" @@ -1,31 +1,31 @@ package `3week`.acw - import java.util.PriorityQueue + class 크로스워드{ lateinit var puzzle: Array + var row=0 + var column=0 - lateinit var size: List - - val direction = listOf>( + private val direction = listOf( Pair(0, -1), Pair(0, 1), Pair(1, 0), Pair(-1, 0) ) + data class Pos(val y: Int,val x: Int) var words = PriorityQueue() fun isIsolate(y:Int,x:Int):Boolean{ for(i in 0 until 4){ - val ny=y+direction[i].first - val nx=x+direction[i].second + val nPos= Pos(y+direction[i].first,x+direction[i].second) - if(ny<0 || nx<0 || ny>=size[0] ||nx>=size[1]){ + if(nPos.y<0 || nPos.x<0 || nPos.y>=column ||nPos.x>=row){ continue } - if(puzzle[ny][nx] !in 'a'..'z'){ + if(puzzle[nPos.y][nPos.x] !in 'a'..'z'){ return false } } @@ -34,14 +34,14 @@ class 크로스워드{ } fun solution() { + readln().split(" ").let{ (c,r) -> + row=r.toInt() + column=c.toInt() + } + puzzle = Array(column){ readln() } - size = readln().split(" ").map { it.toInt() } - - puzzle = Array(size[0]){ readln() } - - //visit = Array(size[0]) { Array(size[1]) { false } } - repeat(size[0]){ + repeat(column){ puzzle[it].split("#").map { if(it.length>1){ words.add(it) @@ -50,9 +50,9 @@ class 크로스워드{ }//가로 - repeat(size[1]){ + repeat(row){ var word="" - for(i in 0 until size[0]){ + for(i in 0 until column){ word+=puzzle[i][it] } word.split("#").map { @@ -62,8 +62,8 @@ class 크로스워드{ } }//세로 - for(i in 0 until size[0]){ - for(j in 0 until size[1]){ + for(i in 0 until column){ + for(j in 0 until row){ isIsolate(i,j) } } @@ -77,3 +77,4 @@ fun main(){ val sol = 크로스워드() sol.solution() } +