https://www.acmicpc.net/problem/2630
2630번: 색종이 만들기
첫째 줄에는 전체 종이의 한 변의 길이 N이 주어져 있다. N은 2, 4, 8, 16, 32, 64, 128 중 하나이다. 색종이의 각 가로줄의 정사각형칸들의 색이 윗줄부터 차례로 둘째 줄부터 마지막 줄까지 주어진다.
www.acmicpc.net
단순 재귀 문제였다.
문제 풀이
- 입력받은 색종이를 4군데로 분할해서 풀어야 되니, 점점 작아지는 재귀 함수를 만든다.
- 크기가 1보다 작으면 2로 나누지 못하니 종료시키기
- blue와 white 변수를만들어 그 범위를 탐색 했을 때 어느 하나가 True 면 그 값 증가
- 둘다 false면 크기를 2로 나눈 뒤 4군데로 나누어 재귀 실행.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Baekjoon.silver
{
class _2630
{
static int blue = 0;
static int white = 0;
static int[][] paper;
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
paper = new int[n][];
for (int i = 0; i < n; i++)
paper[i] = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
find_color(0, 0, n);
Console.WriteLine(white);
Console.WriteLine(blue);
}
static void find_color(int x, int y, int n) //x 위아래, y 왼오
{
if (n < 1)
return;
bool isblue = true;
bool iswhite = true;
//Console.WriteLine($"x {x}부터 {x+n}, y {y}부터{y+n}까지");
for(int i = x; i< x + n; i++)
{
for(int j = y; j<y + n; j++)
{
if (paper[i][j] == 1)
iswhite = false;
else
isblue = false;
}
}
if (isblue)
{
blue++;
//Console.WriteLine($"x{x}부터 {x+n}, y{y}부터{y+n}까지 blue");
}
else if (iswhite)
{
white++;
//Console.WriteLine($"x{x}부터 {x + n}, y{y}부터{y + n}까지 white");
}
else
{
n /= 2;
find_color(x, y, n); // 좌측 상
find_color(x, y + n, n); // 우측 상
find_color(x + n, y, n); // 좌측 하
find_color(x + n , y + n, n); // 우측 하
}
}
}
}
'문제 풀이 > C#' 카테고리의 다른 글
[11054] - 가장 긴 바이토닉 부분 수열 (0) | 2022.10.29 |
---|---|
[1992] - 쿼드트리 (0) | 2022.10.18 |
[2589] - 보물섬 (0) | 2022.10.12 |
[1707] - 이분 그래프 (0) | 2022.10.11 |
[1806] - 부분합 (0) | 2022.10.10 |