일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- D2
- data structure
- 프로그래머스
- 문제풀이
- 독서
- 알고리즘
- 운영체제
- Programmers
- D3
- 감상문
- c++
- algorithm
- db
- LeetCode
- network
- 재테크/투자
- cs
- BOJ
- 네트워크
- Computer Science
- Database
- 자료구조
- 법의학
- language
- algogritim
- swea
- SW Expert Academy
- 백준
- OS
- 데이터베이스
Archives
- Today
- Total
선택은 나의 것
[프로그래머스] 카카오프렌즈 컬러링북 본문
문제
Programmers 2017 카카오코드 예선 : 카카오프렌즈 컬러링북
코딩테스트 연습 - 카카오프렌즈 컬러링북
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
접근
모든 좌표에 대해 dfs를 통해 영역의 개수와 가장 큰 영역의 크기를 구해주었다.
코드
#include <vector>
using namespace std;
int number_of_area, max_size_of_one_area, sum;
int visit[110][110];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
void dfs(int x, int y, int m, int n, vector<vector<int>> picture) {
visit[x][y] = 1;
sum++;
for (int i = 0; i < 4; i++) {
int newx = x + dx[i];
int newy = y + dy[i];
if (newx < 0 || newx >= m || newy < 0 || newy >= n || picture[newx][newy] != picture[x][y] || visit[newx][newy] == 1)
continue;
dfs(newx, newy, m, n, picture);
}
}
vector<int> solution(int m, int n, vector<vector<int>> picture) {
number_of_area = 0;
max_size_of_one_area = 0;
for (int i = 0; i < 110; i++) {
for (int j = 0; j < 110; j++) {
visit[i][j] = 0;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (visit[i][j] == 0 && picture[i][j] != 0) {
sum = 0;
dfs(i, j, m, n, picture);
number_of_area++;
if (sum > max_size_of_one_area)
max_size_of_one_area = sum;
}
}
}
vector<int> answer;
answer.push_back(number_of_area);
answer.push_back(max_size_of_one_area);
return answer;
}
'☽ Algorithm > Programmers' 카테고리의 다른 글
[프로그래머스] 로또의 최고 순위와 최저 순위 (0) | 2021.07.08 |
---|---|
[프로그래머스] 중성화 여부 파악하기 (SQL) (0) | 2020.08.10 |
[프로그래머스] 튜플 (0) | 2020.05.27 |
[프로그래머스] 리틀 프렌즈 사천성 (0) | 2020.05.26 |
[프로그래머스] 파일명 정렬 (python) (0) | 2020.05.23 |
Comments