시뮬레이션 문제이다.
주어진 조건이 있어서 해당 조건에 적합하지 않은 연산은 무시하고 지나가야한다.
따라서 일단 설치하고, 만약 설치된 구조물이 적합하지 않으면 해당 연산은 무시하는 코드만 짜주면 된다.
i, j에 설치했으면 그 주위만 확인해주는 것이 효율적이긴한데, 코너케이스에 걸릴까봐 그리고 빠르게 코드를 짜고싶어서 [0~n][0~n]까지 다 검사했다.
코드는 다음과 같다.
#include <string>
#include <vector>
using namespace std;
#define REMOVE 0
#define INSTALL 1
#define PILLAR 0
#define BEAM 1
vector<vector<int>> beam;
vector<vector<int>> pillar;
bool is_fine_operation(int n)
{
for(int i=0;i<n;i++) {
for(int j=0;j<=n;j++) {
if(pillar[i][j] == 0)
continue;
// 기둥이 바닥 바로 위에 있음.
if(i == 0)
continue;
// 기둥 밑에 기둥이 있음
if(pillar[i-1][j] == 1)
continue;
// 기둥이 다른 보 위에 있음
if(j == 0) {
if(beam[i][j] == 1)
continue;
} else if(j == n) {
if(beam[i][j-1] == 1)
continue;
} else {
if(beam[i][j] == 1 || beam[i][j-1] == 1)
continue;
}
return false;
}
}
for(int i=0;i<=n;i++) {
for(int j=0;j<n;j++) {
if(beam[i][j] == 0)
continue;
if(i == 0 || j == n)
continue;
// 보의 한쪽 끝 부분이 기둥 위에 있음
if(pillar[i-1][j] == 1 || pillar[i-1][j+1] == 1)
continue;
if(j == 0 || j == n - 1)
return false;
// 보의 양쪽 끝 부분이 다른 보와 동시에 연결되어 있음
if(beam[i][j-1] == 1 && beam[i][j+1] == 1)
continue;
//printf("Beam [%d][%d]\n", i, j);
return false;
}
}
return true;
}
void simulate(int x, int y, int arch_type, int op_type)
{
if(op_type == INSTALL) {
if(arch_type == PILLAR)
pillar[y][x] = 1;
else
beam[y][x] = 1;
} else {
if(arch_type == PILLAR)
pillar[y][x] = 0;
else
beam[y][x] = 0;
}
}
void restore(int x, int y, int arch_type, int op_type)
{
if(op_type == INSTALL) {
if(arch_type == PILLAR)
pillar[y][x] = 0;
else
beam[y][x] = 0;
} else {
if(arch_type == PILLAR)
pillar[y][x] = 1;
else
beam[y][x] = 1;
}
}
vector<vector<int>> solution(int n, vector<vector<int>> build_frame) {
vector<vector<int>> answer;
beam.resize(n + 1, vector<int>(n + 1, 0));
pillar.resize(n + 1, vector<int>(n + 1, 0));
int x, y, arch_type, op_type;
for(auto it : build_frame) {
x = it[0]; y = it[1]; arch_type = it[2]; op_type = it[3];
simulate(x, y, arch_type, op_type);
if(!is_fine_operation(n))
restore(x, y, arch_type, op_type);
}
for(int j=0;j<=n;j++) {
for(int i=0;i<=n;i++) {
if(pillar[i][j] == 1)
answer.push_back( {j, i, 0} );
if(beam[i][j] == 1)
answer.push_back( {j, i, 1} );
}
}
return answer;
}