[프로그래머스] 프렌즈4블록
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int M, N;
vector<vector<int>> intBoard;
vector<vector<int>> eraseBoard;
bool inRange(int r, int w) {
if (r < 0 || r >= M) return false;
if (w < 0 || w >= N) return false;
return true;
}
bool findSquare(int r, int w) {
if (!inRange(r, w + 1) || !inRange(r + 1, w) || !inRange(r + 1, w + 1)) return false;
int cur = intBoard[r][w];
if ((cur == intBoard[r][w + 1]) && (cur == intBoard[r + 1][w]) && (cur == intBoard[r + 1][w + 1])) return true;
else return false;
}
int eraseSquare(int r, int w) {
int cnt = 0;
if (eraseBoard[r][w] == 0) cnt++;
if (eraseBoard[r][w+1] == 0) cnt++;
if (eraseBoard[r+1][w] == 0) cnt++;
if (eraseBoard[r+1][w+1] == 0) cnt++;
eraseBoard[r][w] = eraseBoard[r][w+1] = eraseBoard[r+1][w] = eraseBoard[r+1][w+1] = 1;
return cnt;
}
void moveBoardDown(int r, int w) {
for (int i = r; i > 0 ; --i) {
intBoard[i][w] = intBoard[i - 1][w];
}
intBoard[0][w] = -1;
return;
}
int solution(int m, int n, vector<string> board) {
M = m;
N = n;
for (int i = 0; i < M; ++i) {
vector<int> boardBuff;
vector<int> eraseBuff;
for (int j = 0; j < N; ++j) {
boardBuff.push_back(board[i][j]);
eraseBuff.push_back(0);
}
intBoard.push_back(boardBuff);
eraseBoard.push_back(eraseBuff);
}
int answer = 0;
while (true) {
int eraseCnt = 0;
for (int r = 0; r < M; ++r) {
for (int w = 0; w < N; ++w) {
if (intBoard[r][w] == -1) continue;
if (findSquare(r, w)) eraseCnt += eraseSquare(r, w);
}
}
if (eraseCnt == 0) break;
else answer += eraseCnt;
for (int w = 0; w < N; ++w) {
for (int r = 0; r < M; ++r) {
if (eraseBoard[r][w] == 1) {
moveBoardDown(r, w);
eraseBoard[r][w] = 0;
}
}
}
}
return answer;
}