본문 바로가기
전공공부/코딩테스트

(c++) 프로그래머스 "[1차] 프렌즈4블록"

by 시아나 2022. 4. 29.

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

 

코딩테스트 연습 - [1차] 프렌즈4블록

프렌즈4블록 블라인드 공채를 통과한 신입 사원 라이언은 신규 게임 개발 업무를 맡게 되었다. 이번에 출시할 게임 제목은 "프렌즈4블록". 같은 모양의 카카오프렌즈 블록이 2×2 형태로 4개가 붙

programmers.co.kr


나의 코드

실패한 코드 

#include <string>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

vector<pair<int, int>>check = { {0,0},{0,1},{1,0},{1,1} };
stack<pair<int, int>> s;

void check4(int m, int n, vector<string> &board) {
    stack<pair<int, int>> tmp;
    for (int i = 0; i < m-1; i++) {
        for (int k = 0; k < n-1; k++) {
            bool tag = true;
            char c = board[i][k];
            if (c != '.') {
                for (int t = 0; t < 4; t++) {
                    if (c != board[i + check[t].first][k + check[t].second]) {
                        tag = false;
                        break;
                    }
                }
                if (tag) {
                    s.push({ i,k });
                    tmp.push({ i,k });
                }
            }
        }
    }
    while (!tmp.empty()) {
        int i = tmp.top().first, k = tmp.top().second;
        tmp.pop();
        for (int t = 0; t < 4; t++) {
            board[i + check[t].first][k + check[t].second] = '.';
        }
    }
}

int solution(int m, int n, vector<string> board) {
    int answer = 0;
    check4(m,n,board);
    while (!s.empty()) {
        while (!s.empty()) {
            int x = s.top().first, y = s.top().second;
            s.pop();
            for (int i = 0; i < 2; i++) {
                int num = 0;
                for (int k = x; k < m && board[k][y + i] == '.'; k++, num++);
                for (int k = x - 1; k >= 0 && board[k][y+i] != '.'; k--) {
                   board[k + num][y + i] = board[k][y + i];
                   board[k][y + i] = '.';
                }
            }
        }
        check4(m, n, board);
    }
    for (int i = 0; i < m; i++) {
        for (int k = 0; k < n; k++) {
            if (board[i][k] == '.') {
                answer++;
            }
        }
    }

    return answer;
}

 

테스트 6번만 돌아가지 않아 애를 먹었는데

테스트 6번에서 내 코드가 빈공간을 내려갈때 빈공간이 아닌 칸을 덮어버리는 문제가 있는것 같아 이렇게 수정했다.

#include <string>
#include <vector>
#include <stack>
#include <algorithm>

using namespace std;

vector<pair<int, int>>check = { {0,0},{0,1},{1,0},{1,1} };
stack<pair<int, int>> s;

void check4(int m, int n, vector<string> &board) {
    stack<pair<int, int>> tmp;
    for (int i = 0; i < m-1; i++) {
        for (int k = 0; k < n-1; k++) {
            bool tag = true;
            char c = board[i][k];
            if (c != '.') {
                for (int t = 0; t < 4; t++) {
                    if (c != board[i + check[t].first][k + check[t].second]) {
                        tag = false;
                        break;
                    }
                }
                if (tag) {
                    s.push({ i,k });
                    tmp.push({ i,k });
                }
            }
        }
    }
    while (!tmp.empty()) {
        int i = tmp.top().first, k = tmp.top().second;
        tmp.pop();
        for (int t = 0; t < 4; t++) {
            board[i + check[t].first][k + check[t].second] = '.';
        }
    }
}

int solution(int m, int n, vector<string> board) {
    int answer = 0;
    check4(m,n,board);
    while (!s.empty()) {
        while (!s.empty()) {
            int x = s.top().first, y = s.top().second;
            s.pop();
            for (int i = 0; i < 2; i++) {
                int num = 0;
                for (int k = x; k < m && board[k][y + i] == '.'; k++, num++);
                for (int k = x - 1; k >= 0 && board[k][y+i] != '.'; k--) {
                    if (board[k + num][y + i] == '.') {
                        board[k + num][y + i] = board[k][y + i];
                        board[k][y + i] = '.';
                    }
                }
            }
        }
        check4(m, n, board);
    }
    for (int i = 0; i < m; i++) {
        for (int k = 0; k < n; k++) {
            if (board[i][k] == '.') {
                answer++;
            }
        }
    }

    return answer;
}