private boolean[][] pillar;
private boolean[][] bar;
public int[][] solution(int n, int[][] buildFrame) {
pillar = new boolean[n + 1][n + 1];
bar = new boolean[n + 1][n + 1];
int count = 0;
for (int[] build : buildFrame) {
int x = build[0];
int y = build[1];
int type = build[2];
int action = build[3];
switch (type) {
case 0:
count = processPillar(n, count, x, y, action);
break;
case 1:
count = processBar(n, count, x, y, action);
break;
default:
break;
}
}
return getResultArray(count);
}
private int processBar(int n, int count, int x, int y, int action) {
if (action == 1) {
if (checkBar(x, y)) {
bar[x][y] = true;
count++;
}
} else {
bar[x][y] = false;
if (!canDelete(n))
bar[x][y] = true;
else
count--;
}
return count;
}
private int processPillar(int n, int count, int x, int y, int action) {
if (action == 1) {
if (checkPillar(x, y)) {
pillar[x][y] = true;
count++;
}
} else {
pillar[x][y] = false;
if (!canDelete(n))
pillar[x][y] = true;
else
count--;
}
return count;
}
private int[][] getResultArray(int count) {
int[][] result = new int[count][3];
int idx = 0;
for (int i = 0; i < pillar.length; i++) {
for (int j = 0; j < pillar[i].length; j++) {
if (pillar[i][j]) {
result[idx][0] = i;
result[idx][1] = j;
result[idx++][2] = 0;
}
if (bar[i][j]) {
result[idx][0] = i;
result[idx][1] = j;
result[idx++][2] = 1;
}
}
}
return result;
}
private boolean canDelete(int n) {
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (pillar[i][j] && !checkPillar(i, j))
return false;
if (bar[i][j] && !checkBar(i, j))
return false;
}
}
return true;
}
private boolean checkPillar(int x, int y) {
return (y == 0) || (y > 0 && pillar[x][y - 1]) || (x > 0 && bar[x - 1][y] || bar[x][y]);
}
private boolean checkBar(int x, int y) {
return (y > 0 && pillar[x][y - 1] || pillar[x + 1][y - 1]) || (x > 0 && bar[x - 1][y] && bar[x + 1][y]);
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/60061