Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "REGIONS"
- #include <iostream>
- #include <cstdio>
- #include <queue>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 1e3 + 5;
- int hor[] = {0, 0, -1, 1},
- ver[] = {-1, 1, 0, 0};
- int m, n, k;
- bool IsRoad[N][N], vis[N][N];
- void Read()
- {
- cin >> m >> n >> k;
- for (int i = 1; i <= k; ++i)
- {
- int x, y, z, t;
- cin >> x >> y >> z >> t;
- // Đánh dấu đường đi
- if (x == z)
- for (int j = y; j <= t; ++j)
- IsRoad[x][j] = 1;
- else
- for (int j = x; j <= z; ++j)
- IsRoad[j][y] = 1;
- }
- }
- void BFS(int x, int y) // Thực hiện loang
- {
- queue<pair<int, int>> q;
- q.emplace(x, y);
- vis[x][y] = 1;
- while (!q.empty())
- {
- pair<int, int> c = q.front();
- q.pop();
- for (int i = 0; i < 4; ++i)
- {
- int u = c.first + hor[i],
- v = c.second + ver[i];
- if (u > 0 && u <= m && v > 0 && v <= n && !IsRoad[u][v] && !vis[u][v]) // Không loang vào đường đi
- {
- vis[u][v] = 1;
- q.emplace(u, v);
- }
- }
- }
- }
- void Solve()
- {
- int ans(0);
- for (int i = 1; i <= m; ++i)
- for (int j = 1; j <= n; ++j)
- if (!IsRoad[i][j] && !vis[i][j]) // Một miền được liệt kê
- {
- BFS(i, j);
- ++ans;
- }
- cout << ans;
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- Solve();
- }
Add Comment
Please, Sign In to add comment