Problem - D - Codeforces
题意:
思路:
直接搜索即可,对于每个连通块都去染色,对于每一个色块都维护这个色块的墙壁数
或者麻烦点用并查集维护也行
Code:
int n, m, k;
bool st[N][N];
char g[N][N];
int ans[N * N];
int id[N][N];
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
void dfs(int sx, int sy, int cnt)
{
st[sx][sy] = 1;
id[sx][sy] = cnt;
for(int i = 0; i < 4; i ++ )
{
int tx = sx + dx[i], ty = sy + dy[i];
if(tx < 0 || tx >= n || ty >= m || ty < 0 || st[tx][ty]) continue;
if(g[tx][ty] == '*') ans[cnt] ++;
else dfs(tx, ty, cnt);
}
}
signed main()
{
FAST
cin >> n >> m >> k;
for(int i = 0; i < n; i ++ ) cin >> g[i];
int cnt = 0;
for(int i = 0; i < n; i ++ )
for(int j = 0; j < m; j ++ )
if(!st[i][j] && g[i][j] == '.') dfs(i, j, cnt ++ );
while(k --)
{
int x, y; cin >> x >> y;
x --, y --;
cout << ans[id[x][y]] << '\n';
}
return 0;
}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mxn=1e3+10;
const int mxv=1e6+10;
const int mod=1e9+7;
string s[mxn];
set<array<int,4> > S2;
multiset<pair<int,int> > S[mxv];
int N,M,K,x,y;
int F[mxv],st[mxn][mxn];
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};
int find(int x){
return F[x]=(x==F[x])?x:find(F[x]);
}
void join(int u,int v){
int f1=find(u),f2=find(v);
if(f1!=f2) F[f1]=f2;
}
bool check(int x,int y){
return x>=1&&x<=N&&y>=1&&y<=M;
}
int calc(int x,int y){
return (x-1)*M+y;
}
void dfs(int x,int y){
//cout<<x<<" "<<y<<'\n';
for(int i=0;i<4;i++){
int vx=x+dx[i];
int vy=y+dy[i];
if(check(vx,vy)&&!st[vx][vy]){
if(s[vx][vy]=='.'){
st[vx][vy]=1;
join(calc(x,y),calc(vx,vy));
//cout<<calc(x,y)<<" "<<calc(vx,vy)<<'\n';
dfs(vx,vy);
}else{
S2.insert({x,y,vx,vy});
//cout<<x<<" "<<y<<" "<<vx<<" "<<vy<<'\n';
}
}
}
}
void solve(){
cin>>N>>M>>K;
for(int i=1;i<=N;i++){
cin>>s[i];
s[i]=" "+s[i];
}
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++) F[calc(i,j)]=calc(i,j);
}
for(int i=1;i<=N;i++){
for(int j=1;j<=M;j++){
if(!st[i][j]&&s[i][j]=='.'){
st[i][j]=1;
dfs(i,j);
}
}
}
for(auto v:S2){
S[find(calc(v[0],v[1]))].insert({v[2],v[3]});
}
for(int i=1;i<=K;i++){
cin>>x>>y;
cout<<S[find(calc(x,y))].size()<<'\n';
}
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int __=1;//cin>>__;
while(__--)solve();return 0;
}