Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

怒水一发

Posted by KatrineYang at 2016-08-27 10:01:16 on Problem 2386
```#include <iostream>
#include <stdio.h>
using namespace std;

int N, M;
bool isLake[120][120] = {0};
bool used[120][120] = {0};

bool inRange(int x, int y){
return x >= 0 && x < N && y >= 0 && y < M;
}

int dir[8][2] = {{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};

void dfs(int x, int y){
used[x][y] = 1;
for(int i = 0; i < 8; i++){
int x_ = x+dir[i][0], y_ = y+dir[i][1];
if(inRange(x_,y_) && isLake[x_][y_] && !used[x_][y_]){
dfs(x_,y_);
}
}
}

int main() {
scanf("%d%d", &N, &M);
for(int i = 0; i < N; i++){
char data[120];
scanf("%s", data);
for(int j = 0; j < M; j++){
if(data[j] == 'W') isLake[i][j] = 1;
}
}
int cnt = 0;
for(int i = 0; i < N; i++){
for(int j = 0; j < M; j++){
if(!isLake[i][j] || used[i][j]) continue;
cnt++;
dfs(i, j);
}
}
printf("%d\n", cnt);
return 0;
}
```

Followed by: