코딩테스트/코딩테스트 연습

[C++] 프로그래머스. 2020 KAKAO BLIND RECRUITMENT 기둥과 보 설치

Bradbury 2020. 7. 22. 00:17
728x90

다시 풀어보기

#include <vector>

using namespace std;

struct coord {
	int pillar;
	int beam;
};

coord arr[101][101];

//기둥 세울 조건 바닥이거나 x=0, 보의 한쪽 끝부분(양쪽은 안됨), 다른 기둥 위
// 보 한쪽 끝부분이 기둥 위에 있거나, 양쪽 끝 부분이 다른 보와 연결

bool pillar_check(int x, int y, int n) {
	if (y == 0) return true;
	if (arr[x][y - 1].pillar) return true;
	if (x > 0 && arr[x - 1][y].beam) return true;
	if (x < n && arr[x][y].beam) return true;

	return false;
}

bool beam_check(int x, int y, int n) {
	if (arr[x][y - 1].pillar) return true;
	if (x < n && arr[x + 1][y - 1].pillar) return true;
	if (x > 0 && x < n && arr[x + 1][y].beam && arr[x - 1][y].beam) return true;

	return false;
}

vector<vector<int>> solution(int n, vector<vector<int>> build_frame) {
	vector<vector<int>> answer;

	for (int i = 0; i < build_frame.size(); i++) {
		int x = build_frame[i][0];
		int y = build_frame[i][1];
		int a = build_frame[i][2];
		int b = build_frame[i][3];

		if (b == 1 && a == 0) {//기둥 설치
			if (pillar_check(x, y, n)) arr[x][y].pillar = 1;
		}
		else if (b == 1 && a == 1) {// 보 설치
			if (beam_check(x, y, n)) arr[x][y].beam = 1;
		}
		else if (b == 0 && a == 0) {// 기둥 삭제
			bool check = true;
			arr[x][y].pillar = 0;

			if (y < n && arr[x][y + 1].pillar && !pillar_check(x, y + 1, n)) check = false;
			else if (y < n && arr[x][y + 1].beam && !beam_check(x, y + 1, n)) check = false;
			else if (x > 0 && arr[x - 1][y + 1].beam && y < n && !beam_check(x - 1, y + 1, n)) check = false;

			if (!check) arr[x][y].pillar = 1;

		}
		else if (b == 0 && a == 1) {// 보 삭제
			bool check = true;
			arr[x][y].beam = 0;

			if (arr[x][y].pillar && !pillar_check(x, y, n)) check = false;
			else if (x < n && arr[x + 1][y].pillar && !pillar_check(x + 1, y, n)) check = false;
			else if (x > 0 && arr[x - 1][y].beam && !beam_check(x - 1, y, n)) check = false;
			else if (x < n && arr[x + 1][y].beam && !beam_check(x + 1, y, n)) check = false;

			if (!check) arr[x][y].beam = 1;
		}
	}

	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n; j++) {
			if (arr[i][j].pillar) answer.push_back({ i, j, 0 });
			if (arr[i][j].beam) answer.push_back({ i, j, 1 });
		}
	}

	return answer;
}

 

 

728x90