题目链接
dfs水题
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<time.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#define pi acos(-1)
#define maxn 111111
#define maxm 11111
#define INF 0x3F3F3F3F
#define eps 1e-8
#define pb push_back
#define mem(a) memset(a,0,sizeof a)
using namespace std ;
const long long mod = 1000000007 ;
int n, m;
char smp[111 ][111 ];
int mp[111 ][111 ];
bool vis[111 ][111 ];
int stepx[9 ] = { 0 , -1 , 0 , 1 , 0 , -1 , -1 , 1 , 1 };
int stepy[9 ] = { 0 , 0 , -1 , 0 , 1 , -1 , 1 , -1 , 1 };
bool pan(int x, int y) {
if (x >= 1 && x <= n && y >= 1 && y <= m) return 1 ;
return 0 ;
}
void dfs(int x, int y) {
for (int i = 1 ; i <= 8 ; i++) {
int xx = x + stepx[i];
int yy = y + stepy[i];
if (pan(xx, yy) && !vis[xx][yy] && !mp[xx][yy] ) {
vis[xx][yy] = 1 ;
dfs(xx, yy);
}
}
}
int main() {
while (scanf ("%d%d" , &n, &m) && (n != 0 && m != 0 )) {
for (int i = 0 ; i < n; i++) {
scanf ("%s" , smp[i]);
}
for (int i = 1 ; i <= n; i++) {
for (int j = 1 ; j <= m; j++) {
if (smp[i - 1 ][j - 1 ] == '*' ) mp[i][j] = 1 ;
else mp[i][j] = 0 ;
}
}
memset (vis, 0 , sizeof vis);
int cnt = 0 ;
for (int i = 1 ; i <= n;i++)
for (int j = 1 ; j <= m; j++) {
if (!vis[i][j] && !mp[i][j]) {
cnt++;
vis[i][j] = 1 ;
dfs(i, j);
}
}
printf ("%d\n" , cnt);
}
return 0 ;
}