fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. const int N = 1e3 + 5;
  6. int m, n, k;
  7. int b[N][N];
  8. int dx[4] = {1, -1, 0, 0};
  9. int dy[4] = {0, 0, 1, -1};
  10. const int INF = 1e9 + 5;
  11.  
  12. void loang(int x, int y, int val) {
  13. queue<pair<int, int>> q;
  14. q.push({x, y});
  15. b[x][y] = INF; // Mark the cell as visited
  16. while (!q.empty()) {
  17. auto [cx, cy] = q.front(); q.pop();
  18. for (int d = 0; d < 4; ++d) {
  19. int nx = cx + dx[d];
  20. int ny = cy + dy[d];
  21. if (nx >= 0 && nx < m && ny >= 0 && ny < n && b[nx][ny] == val) {
  22. b[nx][ny] = INF; // Mark as visited
  23. q.push({nx, ny});
  24. }
  25. }
  26. }
  27. }
  28.  
  29. int32_t main() {
  30. ios_base::sync_with_stdio(false);
  31. cin.tie(0);
  32. cout.tie(0);
  33. freopen("cake.inp", "r", stdin);
  34. freopen("cake.out", "w", stdout);
  35.  
  36. while (true) {
  37. cin >> m >> n;
  38. if (m == 0 && n == 0) break;
  39.  
  40. cin >> k;
  41. memset(b, 0, sizeof(b)); // Initialize the grid to 0
  42.  
  43. for (int i = 1; i <= k; ++i) {
  44. int x1, y1, x2, y2;
  45. cin >> x1 >> y1 >> x2 >> y2;
  46. // Fill the rectangle
  47. for (int x = x1; x < x2; ++x) {
  48. for (int y = y1; y < y2; ++y) {
  49. b[x][y] += (1 << (i - 1)); // Use i-1 to set the correct bit
  50. }
  51. }
  52. }
  53.  
  54. int ans = 0;
  55. for (int i = 0; i < m; ++i) {
  56. for (int j = 0; j < n; ++j) {
  57. if (b[i][j] != INF && b[i][j] != 0) { // Ensure it's not visited and not empty
  58. ans++;
  59. loang(i, j, b[i][j]);
  60. }
  61. }
  62. }
  63. cout << ans << "\n"; // Output the count of distinct regions
  64. }
  65.  
  66. return 0;
  67. }
  68.  
Success #stdin #stdout 0s 5284KB
stdin
3 5
3
1 1 3 2
4 0 2 3
4 0 5 1
6 6
2
2 0 5 3
3 1 4 2
0 0
stdout
Standard output is empty