관리 메뉴

Mini

프로그래머스 프렌즈4블록 cpp // 배열빡구현, check에서 삭제처리인지검사해야 본문

Algorithm/programmers

프로그래머스 프렌즈4블록 cpp // 배열빡구현, check에서 삭제처리인지검사해야

Mini_96 2023. 7. 13. 10:34

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

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

* 의사코드

1. 배열돌며 2x2 인지 체크, 삭제예정기록('.')

2.삭제카운팅++, 실제삭제

3.떨어뜨리기

내가블록이있고, 아래가비엇으면 swap

4. 1-2-3반복

종료조건 : 터질게없다

#include <bits/stdc++.h>

using namespace std;

int dy[]={1,0,1};
int dx[]={1,1,0};
int v[34][34];
char a[34][34];
char pop[34][34];
int M,N,y,x,ret;

bool check(int y, int x){
    if(a[y][x]!='.' and a[y][x]==a[y+1][x+1] and 
       a[y][x]==a[y][x+1] and a[y][x]==a[y+1][x])
        return true;
    return false;
}
int solution(int m, int n, vector<string> board) {

    M=m; N=n;
    for(int i=0;i<m;++i){
        for(int j=0;j<n;++j){
            a[i][j]=board[i][j];
        }
    }
    
    // for(int i=0;i<m;++i){
    //     for(int j=0;j<n;++j){
    //         cout<<a[i][j];
    //     }
    //     cout<<endl;
    // }
    
    bool pang=false;
    do{
        //1. 배열돌며 2x2 체크, 삭제예정기록
        //초기화
        fill(&v[0][0],&v[0][0]+34*34,0);
        pang=false;
        for(int i=0;i<m-1;++i){
            for(int j=0;j<n-1;++j){
                if(check(i,j)){
                    //애니팡
                    v[i][j]=1;
                    v[i+1][j+1]=1;
                    v[i][j+1]=1;
                    v[i+1][j]=1;
                    pang=true;
                }
            }
        }
        //if(!pang) break;

        //  for(int i=0;i<m;++i){
        //     for(int j=0;j<n;++j){
        //         cout<<v[i][j];
        //     }
        //      cout<<endl;
        // }

        //2.삭제카운팅 and 실제삭제
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                if(v[i][j]==1) {
                    ret++;
                    a[i][j]='.';
                }
            }
        }

        //3. 떨어뜨리기
        bool mmove=false;
        do{
            mmove=false;
            for(int i=m-1;i>=0;--i){
                for(int j=0;j<n;++j){
                    //문자가있고 아래가 빈칸이면 -> 한칸 아래로 swap
                    if(a[i][j]!='.' and a[i+1][j]=='.') {
                        char temp=a[i][j];
                        a[i][j]='.';
                        a[i+1][j]=temp;
                        mmove=true;
                    }
                }
            }
        }while(mmove==true);
        
        for(int i=0;i<m;++i){
        for(int j=0;j<n;++j){
            cout<<a[i][j];
        }
         cout<<endl;
        }
    }while(pang);
    
    // for(int i=0;i<m;++i){
    //     for(int j=0;j<n;++j){
    //         cout<<a[i][j];
    //     }
    //      cout<<endl;
    // }

    return ret;
}