#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
bool possible(vector<vector<int>> answer) {
for (int i = 0; i < answer.size(); i++) {
int x = answer[i][0];
int y = answer[i][1];
int stuff = answer[i][2];
if (stuff == 0) { //기둥 설치시
bool check = false;
if (y == 0) check = true;
for (int j = 0; j < answer.size(); j++) {
if (x - 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2])
check = true;
if (x == answer[j][0] && y == answer[j][1] && 1 == answer[j][2])
check = true;
if (x == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2])
check = true;
}
if (!check) return false;
}
else if (stuff == 1) { //보 설치시
bool check = false;
bool left = false;
bool right = false;
for (int j = 0; j < answer.size(); j++) {
if (x == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2])
check = true;
if (x + 1 == answer[j][0] && y - 1 == answer[j][1] && 0 == answer[j][2])
check = true;
if (x - 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2])
left = true;
if (x + 1 == answer[j][0] && y == answer[j][1] && 1 == answer[j][2])
right = true;
}
if (left && right) check = true;
if (!check) return false;
}
}
return true;
}
vector<vector<int>> solution(int n, vector<vector<int>> build_frame) {
vector<vector<int>> answer(1000, vector<int>(2));
for (int i = 0; i < build_frame.size(); i++) {
int x = build_frame[i][0];
int y = build_frame[i][1];
int stuff = build_frame[i][2];
int operate = build_frame[i][3];
if (operate == 0) {
int index = 0;
for (int j = 0; j < answer.size(); j++)
if (x == answer[j][0] && y == answer[j][i] && stuff == answer[j][2])
index = j;
vector<int> erased = answer[index];
answer.erase(answer.begin() + index);
if (!possible(answer))
answer.push_back(erased);
}
if (operate == 1) {
vector<int> inserted;
inserted.push_back(x);
inserted.push_back(y);
inserted.push_back(stuff);
answer.push_back(inserted);
if (!possible(answer))
answer.pop_back();
}
}
sort(answer.begin(), answer.end());
return answer;
}
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
void check(int answer[100][100], int n, int row, int col, int a, int b) {
if (a == 2 && b == 1) {
if (row == 0)//바닥이면 설치 가능
answer[row][col] = 2;
else if (col > 0 && answer[row][col-1] == 1)//왼쪽이 보면 설치 가능
answer[row][col] = 2;
else if (row > 0 && answer[row - 1][col] == 2)//아래가 기둥이면 설치 가능
answer[row][col] = 2;
else
answer[row][col] = 0;
}
else if (a == 1 && b == 1) {
if (row > 0 && answer[row - 1][col] ==2){//아래가 기둥이고
if (col<n&&answer[row-1][col+1]==2)//오른쪽 아래가 기둥일 때
answer[row][col] = 1;
else if (col<n&&answer[row][col+1]==1)//오른쪽이 기둥일 때
answer[row][col]=1;
}
else if (col > 0 && answer[row][col - 1] == 1) {//왼쪽이 보고
if (row < n && answer[row][col] == 2)//오른쪽 아래가 기둥
answer[row][col] = 1;
}
else
answer[row][col] = 0;
}
else if (a == 2 && b == 0) { //기둥 삭제
if (row < n && answer[row + 1][col] == 2)//위에 기둥이 있다면 삭제 불가
answer[row][col] = 2;
else if (row < n && col>0 && answer[row + 1][col - 1] == 1) {//왼쪽 위에 보가 있고
if (answer[row + 1][col] == 1)//위에 연결된 보가 있다면
answer[row][col] = 0;//삭제 가능
else
answer[row][col] = 2;
}
else if (row<n&&answer[row+1][col]==1){//위에 보가 있고
if(col>0&&answer[row+1][col-1]==1)//왼쪽 위에 보가 있다면 삭제 가능
answer[row][col]=0;
}
else
answer[row][col] = 0;
}
else { //보 삭제
if (row < n && answer[row + 1][col] == 2)//위에 기둥이 있다면 삭제 불가
answer[row][col] = 1;
else if (row > 0 && col > 0 && answer[row][col - 1] == 1 && answer[row - 1][col] == 2)
answer[row][col] = 0;
else if (row > 0 && col < n && answer[row - 1][col + 1] == 2 && answer[row][col + 1] == 1)
answer[row][col] = 0;
else
answer[row][col] = 0;
}
}
void make_map(int n, int answer[100][100], vector<vector<int>> build_frame) {
for (int i = 0; i < build_frame.size(); i++) {
int row = build_frame[i][1], col = build_frame[i][0];
if (build_frame[i][3] == 1) { //생성
if (build_frame[i][2] == 0) { //기둥 생성
check(answer, n, row, col, 2, 1);
}
else if (build_frame[i][2] == 1) { //보 생성
check(answer, n, row, col, 1, 1);
}
}
if (build_frame[i][3] == 0) { //삭제
if (build_frame[i][2] == 0) { //기둥 삭제
if (answer[build_frame[i][1]][build_frame[i][0]] == 2) {
check(answer, n, row, col, 2, 0);
}
}
else if (build_frame[i][2] == 1) { //보 삭제
if (answer[build_frame[i][1]][build_frame[i][0]] == 1) {
check(answer, n, row, col, 1, 0);
}
}
}
}
}
vector<vector<int>> solution(int n, vector<vector<int>> build_frame) {
int answer[100][100] = { 0, };
make_map(n, answer, build_frame);
vector<vector<int>> result;
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
if (answer[i][j] == 2) {
vector<int> temp;
temp.push_back(j);
temp.push_back(i);
temp.push_back(answer[i][j] - 2);
result.push_back(temp);
}
else if (answer[i][j] == 1) {
vector<int> temp;
temp.push_back(j);
temp.push_back(i);
temp.push_back(answer[i][j]);
result.push_back(temp);
}
}
}
sort(result.begin(), result.end());
return result;
}
헷갈리니까 보면서 하자!!!
'코테 > 구현' 카테고리의 다른 글
[C/C++] 프로그래머스스쿨 외벽 점검, 구현 (0) | 2023.02.26 |
---|---|
[C/C++] 백준 치킨 배달, 구현 (0) | 2023.02.26 |
[C/C++] 프로그래머스스쿨 자물쇠와 열쇠, 구현 (0) | 2023.02.26 |
[C/C++] 백준 3190, 구현 (0) | 2023.02.18 |
[C/C++] 프로그래머스스쿨 문자열압축, 구현 (0) | 2023.02.16 |