일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 방통대 대학원 정보과학과
- Code Jam 2022
- hackerrank
- 하늘과 바람과 별과 詩
- 프로그래머스
- on-prem
- secondlowestgrade
- LEVEL 2
- GitLab
- 파이썬
- 정보과학과
- MySQL
- 방송통신대학교 대학원 정보과학과
- C++
- ingress-nginx
- 2022
- swift
- 해커랭크
- Qualification Round
- Code Jam
- 하늘과 바람과 별과 시
- Kubernetes
- openebs
- K8S
- 3D PRINTING
- ESXi 업데이트
- nestedlists
- 코딩테스트
- Python
Archives
- Today
- Total
공대생의 비망록
[프로그래머스][Lv. 2] 카카오프렌즈 컬러링북 C++ 풀이 본문
https://programmers.co.kr/learn/courses/30/lessons/1829
2017 카카오코드 예선에 나온 문제입니다.
풀이는 추후에 차차 올리도록 하겠습니다...
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
|
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
int width = 0, height = 0;
int** checked = NULL;
// 0: CHECKED ALREADY OR DIFFERENT COLOR
// 1: SAME REGION
int checkRegion(int i, int j, vector<vector<int>>& picture, int color) {
if (i < 0 || i == height || j < 0 || j == width) { return 0; }
if (checked[i][j] == 1 || color != picture[i][j] || picture[i][j] == 0) { return 0; }
int curColor = picture[i][j], count = 1;
checked[i][j] = 1;
count += checkRegion(i - 1, j, picture, curColor); // U
count += checkRegion(i, j - 1, picture, curColor); // L
count += checkRegion(i, j + 1, picture, curColor); // R
count += checkRegion(i + 1, j, picture, curColor); // D
return count;
}
// 전역 변수를 정의할 경우 함수 내에 초기화 코드를 꼭 작성해주세요.
vector<int> solution(int m, int n, vector<vector<int>> picture) {
int regions = 0, maxSum = 0;
width = n, height = m;
checked = (int**)malloc(sizeof(int*) * height);
for (int i = 0; i < height; i++) {
*(checked + i) = (int*)malloc(sizeof(int) * width);
memset(*(checked + i), 0, sizeof(int) * width);
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (checked[i][j] == 0 && picture[i][j] > 0) {
regions++;
int count = checkRegion(i, j, picture, picture[i][j]);
if (maxSum < count) {
maxSum = count;
}
}
}
}
for (int i = 0; i < height; i++) {
free(*(checked + i));
}
free(checked);
vector<int> answer(2);
answer[0] = regions;
answer[1] = maxSum;
return answer;
}
|
cs |
'Programming Language > C, C++' 카테고리의 다른 글
[프로그래머스][Lv. 2] 주식가격 C++ 풀이 (0) | 2022.05.04 |
---|---|
[프로그래머스][Lv. 2] 행렬의 곱셈 C++ 풀이 (0) | 2022.05.03 |
[C++] map 컨테이너에 데이터 추가하기 insert()? [] 연산자? (0) | 2022.05.03 |
[프로그래머스][Lv. 2] 124 나라의 숫자 C++ 풀이 (0) | 2022.04.17 |
[Google Code Jam][Qualification Round 2022] 3D Printing C++ 풀이 (0) | 2022.04.03 |
Comments