-
Notifications
You must be signed in to change notification settings - Fork 0
[Week3] 안찬우 : 킹, 랜선 자르기, 크로스워드, 단풍잎이야기 #16
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package `3week`.acw | ||
|
||
class 단풍잎이야기{ | ||
lateinit var quests:Array<Array<Int>> | ||
lateinit var comb:Array<Boolean> | ||
var answer=0 | ||
fun makeComb(arr:MutableList<Int>,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>):Int{ | ||
var count=0 | ||
loop1@for(i in quests){ | ||
for(j in i){ | ||
if(!arr.contains(j)) { | ||
continue@loop1 | ||
} | ||
} | ||
|
||
count++ | ||
} | ||
return count | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 함수 따로 뺴지 않고 아래와 같이 대체될 수 있을것 같아요 quests.sumOf { quest -> quest.count { q -> arr.contains(q) } } There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 훨씬 깔끔하고 좋아보입니다. 그런데 만약에 위와 같이 함수로 빼는게 코드량은 늘지만 가독성면에 있어서는 훨씬 좋다면 어떤것을 선택하는게 좋을까요? (물론 위의 함수는 그리 가독성도 좋아보이지는 않습니다만... 만약을 가정하면요 ㅎㅎ...) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 가독성 좋은 쪽을 택하는게 맞습니다 |
||
fun solution(){ | ||
val (n,m,k)=readln().split(" ").map{it.toInt()} | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
quests=Array(m){readln().split(" ").map{it.toInt()}.toTypedArray()} | ||
comb=Array(2*n+1){false } | ||
|
||
makeComb(mutableListOf(),n,0,1) | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
println(answer) | ||
|
||
|
||
} | ||
} | ||
fun main() { | ||
val sol = 단풍잎이야기() | ||
sol.solution() | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package `3week`.acw | ||
|
||
import java.util.PriorityQueue | ||
class 랜선자르기{ | ||
var answer=0L | ||
var test=PriorityQueue<Long>() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분에서부터 생성을 꼭 해둘 필요는 없다고 생각하는데 lateinit 해두는건 어떠신가요? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. answer을 말씀하시는 거라면, Long type이라 lateinit 선언이 안되서 그냥 var로 미리 선언해두었습니다. delegate라도 사용해서 늦은 초기화를 사용하는게 좋을까요? |
||
fun binarySearch(start:Long,end:Long,targetNum:Int){ | ||
|
||
if(start>end){ | ||
return | ||
} | ||
|
||
var s:Long=start.toLong() | ||
var e:Long=end.toLong() | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
var count=0L | ||
val now=((s+e)/2) | ||
for(i in test){ | ||
count+=(i/now) | ||
|
||
} | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(count<targetNum){ | ||
binarySearch(s,now-1,targetNum) | ||
//작을 땐 더 작은 수로 계산해보기 | ||
}else{ | ||
answer=answer.coerceAtLeast(now) | ||
binarySearch(now+1,e,targetNum) | ||
//크면 더 큰 수 찾아보기 | ||
} | ||
|
||
} | ||
|
||
fun solution(){ | ||
val (K,N)= readln().split(" ").map{it.toInt()} | ||
|
||
repeat(K){ | ||
val input= readln().toLong() | ||
test.add(input) | ||
} | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
binarySearch(1,test.last(),N) | ||
|
||
println(answer) | ||
|
||
|
||
|
||
|
||
|
||
} | ||
} | ||
fun main() { | ||
val sol = 랜선자르기() | ||
sol.solution() | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package `3week`.acw | ||
|
||
import java.util.PriorityQueue | ||
|
||
class 크로스워드{ | ||
lateinit var puzzle: Array<String> | ||
|
||
lateinit var size: List<Int> | ||
|
||
val direction = listOf<Pair<Int, Int>>( | ||
Pair(0, -1), | ||
Pair(0, 1), | ||
Pair(1, 0), | ||
Pair(-1, 0) | ||
) | ||
|
||
var words = PriorityQueue<String>() | ||
|
||
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 | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if(ny<0 || nx<0 || ny>=size[0] ||nx>=size[1]){ | ||
continue | ||
} | ||
|
||
if(puzzle[ny][nx] !in 'a'..'z'){ | ||
return false | ||
} | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
return true | ||
} | ||
|
||
fun solution() { | ||
|
||
size = readln().split(" ").map { it.toInt() } | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
puzzle = Array(size[0]){ readln() } | ||
|
||
//visit = Array(size[0]) { Array(size[1]) { false } } | ||
|
||
repeat(size[0]){ | ||
puzzle[it].split("#").map { | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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()) | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
} | ||
} | ||
fun main(){ | ||
val sol = 크로스워드() | ||
sol.solution() | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package `3week`.acw | ||
|
||
const val king=3 | ||
const val stone=2 | ||
class 킹{ | ||
lateinit var input:List<String> | ||
var chessMap=Array(8){Array(8){0} } | ||
|
||
val direction= hashMapOf<String,Pair<Int,Int>>( | ||
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)) | ||
) | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
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() | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 | ||
ChanwooAn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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}") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 출력에 개행 포함해서 out 연산이 한번인건 좋아요! |
||
|
||
} | ||
} | ||
|
||
|
||
fun main(){ | ||
val sol = 킹() | ||
sol.solution() | ||
} |
Uh oh!
There was an error while loading. Please reload this page.