영역 구하기
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <math.h> #include <algorithm> int arr[101][101]; int col, row, n; int count; int bound(int i, int j) { if (i >= 0 && i < col && j >= 0 && j < row) { return 0; } else { return -1; } } void clear(int i, int j) { arr[i][j] = 1; count++; if (bound(i + 1, j) == 0) { if (arr[i + 1][j] == 0) { clear(i + 1, j); } } if (bound(i - 1, j) == 0) { if (arr[i - 1][j] == 0) { clear(i - 1, j); } } if (bound(i, j + 1) == 0) { if (arr[i][j+1] == 0) { clear(i, j + 1); } } if (bound(i, j - 1) == 0) { if (arr[i][j-1] == 0) { clear(i, j-1); } } } int main() { for (int i = 0; i < 101; i++) { for (int j = 0; j < 101; j++) { arr[i][j] = 0; } } scanf("%d %d %d", &col, &row, &n); int a, b, c, d; for (int i = 0; i < n; i++) { scanf("%d %d %d %d", &a, &b, &c, &d); for (int k = b; k < d; k++) { for (int j = a; j < c; j++) { arr[k][j] = 1; } } } int num = 0; int output[10000]; for (int i = 0; i < col; i++) { for (int j = 0; j < row; j++) { if (arr[i][j] == 0) { count = 0; clear(i,j); output[num] = count; num++; } } } std::sort(output, output + num); printf("%d\n", num); for (int i = 0; i < num; i++) { printf("%d ", output[i]); } }
1012번 <유기농 배추>의 정말살찍진짜조금 응용문제!
0이 들어오면 해당 영역을 재귀로 순환하며 1로 바꾸어 없에며 넓이, 개수를 판단한다.
그리고 단순히 정렬을 하려면 굳이 qsort할 필요 없음.
memset은 C의 stdio.h라이브러리에 포함되어 있으며 C++에서 동작하지 않음.
따라서 백준에 정답 입력시 c++로 채점을 돌리면 컴파일 에러가 발생.
C99로 돌리거나 memset대신 fill함수를 사용해야 한다.
대신 fill로 2차원 배열을 초기화 하려면 역참조 배열을 사용해야 한다.