Skip to content

[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

Merged
merged 2 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
57 changes: 57 additions & 0 deletions src/3week/acw/단풍잎이야기.kt
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
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 함수 따로 뺴지 않고 아래와 같이 대체될 수 있을것 같아요

quests.sumOf { quest -> quest.count { q -> arr.contains(q) } }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

훨씬 깔끔하고 좋아보입니다. 그런데 만약에 위와 같이 함수로 빼는게 코드량은 늘지만 가독성면에 있어서는 훨씬 좋다면 어떤것을 선택하는게 좋을까요? (물론 위의 함수는 그리 가독성도 좋아보이지는 않습니다만... 만약을 가정하면요 ㅎㅎ...)
늦게 질문드려서 죄송합니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

가독성 좋은 쪽을 택하는게 맞습니다
사실 애초에 속도 빠른 코드를 짜기위해서 시스템 코딩을 해야하는 경우엔 Go나 Rust로 짜겠지만 저희는 여러 사람이 함께 작업하는 코드를 만들어가는게 중요하니까요 :)

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()
}

55 changes: 55 additions & 0 deletions src/3week/acw/랜선자르기.kt
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>()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에서부터 생성을 꼭 해둘 필요는 없다고 생각하는데 lateinit 해두는건 어떠신가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

answer을 말씀하시는 거라면, Long type이라 lateinit 선언이 안되서 그냥 var로 미리 선언해두었습니다. delegate라도 사용해서 늦은 초기화를 사용하는게 좋을까요?
test를 말씀하신거라면 test가 거의 바로 사용되기 때문에 lateinit할필요없이 바로 생성하는게 좋다고 생각했었습니다.

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<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)
}
binarySearch(1,test.last(),N)

println(answer)





}
}
fun main() {
val sol = 랜선자르기()
sol.solution()
}


79 changes: 79 additions & 0 deletions src/3week/acw/크로스워드.kt
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

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()
}
97 changes: 97 additions & 0 deletions src/3week/acw/킹.kt
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))
)


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}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

출력에 개행 포함해서 out 연산이 한번인건 좋아요!
이부분도 toString 오버라이딩 해서 king과 stone 출력하는 함수 만들면 king따로 출력연산 구성하고 stone따로 연산 구성할 필요없이 "$king\n$stone" 으로 출력이 끝나겠죠?
[참고 - 현수님 리뷰]


}
}


fun main(){
val sol = 킹()
sol.solution()
}