about. What I learned/about.Algorithm

[백준 2468] 안전 영역

#include<bits/stdc++.h>
using namespace std;

int n, m,ret=0, nx, ny, x, y, ma = 0;
int max_len;
int h[104][104];
bool sc[104][104];
set<int> _set;
int dy[4] = {-1,0,1,0};
int dx[4] = {0,1,0,-1}; 

void dfs(int y, int x, int m){
    sc[y][x] = 1;
    for (int i =0; i<4; i++){
        ny = y + dy[i];
        nx = x + dx[i];
        // 인덱스로 표현하면 n보다 하나 작아야하니깐 n까지 포함시켜서 커지면 안됨
        if(nx < 0 || ny < 0 || nx >= n || ny >= n) continue;
        if(h[ny][nx] > m && !sc[ny][nx]){
            dfs(ny,nx,m);
        }
    }
}

int main(){
    cin >> n;
    for (int i =0; i < n; i++){
        for (int j = 0;j < n; j++){
            cin >> h[i][j];
            if(h[i][j] > max_len){
                max_len = h[i][j];
            }
        }
    }

    for (int i = 0 ; i < max_len; i++){
        m = i;
        int ret = 0; 
        fill(&sc[0][0],&sc[0][0] + 104*104,0);
        
        for (int i =0; i < n; i++){
            for (int j = 0;j < n; j++){
                if(h[i][j] > m && !sc[i][j]){
                    dfs(i,j,m);
                    ret++;
                }
               
            }
        }
        if(ma <= ret) {
            ma = ret;
        }
    }
        cout << ma << "\n";

    return 0;
}