Gidhub BE Developer

[Programmers] 카카오프렌즈 컬러링북

2018-07-22
goodGid

Problem

Problem URL : 카카오프렌즈 컬러링북


[1] Answer Code (18. 07. 23)


#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;

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


int dfs(int x,int y){
    if(visit[x][y] || map[x][y] == 0)
        return 0;
    
    visit[x][y] = 1;
    
    int ret = 1 ;
    for(int k=0; k<4; k++){
        int nx = x + dx[k];
        int ny = y + dy[k];
        if( nx < 0 || nx >= N || ny < 0 || ny >= M
           || map[x][y] != map[nx][ny]
           || visit[nx][ny]){
            continue;
        }
        ret += dfs(nx, ny);
    }
    return ret;
}

vector<int> solution(int n, int m, vector<vector<int>> picture) {
    memset(visit, 0, sizeof(visit));    // [3]
    N = n;
    M = m;
    int number_of_area = 0;
    int max_size_of_one_area = 0;
    
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            map[i][j] = picture[i][j];
        }
    }
    
    
    for(int i=0; i<n; i++){
        for(int j=0; j<m; j++){
            int result = dfs(i,j);
            if(result > 0 ){
                number_of_area ++;
                max_size_of_one_area = max_size_of_one_area > result? max_size_of_one_area : result;
            }

        }
    }

    vector<int> answer(2);
    answer[0] = number_of_area;
    answer[1] = max_size_of_one_area;
    return answer;
}


int main(){
    

    // [1]
    /*
    vector<vector<int>> v(6);
    v[0].push_back(1); v[0].push_back(1); v[0].push_back(1); v[0].push_back(0);
    v[1].push_back(1); v[1].push_back(2); v[1].push_back(2); v[1].push_back(0);
    v[2].push_back(1); v[2].push_back(0); v[2].push_back(0); v[2].push_back(1);
    v[3].push_back(0); v[3].push_back(0); v[3].push_back(0); v[3].push_back(1);
    v[4].push_back(0); v[4].push_back(0); v[4].push_back(0); v[4].push_back(3);
    v[5].push_back(0); v[5].push_back(0); v[5].push_back(0); v[5].push_back(3);
    solution(6, 4, v );
     */
    
    // [2]
    vector<int> ans = solution(6,4,{
        {1, 1, 1, 0},
        {1, 2, 2, 0},
        {1, 0, 0, 1},
        {0, 0, 0, 1},
        {0, 0, 0, 3},
        {0, 0, 0, 3}});
       
    return 0;
}

[1] Answer Code (18. 07. 22)

  • [1] 처럼 노가다가 아닌 [2]처럼 타입에 맞게 넘겨주기만 하면 된다.

  • [3] 전역으로 선언했으니 당연히 초기화되어있을꺼라 생각했는데 아니였다. 반드시 초기화를 해줘야한다 !!!


Recommend

Index