오늘 풀어본 문제는 ⭐색종이 붙이기 라는 문제이다.
불가능한 경우 -1 출력
public class Main {
static int[][] PAPER;
static int[] TYPE_CNT = {0, 5, 5, 5, 5, 5};
static int ANSWER;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
PAPER = new int[10][10];
ANSWER = Integer.MAX_VALUE;
for (int i = 0; i < 10; i++) {
st = new StringTokenizer(br.readLine());
for (int j = 0; j < 10; j++) {
PAPER[i][j] = Integer.parseInt(st.nextToken());
}
}
dfs(0);
System.out.println(ANSWER == Integer.MAX_VALUE ? -1 : ANSWER);
}
static void dfs(int used) {
if (used >= ANSWER) {
return;
}
boolean found = false;
int r = 0;
int c = 0;
for (r = 0; r < 10; r++) {
for (c = 0; c < 10; c++) {
if (PAPER[r][c] == 1) {
found = true;
break;
}
}
if (found) {
break;
}
}
if (!found) {
ANSWER = Math.min(ANSWER, used);
return;
}
for (int size = 1; size <= 5; size++) {
if (!canGo(r,c,size))
continue;
attach(r, c, size, 0);
TYPE_CNT[size]--;
dfs(used + 1);
attach(r, c, size, 1);
TYPE_CNT[size]++;
}
}
static boolean canGo(int r, int c, int size) {
if (r + size > 10 || c + size > 10 || TYPE_CNT[size] == 0) {
return false;
}
for (int nr = r; nr < r + size; nr++) {
for (int nc = c; nc < c + size; nc++) {
if (PAPER[nr][nc] == 0) {
return false;
}
}
}
return true;
}
static void attach(int r, int c, int size, int value) {
for (int nr = r; nr < r + size; nr++) {
for (int nc = c; nc < c + size; nc++) {
PAPER[nr][nc] = value;
}
}
}
}