-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkakao_friends_coloring_book_bfs.cpp
50 lines (46 loc) · 1.8 KB
/
kakao_friends_coloring_book_bfs.cpp
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
#include <vector>
#include <queue>
#include <utility>
#define MAXSIZE 100
using namespace std;
typedef pair<int, int> Node;
vector<int> solution(int m, int n, vector<vector<int>> picture) {
int number_of_area = 0;
int max_size_of_one_area = 0;
int size = 0, color, row, col;
int visited[MAXSIZE][MAXSIZE] = {0};
queue<Node> q;
Node node;
for(int i=0; i<m; i++) {
for(int j=0; j<n; j++) {
if(picture[i][j] && !visited[i][j]) {
size = 0;
color = picture[i][j];
number_of_area++;
q.push(make_pair(i, j));
while(!q.empty()) {
node = q.front(); q.pop();
row = node.first;
col = node.second;
if(!visited[row][col]) {
visited[row][col] = 1;
size++;
if(row + 1 < m && picture[row + 1][col] == color && !visited[row + 1][col])
q.push(make_pair(row + 1, col));
if(col + 1 < n && picture[row][col + 1] == color && !visited[row][col + 1])
q.push(make_pair(row, col + 1));
if(0 <= row - 1 && picture[row - 1][col] == color && !visited[row - 1][col])
q.push(make_pair(row - 1, col));
if(0 <= col - 1 && picture[row][col - 1] == color && !visited[row][col - 1])
q.push(make_pair(row, col - 1));
}
}
max_size_of_one_area = size > max_size_of_one_area ? size : max_size_of_one_area;
}
}
}
vector<int> answer(2);
answer[0] = number_of_area;
answer[1] = max_size_of_one_area;
return answer;
}