본문 바로가기
백준 알고리즘/BFS

백준 2583번 C++

by paysmile 2019. 1. 8.

#include <iostream>

#include <vector>

#include <algorithm>

#include <cstring>

using namespace std;


int M, N, K;

int arr[100][100];

int visited[100][100];

int ans[100];

int counts;


typedef struct {

int x;

int y;

}Move;

Move m[4] = { {1,0},{-1,0},{0,1},{0,-1} };


void dfs(int a, int b) {

visited[a][b] = 1;

counts += 1;


for (int i = 0; i < 4; i++) {

int ma = a + m[i].x;

int mb = b + m[i].y;


if (ma >= 0 && ma < M && mb >= 0 && mb < N) {

if (arr[ma][mb] == 0 && visited[ma][mb] == 0)

dfs(ma, mb);

}

}

}


int main(void) {

cin >> M >> N >> K;


memset(visited, 0, sizeof(visited));

memset(arr, 0, sizeof(arr));

for (int i = 0; i < K; i++) {

int x1, y1, x2, y2;

cin >> x1 >> y1 >> x2 >> y2;

for (int z = y1; z < y2; z++) {

for (int k = x1; k < x2; k++) {

arr[z][k] = 1;

}

}

}

vector<int> answer;


for (int i = 0; i < M; i++) {

for (int j = 0; j < N; j++) {

if (arr[i][j] == 0 && visited[i][j] == 0) {

counts = 0;

dfs(i, j);

answer.push_back(counts);

}

}

}

sort(answer.begin(), answer.end());

cout << answer.size() << endl;

for (int i = 0; i < answer.size(); i++) {

cout << answer[i] << " ";

}

return 0;

}



'백준 알고리즘 > BFS' 카테고리의 다른 글

백준 14502번 C++  (0) 2019.01.10
백준 2468번 C++  (0) 2019.01.09
백준 11724번 C++  (0) 2019.01.09
백준 11403번 C++  (0) 2019.01.07
백준 1697번 C++  (0) 2019.01.07