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 | #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <vector> #include <algorithm> using namespace std; int m, n, k; int map[101][101]; bool check[101][101]; int dx[] = { 1,-1,0,0 }; int dy[] = { 0,0,1,-1 }; int dfs(int row, int col) { if (check[row][col]) return 0; check[row][col] = true; int temp = 1; for (int i = 0; i < 4; i++) { int nx = col + dx[i]; int ny = row + dy[i]; if (0 <= nx && nx < n && 0 <= ny && ny < m) { if (!check[ny][nx] && map[ny][nx] == 0) { temp += dfs(ny, nx); } } } return temp; } int main() { scanf("%d %d %d", &m, &n, &k); for (int i = 0; i < k; i++) { int x1, y1, x2, y2; scanf("%d %d %d %d", &x1, &y1, &x2, &y2); for (int y = y1; y < y2; y++) { for (int x = x1; x < x2; x++) { map[y][x] = 1; } } } vector<int> a; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (map[i][j] == 0 && !check[i][j]) { int count = dfs(i, j); a.push_back(count); } } } sort(a.begin(), a.end()); printf("%d\n", a.size()); for (int i = 0; i < a.size(); i++) { printf("%d ", a[i]); } return 0; } | cs |
항상 이런문제를 풀 때는 row, column과 x, y를 헷갈리기 마련.
그러니 문제 풀기 전 본인만의 스타일을 확실히 해야한다.
배열이든 함수호출이든 row에 해당하는 값을 먼저 쓸 것. (row 또는 y)
백준 2468 안전 영역 (0) | 2018.05.17 |
---|---|
백준 11724 연결 요소의 개수 (0) | 2018.05.17 |
백준 2667 단지번호붙이기 (0) | 2018.05.17 |
백준 1012 유기농 배추 (0) | 2018.05.17 |
백준 1260 DFS와 BFS (0) | 2018.05.16 |
댓글,
jayharvey
머신러닝/딥러닝 관련 글을 포스팅할 예정입니다 :)