728x90
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
첨에 문제를 제대로 안 읽고 풀다가 끙끙거렸는데 다시 제대로 읽어보니 분할 정복 문제였다. 시키는 대로 재귀호출로 종이를 4조각씩 잘라나가서 최종적으로 잘려지는 종이들의 수를 세면 된다.
Solution
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.util.*
var br = BufferedReader(InputStreamReader(System.`in`))
var bw = BufferedWriter(OutputStreamWriter(System.out))
var N = 0
var color_count = IntArray(2)
val BLUE = 1
val WHITE = 0
fun solution() {
N = br.readLine().toInt()
var map = Array(N, {Array(N, {0})})
map.forEach {
var strtok = StringTokenizer(br.readLine())
it.forEachIndexed { index, i -> it[index] = strtok.nextToken().toInt() }
}
countSquares(0, 0, N, map)
bw.write("${color_count[WHITE]}\n")
bw.write("${color_count[BLUE]}\n")
}
fun countSquares(x : Int, y : Int, length : Int, map: Array<Array<Int>>)
{
var color = map[x][y]
var isAllSame = true
for(i in x until x + length)
{
for(j in y until y + length)
{
if(color != map[i][j])
{
isAllSame = false
break
}
}
}
if(isAllSame)
color_count[color]++
else
{
countSquares(x, y, length / 2, map)
countSquares(x + length / 2, y, length / 2, map)
countSquares(x, y + length / 2, length / 2, map)
countSquares(x + length / 2, y + length / 2, length / 2, map)
}
}
fun main() {
solution()
br.close()
bw.close()
}
728x90
'Algorithm > 문제 풀이' 카테고리의 다른 글
[알고리즘] 합분해 (백준 2225번) (0) | 2020.10.20 |
---|---|
[알고리즘] 풍선 터트리기 (월간 코드 챌린지 시즌1 - 9월) (0) | 2020.10.09 |
[알고리즘] 파티 (백준 1238번) (0) | 2020.10.07 |
[알고리즘] 숨바꼭질 3 (백준 13549번) (0) | 2020.10.07 |
[알고리즘] ABCDE (백준 13023번) (0) | 2020.10.06 |
댓글