HelloMinchan
처음처럼
HelloMinchan
LinkedIn
전체 방문자
오늘
어제
  • 분류 전체보기 (306)
    • Backend (4)
      • NestJS (1)
      • Express (1)
      • Spring (2)
    • Infrastructure (1)
      • AWS (1)
    • Frontend (1)
      • Next.js (1)
    • Language & Runtime (4)
      • Java (2)
      • Node.js (2)
    • Computer Science (8)
      • Computer Networks (3)
      • Operating Systems (4)
      • OOP (1)
    • 독서 (4)
      • 데이터 중심 애플리케이션 설계 (3)
      • 객체지향의 사실과 오해 (1)
    • 회고 (4)
      • Project (2)
      • Career (2)
    • Deprecated (280)

채널

  • GitHub
  • LinkedIn

최근 글

태그

  • 프로그래머스Python
  • 백준Python
  • 알고스팟
  • Baekjoon Online Judge
  • 데이터베이스
  • back-end
  • Database
  • 개발자
  • programmers
  • 백엔드
  • 백준
  • 백준C++
  • 알고스팟Python
  • front-end
  • 프로그래밍
  • 코딩
  • 백준Go
  • Algospot
  • 프로그래머스
  • 프로그래머스C++

최근 댓글

인기 글

hELLO
HelloMinchan

처음처럼

[programmers] 프로그래머스 카카오프렌즈 컬러링북(C++)
Deprecated

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

2020. 6. 7. 18:38

(주)그렙

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

(C++)

(글쓴날 : 2020.06.07)

 


* programmers, 프로그래머스 문제 C++ 언어 풀이입니다.

* 소스 코드의 저작권은 글쓴이에게 있습니다.


 

 

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


1) 문제

문제 링크 : https://programmers.co.kr/learn/courses/30/lessons/1829

 

코딩테스트 연습 - 카카오프렌즈 컬러링북

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]] [4, 5]

programmers.co.kr


2) 풀이 과정

* 시간 복잡도 : O(n²)

 

2차원 형태의 컬러링 북이 주어질 때, 색깔별로 다른 구간의 수와, 가장 넓은 범위의 크기를 구하는 문제입니다.

 

저의 경우, BFS를 적용하였으며, C++을 사용했습니다.

방문 기록을 체크할 컬러링 북과 동일한 크기의 2차원 vector를 만든 뒤 큐를 이용한 BFS를 적용하여 문제를 해결했습니다.


3) 코드

 

* C++ 코드

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <vector>
#include <iostream>
#include <queue>
#include <utility>
 
using namespace std;
 
int max_size_of_one_area;
vector<vector<bool>> visit;
 
void BFS(int color, int dx[], int dy[], vector<vector<int>> &picture, int m, int n, int x, int y)
{
    queue<pair<int, int>> q;
 
    q.push({x, y});
 
    int i = 0, j = 0;
    int ii = 0, jj = 0;
    int count = 0;
 
    while (!q.empty())
    {
        i = q.front().first;
        j = q.front().second;
 
        q.pop();
 
        if (visit[i][j])
            continue;
 
        visit[i][j] = true;
        count++;
 
        for (int way = 0; way < 4; way++)
        {
            ii = i + dx[way];
            jj = j + dy[way];
 
            if (ii < 0 || ii > m - 1 || jj < 0 || jj > n - 1)
                continue;
 
            if (picture[ii][jj] != color)
                continue;
 
            q.push({ii, jj});
        }
    }
 
    if (max_size_of_one_area < count)
        max_size_of_one_area = count;
}
 
vector<int> solution(int m, int n, vector<vector<int>> picture)
{
    max_size_of_one_area = 0;
 
    visit = vector<vector<bool>>(m, vector<bool>(n, false));
    int dx[4] = {1, -1, 0, 0};
    int dy[4] = {0, 0, 1, -1};
    int count = 0;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (picture[i][j] != 0 && !visit[i][j])
            {
                BFS(picture[i][j], dx, dy, picture, m, n, i, j);
                count++;
            }
        }
    }
 
    vector<int> answer(2);
    answer[0] = count;
    answer[1] = max_size_of_one_area;
    return answer;
}

 

저작자표시 비영리 변경금지 (새창열림)

'Deprecated' 카테고리의 다른 글

[programmers] 프로그래머스 예산 Level 3(Python)  (0) 2020.06.07
[programmers] 프로그래머스 정수 삼각형(Python)  (0) 2020.06.07
[programmers] 프로그래머스 스킬트리(Python)  (0) 2020.06.07
[programmers] 프로그래머스 [1차] 다트 게임(C++)  (0) 2020.06.06
[programmers] 프로그래머스 실패율(C++)  (0) 2020.06.06
    'Deprecated' 카테고리의 다른 글
    • [programmers] 프로그래머스 예산 Level 3(Python)
    • [programmers] 프로그래머스 정수 삼각형(Python)
    • [programmers] 프로그래머스 스킬트리(Python)
    • [programmers] 프로그래머스 [1차] 다트 게임(C++)
    HelloMinchan
    HelloMinchan
    Though you should not fear failure, You should do your very best to avoid it.

    티스토리툴바