L - Oil Deposits HDU - 1241
注意写check的时候要先判x,y是不是越界了,不然直接用数组可能RE, 也可能直接WA了
#include<cstdio>
#include<iostream>
#include<cstring>
const int maxn = 110;
using namespace std;
int m, n;
char mp[maxn][maxn];
bool vis[maxn][maxn];
int dx[] = {0,0,1,-1,1,1,-1,-1};
int dy[] = {1,-1,0,0,-1,1,1,-1};
bool check(int x, int y) {
if(x < 1 || x > m || y > n || y < 1) return 0;
if(vis[x][y] || mp[x][y] == '*') return 0;
return 1;
}
void dfs(int x, int y) {
vis[x][y] = 1;
for(int i = 0; i < 8; i++) {
int nx = x + dx[i], ny = y + dy[i];
if(check(nx, ny)) {
vis[nx][ny] = 1;
dfs(nx, ny);
}
}
}
int main() {
// freopen("test.in", "r", stdin);
while(scanf("%d", &m) && m) {
int ans = 0;
scanf("%d", &n);
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= m; i++) cin >> mp[i] + 1;
for(int i = 1; i <= m; i++) {
for(int j = 1; j <= n; j++) {
if(mp[i][j] == '@' && !vis[i][j]) dfs(i, j), ans++;
}
}
printf("%d\n", ans);
}
return 0;
}