BOJ::2638 치즈

시사점

이해(x)

설계, 손 코딩(x)

시간 복잡도

공간 복잡도

손 코딩 후 문제 리뷰(x)

구현(x)

함수 List

업데이트 되는 변수

실제 구현

#include<bits/stdc++.h>
#define pb push_back
#define rep(i, a, b) for(int i=a;i<b;i++)
#define r_rep(i,a,b) for(int i=a;i>b;i--)
using namespace std;
const int MAXNM = 100 + 4;
const int dx[]={-1, 0, 1, 0}, dy[]={0, 1, 0, -1};

int n, m;
int a[MAXNM][MAXNM];
queue<pair<int,int> > q;
int cnt[MAXNM][MAXNM];
bool seen[MAXNM][MAXNM];
void input(){
    cin >> n >> m;
    rep(i, 0, n+2){
        rep(j, 0, m+2){
            if(i == 0 || j == 0 || i == n+1 || j == m+1){
                a[i][j] = 0;
                continue;
            }
            cin >> a[i][j];
        }
    }
}
bool over(int x, int y){return (x<0 || y<0 || x>=n+2 || y>=m+2);}
void bfs(){
    rep(tm, 0, 100 + 100){
        q.push({0, 0});
        memset(seen, false, sizeof(seen));
        memset(cnt, 0, sizeof(cnt));
        int changed = 0;
        while(!q.empty()){
            int x = q.front().first, y = q.front().second; q.pop();
            rep(i, 0, 4){
                int nx = x+dx[i], ny = y+dy[i];
                if(over(nx, ny) || seen[nx][ny]) continue;
                if(a[nx][ny] == 1){
                    cnt[nx][ny]++;
                    if(cnt[nx][ny] == 2){
                        a[nx][ny] = 0, changed++;
                        seen[nx][ny] = true;
                    }
                }else{
                    seen[nx][ny] = true;
                    q.push({nx, ny});
                }
            }
        }
        if(changed == 0){
            cout << tm << endl;
            return;
        }
    }
}
void process(){
    input();
    bfs();
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    process();
    return 0;
}

구현 후 코드리뷰 + 예제 돌려보기(x)

디버깅(x)

좋은 코드

최적화