Algorithm/Baekjoon
[Algorithm] Baekjoon 2583번 : 영역 구하기
MaxLevel
2023. 2. 26. 22:16
https://www.acmicpc.net/problem/2583
2583번: 영역 구하기
첫째 줄에 M과 N, 그리고 K가 빈칸을 사이에 두고 차례로 주어진다. M, N, K는 모두 100 이하의 자연수이다. 둘째 줄부터 K개의 줄에는 한 줄에 하나씩 직사각형의 왼쪽 아래 꼭짓점의 x, y좌표값과 오
www.acmicpc.net
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
struct Node
{
int y;
int x;
};
int arr[101][101];
int row, col, rectCount;
int dir[4][2] = { {-1,0}, {1,0}, {0,-1}, {0,1} };
// 입력값이 되면 continue.
void color(int leftX, int leftY, int rightX, int rightY)
{
for (int i = leftX; i < rightX; ++i)
{
for (int j = leftY; j < rightY; ++j)
{
arr[j][i] = 1;
}
}
}
int BFS(int startY, int startX)
{
queue<Node> q;
q.push({ startY,startX });
arr[startY][startX] = 1;
int count = 1;
while (!q.empty())
{
int curY = q.front().y;
int curX = q.front().x;
q.pop();
for (int i = 0; i < 4; ++i)
{
int nextY = curY + dir[i][0];
int nextX = curX + dir[i][1];
if (nextY < 0 || nextY == row) continue;
if (nextX < 0 || nextX == col) continue;
if (arr[nextY][nextX] == 1) continue;
q.push({ nextY,nextX });
arr[nextY][nextX] = 1;
++count;
}
}
return count;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b, c, d;
int answer = 0;
vector<int> results;
cin >> row >> col >> rectCount;
for (int i = 0; i < rectCount; ++i)
{
cin >> a >> b >> c >> d;
color(a, b, c, d);
}
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
{
if (arr[i][j] == 1) continue;
++answer;
results.push_back(BFS(i, j));
}
}
sort(results.begin(), results.end());
cout << answer << endl;
for (int& temp : results)
{
cout << temp << ' ';
}
}
|
cs |
색칠만 잘 해놓으면 평범한 기본BFS문제이다.
문제에서는 0,0이 왼쪽아래이고 코드상의 0,0은 왼쪽위라서 색칠했을 때 모양이 완전히 같지는 않지만, 대칭되기 때문에 문제를 푸는데는 아무 상관없다.