C - Sensors
但看数据发现是经典油田问题,直接dfs
#include<bits/stdc++.h>
using namespace std;
int n,m;
int dx[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int vis[1005][1005];
char mp[1005][1005];
void dfs(int x,int y){
vis[x][y]=1;
for(int i=0;i<8;i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&mp[nx][ny]=='#'&&vis[nx][ny]==0){
dfs(nx,ny);
}
}
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>mp[i][j];
}
}
int ans=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!vis[i][j]&&mp[i][j]=='#'){
dfs(i,j);
ans++;
}
}
}
cout<<ans;
return 0;
}
标签:AtCoder,Beginner,Contest,int,dfs,vis,&&,325,1005
From: https://www.cnblogs.com/yufan1102/p/17908990.html