0%

[題解]油田

a102. 油田

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0),cin.tie(0)
#define lld long long
#define N 105
using namespace std;
int n,m,ans = 0;

int dx[8] = {-1,-1,-1,0,1,1,1,0};
int dy[8] = {1,0,-1,-1,-1,0,1,1};

bool visit[N][N],maze[N][N];

void dfs(int x,int y){
visit[x][y] = 1;
for(int i=0;i<8;i++){
int nx = x+dx[i],ny = y+dy[i];
if(nx<1||nx>n||ny<1||ny>m)continue;
if(visit[nx][ny])continue;
if(maze[nx][ny]==1)dfs(nx,ny);
}
}

void solve(){
memset(visit,0,sizeof(visit));
memset(maze,0,sizeof(maze));
ans = 0;

for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
char temp;cin>>temp;
if(temp=='@')maze[i][j] = 1;
else maze[i][j] = 0;
}
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(maze[i][j]==1 && visit[i][j]==0){
dfs(i,j);
ans+=1;
}
}
}
cout<<ans<<endl;
}

signed main(){
while(cin>>n>>m && n&& m){
solve();
}
}