본문 바로가기
카테고리 없음

블록 게임 C++

by paysmile 2022. 3. 21.

https://programmers.co.kr/learn/courses/30/lessons/42894

 

코딩테스트 연습 - 블록 게임

[[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0,0,0],[0,0,0,0,0,0,4,0,0,0],[0,0,0,0,0,4,4,0,0,0],[0,0,0,0,3,0,4,0,0,0],[0,0,0,2,3,0,0,0,5,5],[1,2,2,2,3,3,0,0,0,5],[1,1,1,0,0,0,0,0,0,5]] 2

programmers.co.kr

#include <string>
#include <vector>

using namespace std;
int answer = 0;
int R, C;
vector<vector<int>> b;
bool flag;

bool CanDrop(int ii, int jj) {
	for (int i = 0; i < ii; i++) {
		if (b[i][jj] != 0) return false;
	}

	return true;
}

void FindBlock(int ii, int jj, int r, int c) {
	int count = 0;
	int block = -1;

	for (int i = ii; i < ii + r; i++) {
		for (int j = jj; j < jj + c; j++) {
			if (!(i >= 0 && i < R && j >= 0 && j < C)) return;

			if (b[i][j] == 0) {
				count++;

				if (CanDrop(i, j) == false) return;
			}
			else {
				if (block == -1) block = b[i][j];
				else {
					if (block != b[i][j]) return;
				}
			}

			if (count > 2) return;
		}
	}

	answer++;
	for (int i = ii; i < ii + r; i++) {
		for (int j = jj; j < jj + c; j++) {
			b[i][j] = 0;
		}
	}
	flag = true;
}

int solution(vector<vector<int>> board) {
	R = board.size();
	C = board[0].size();
	b = board;
	flag = true;

	while (flag) {
		flag = false;
		for (int i = 0; i < R; i++) {
			for (int j = 0; j < C; j++) {
				FindBlock(i, j, 2, 3);
				FindBlock(i, j, 3, 2);
			}
		}
	}
	return answer;
}