fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <vector>
  5. #include <set>
  6. #include <climits>
  7. #include <algorithm>
  8. #include <map>
  9. using namespace std;
  10.  
  11. int n, m;
  12. char a[1001][1001];
  13.  
  14. bool visited[1001][1001];
  15. int dx[4] = { 1,-1,0,0 };
  16. int dy[4] = { 0,0,1,-1 };
  17.  
  18. const int MOD = (int)(1e9 + 7);
  19.  
  20. void dfs(int x, int y, int& area) {
  21. visited[x][y] = 1;
  22. area++;
  23. for (int i = 0;i < 4;i++) {
  24. int nx = x + dx[i];
  25. int ny = y + dy[i];
  26.  
  27. if (nx >= 0 && nx < n && ny >= 0 && ny < m && !visited[nx][ny] && a[nx][ny] == '*') {
  28. dfs(nx, ny, area);
  29. }
  30. }
  31. }
  32.  
  33. int main() {
  34. ios_base::sync_with_stdio(false);
  35. cin.tie(NULL);
  36.  
  37. cin >> n >> m;
  38.  
  39. multiset<int> se;
  40.  
  41. for (int i = 0;i < n;i++) {
  42. for (int j = 0;j < m;j++) {
  43. cin >> a[i][j];
  44. }
  45. }
  46.  
  47. for (int i = 0;i < n;i++) {
  48. for (int j = 0;j < m;j++) {
  49. if (a[i][j] == '*' && !visited[i][j]) {
  50. int area = 0;
  51. dfs(i, j, area);
  52. se.insert(area);
  53. }
  54. }
  55. }
  56. for (auto value : se) {
  57. cout << value << " ";
  58. }
  59. return 0;
  60. }
  61.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty