Dang_Quan_10_Tin

REGIONS TS10 PTNK 2006-2007

Jan 15th, 2022 (edited)
1,068
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #define task "REGIONS"
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <queue>
  6.  
  7. using namespace std;
  8.  
  9. using ll = long long;
  10. using ld = long double;
  11.  
  12. constexpr int N = 1e3 + 5;
  13. int hor[] = {0, 0, -1, 1},
  14.     ver[] = {-1, 1, 0, 0};
  15.  
  16. int m, n, k;
  17. bool IsRoad[N][N], vis[N][N];
  18.  
  19. void Read()
  20. {
  21.     cin >> m >> n >> k;
  22.  
  23.     for (int i = 1; i <= k; ++i)
  24.     {
  25.         int x, y, z, t;
  26.         cin >> x >> y >> z >> t;
  27.  
  28.         // Đánh dấu đường đi
  29.         if (x == z)
  30.             for (int j = y; j <= t; ++j)
  31.                 IsRoad[x][j] = 1;
  32.         else
  33.             for (int j = x; j <= z; ++j)
  34.                 IsRoad[j][y] = 1;
  35.     }
  36. }
  37.  
  38. void BFS(int x, int y) // Thực hiện loang
  39. {
  40.     queue<pair<int, int>> q;
  41.  
  42.     q.emplace(x, y);
  43.     vis[x][y] = 1;
  44.  
  45.     while (!q.empty())
  46.     {
  47.         pair<int, int> c = q.front();
  48.         q.pop();
  49.  
  50.         for (int i = 0; i < 4; ++i)
  51.         {
  52.             int u = c.first + hor[i],
  53.                 v = c.second + ver[i];
  54.  
  55.             if (u > 0 && u <= m && v > 0 && v <= n && !IsRoad[u][v] && !vis[u][v]) // Không loang vào đường đi
  56.             {
  57.                 vis[u][v] = 1;
  58.                 q.emplace(u, v);
  59.             }
  60.         }
  61.     }
  62. }
  63.  
  64. void Solve()
  65. {
  66.     int ans(0);
  67.  
  68.     for (int i = 1; i <= m; ++i)
  69.         for (int j = 1; j <= n; ++j)
  70.             if (!IsRoad[i][j] && !vis[i][j]) // Một miền được liệt kê
  71.             {
  72.                 BFS(i, j);
  73.                 ++ans;
  74.             }
  75.  
  76.     cout << ans;
  77. }
  78.  
  79. int32_t main()
  80. {
  81.     ios::sync_with_stdio(0);
  82.     cin.tie(0);
  83.     cout.tie(0);
  84.     if (fopen(task ".INP", "r"))
  85.     {
  86.         freopen(task ".INP", "r", stdin);
  87.         freopen(task ".OUT", "w", stdout);
  88.     }
  89.  
  90.     Read();
  91.     Solve();
  92. }
  93.  
Add Comment
Please, Sign In to add comment