와이유스토리

[시뮬레이션] 프로그래머스 [1차] 프렌즈4블록 C++ 본문

코딩테스트/시뮬레이션

[시뮬레이션] 프로그래머스 [1차] 프렌즈4블록 C++

유(YOO) 2022. 3. 3. 22:27

 

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

#include <string>
#include <vector>
#include <string.h>

using namespace std;

bool change[30][30];

int make(int i, int j) {
    if (!change[i][j]) {
        change[i][j] = true;
        return 1;
    }
    return 0;
}

int check(int m, int n, vector<string> board) {
    int cnt = 0;
    memset(change, false, sizeof(change));
    for(int i=m-1; i>0; i--) {
        for(int j=0; j<n-1; j++) {
            if (board[i][j] == ' ') continue;
            if ((board[i][j] == board[i][j+1]) && (board[i-1][j] == board[i-1][j+1]) && (board[i][j] == board[i-1][j])) {
                cnt += make(i, j);
                cnt += make(i-1, j);
                cnt += make(i, j+1);
                cnt += make(i-1, j+1);
            }
        }
    }
    return cnt;
}

int solution(int m, int n, vector<string> board) {
    int answer = 0;
    while(true) {
        int cnt = check(m, n, board);
        if (!cnt) break;
        answer += cnt;
        for(int i=0; i<m; i++) {
            for(int j=0; j<n; j++) {
                if (change[i][j]) {
                    if (i==0) board[i][j] = ' ';
                    for(int k=i; k>0; k--) {
                        board[k][j] = board[k-1][j];
                        board[k-1][j] = ' ';
                    }
                }
            }
        }
    }
    
    return answer;
}
Comments