AtCoder Beginner Contest 325

C - Sensors

image

但看数据发现是经典油田问题,直接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;
} 
posted @ 2023-12-17 13:40  yufan1102  阅读(10)  评论(0编辑  收藏  举报