Skip to content

Commit 067b546

Browse files
committed
solve(programmers): LV3_퍼즐조각 채우기_kt
# id: 문제 id를 숫자로 작성 # categories : 해당 문제의 유형을 ,로 구분하여 작성 # tags : 해당 문제의 태그를 ,로 구분하여 작성 # time : 해당 문제 풀이에 걸린 시간을 분단위 숫자로 작성 # try : 해당 문제에 몇번의 시도를 했는지 숫자로 작성 # help: 해당 문제에 외부의 도움을 받았는지 true/false로 작성 # url : 해당 문제의 url을 작성 id: 84021 categories: [DFS] tags: [] time: 70 try: 1 help: false url: https://school.programmers.co.kr/learn/courses/30/lessons/84021#
1 parent 29634be commit 067b546

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import kotlin.collections.ArrayDeque
2+
class Solution {
3+
fun solution(game_board: Array<IntArray>, table: Array<IntArray>): Int {
4+
var answer: Int = -1
5+
6+
val tableP=extractPeices(table,0)
7+
val boardP=extractPeices(game_board,1)
8+
val filled=BooleanArray(boardP.size)
9+
val used=BooleanArray(tableP.size)
10+
var count=0
11+
12+
for(i in boardP.indices){
13+
if(filled[i]) continue
14+
val bp=boardP[i]
15+
for(j in tableP.indices){
16+
if(used[j]) continue
17+
val tp=tableP[j]
18+
val res=checkMatch(bp,tp)
19+
if(res){
20+
filled[i]=true
21+
used[j]=true
22+
count+=tp.size
23+
break
24+
}
25+
}
26+
}
27+
28+
return count
29+
}
30+
}
31+
32+
fun checkMatch(a:MutableList<Pair<Int,Int>>,b:MutableList<Pair<Int,Int>>):Boolean{
33+
34+
if (a.size!=b.size) return false
35+
val c=Comparator{o1:Pair<Int,Int>,o2:Pair<Int,Int>->
36+
if(o1.first==o2.first) o1.second-o2.second else o1.first-o2.first
37+
}
38+
val temp=mutableListOf<Pair<Int,Int>>()
39+
a.sortWith(c)
40+
b.sortWith(c)
41+
for((x,y) in a){
42+
temp.add(x to y)
43+
}
44+
loop@ for(r in 0 until 4){
45+
val gx=temp[0].first-b[0].first
46+
val gy=temp[0].second-b[0].second
47+
for(i in temp.indices){
48+
val (ax,ay)=temp[i]
49+
val (bx,byy)=b[i]
50+
val cgx=ax-bx
51+
val cgy=ay-byy
52+
if(gx!=cgx || gy!=cgy){
53+
rotate90(temp)
54+
temp.sortWith(c)
55+
continue@loop
56+
}
57+
}
58+
return true
59+
}
60+
return false
61+
}
62+
63+
fun rotate90(arr:MutableList<Pair<Int,Int>>){
64+
for(i in arr.indices){
65+
val (x,y)=arr[i]
66+
arr[i]=y to -x
67+
}
68+
}
69+
70+
fun extractPeices(table:Array<IntArray>,blank:Int):MutableList<MutableList<Pair<Int,Int>>>{
71+
val pieces=mutableListOf<MutableList<Pair<Int,Int>>>()
72+
73+
val visited=Array(table.size){BooleanArray(table[0].size)}
74+
75+
val ox=listOf(1,-1,0,0)
76+
val oy=listOf(0,0,1,-1)
77+
78+
79+
//조각분리
80+
for(i in table.indices){
81+
for(j in table[0].indices){
82+
if(visited[i][j] || table[i][j]==blank) continue
83+
visited[i][j]=true
84+
pieces.add(mutableListOf(i to j))
85+
val s=ArrayDeque<Pair<Int,Int>>()
86+
s.add(i to j)
87+
while(s.isNotEmpty()){
88+
val (cx,cy)=s.removeLast()
89+
for(k in 0 until 4){
90+
val nx=cx+ox[k]
91+
val ny=cy+oy[k]
92+
if(nx in 0 until table.size && ny in 0 until table[0].size&& !visited[nx][ny] &&table[nx][ny]==table[cx][cy]){
93+
visited[nx][ny]=true
94+
pieces.last().add(nx to ny)
95+
s.add(nx to ny)
96+
}
97+
}
98+
}
99+
}
100+
}
101+
102+
return pieces
103+
}

0 commit comments

Comments
 (0)