D. Igor In the Museum

320 阅读2分钟

题目

Igor is in the museum and he wants to see as many pictures as possible. Museum can be represented as a rectangular field of n × m cells. Each cell is either empty or impassable. Empty cells are marked with '.', impassable cells are marked with '*'. Every two adjacent cells of different types (one empty and one impassable) are divided by a wall containing one picture. At the beginning Igor is in some empty cell. At every moment he can move to any empty cell that share a side with the current one. For several starting positions you should calculate the maximum number of pictures that Igor can see. Igor is able to see the picture only if he is in the cell adjacent to the wall with this picture. Igor have a lot of time, so he will examine every picture he can see.

输入

First line of the input contains three integers n, m and k (3 ≤ n, m ≤ 1000, 1 ≤ k ≤ min(n·m, 100 000)) — the museum dimensions and the number of starting positions to process. Each of the next n lines contains m symbols '.', '*' — the description of the museum. It is guaranteed that all border cells are impassable, so Igor can't go out from the museum. Each of the last k lines contains two integers x and y (1 ≤ x ≤ n, 1 ≤ y ≤ m) — the row and the column of one of Igor's starting positions respectively. Rows are numbered from top to bottom, columns — from left to right. It is guaranteed that all starting positions are empty cells.

解法

维护连通块,连通块中的每个值都是相等的

#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <stack>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int long long
#define endl '\n'
#define pb push_back
#define NO cout << "NO" << endl;
#define YES cout << "YES" << endl;
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<VI> VII;
ll MOD;
ll powmod(ll a,ll b) {ll res=1;a%=MOD; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}
mt19937 mrand(random_device{}()); 
const int N = 2e5 + 10;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
int f[N];
int find(int x) {return f[x] == x ? f[x] : f[x] = find(f[x]);}
void solve()
{
    int n, m, k; cin >> n >> m >> k;
    VII st(n + 1,VI(m + 1));
    vector<vector<char>> s(n + 1, vector<char>(m + 1));
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> s[i][j];
    VII a(n + 1, VI(m + 1));
    queue<PII> q, p;
    // cout << "---" << n * m << endl;
    int cnt = 0;
    for (int i = 1; i <= n; i++) 
        for (int j = 1; j <= m; j++) {
            if (s[i][j] == '.' && !st[i][j]) {
                q.push({i, j});
                st[i][j] = 1;
                ll res = 0;
                while (q.size()) {
                    auto [x, y] = q.front();
                    q.pop();
                    p.push({x, y});
                    for (int i = 0; i < 4; i++) {
                        int xx = dx[i] + x, yy = y + dy[i];
                        if (xx < 1 || xx > n || yy < 1 || yy > m || st[xx][yy]) continue;
                        if (s[xx][yy] == '*') {
                            res ++;
                        }
                        cnt ++;
                        if (s[xx][yy] == '*') continue;
                        st[xx][yy] = 1;
                        q.push({xx, yy});
                    }
                }
                // cout << res << endl;
                while(p.size()) {
                    auto [x, y] = p.front();
                    a[x][y] = res;
                    p.pop();
                }
            }
        }
        // cout << "___" << cnt << endl;
    while (k --) {
        int l, r; cin >> l >> r;
        cout << a[l][r] << endl;
    }

}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    // int T;cin >> T;
    // while ( T -- )
    solve();
    return 0;
}