[프로그래머스] 카카오프렌즈 컬러링북 - C++

level2

Posted by dongjune on January 1, 2021

문제

프로그래머스 카카오프렌즈 컬러링북

풀이

DFS를 활용하여 영역의 개수를 세줬습니다. 다음은 dfs를 구현한 재귀함수입니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// y,x : 현재 좌표
// type : 영역의 종류
int countArea(const vector<vector<int>> &picture,int y,int x,int type){
    visit[y][x]=true;
    int ret=1;

    for(int i=0;i<4;i++){
        int ny=y+dy[i];
        int nx=x+dx[i];
        // 영역의 범위를 벗어나거나 방문한 적 있으면 continue
        if(ny<0 || nx<0 || ny>=M || nx>=N || visit[ny][nx]) continue;
        // 영역의 종류가 다르면 continue
        if(picture[ny][nx]!=type) continue;
        
        // 재귀함수 호출
        ret+=countArea(picture,ny,nx,type);
    }

    return ret;
}

코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <cstring>
#include <algorithm>

using namespace std;

int M,N;
int dy[4]={0,-1,0,1};
int dx[4]={-1,0,1,0};
bool visit[100][100];

int countArea(const vector<vector<int>> &picture,int y,int x,int type){
    visit[y][x]=true;
    int ret=1;

    for(int i=0;i<4;i++){
        int ny=y+dy[i];
        int nx=x+dx[i];
        // 영역의 범위를 벗어나거나 방문한 적 있으면 continue
        if(ny<0 || nx<0 || ny>=M || nx>=N || visit[ny][nx]) continue;
        // 영역의 종류가 다르면 continue
        if(picture[ny][nx]!=type) continue;
        
        // 재귀함수 호출
        ret+=countArea(picture,ny,nx,type);
    }

    return ret;
}

// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
vector<int> solution(int m, int n, vector<vector<int>> picture) {
    M=m,N=n;
    // 영역의 개수, 가장 큰 영역의 크기
    int countRegions=0,maxRegion=0;
    
    // 전역변수 visit 초기화
    memset(visit,0,sizeof(visit));

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            if(!visit[i][j] && picture[i][j]!=0){
                countRegions++;
                maxRegion = max(maxRegion, countArea(picture,i,j,picture[i][j]));
            }
        }
    }
    return {countRegions,maxRegion};
}