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

## Re:BFS解决~~0~16ms~~

Posted by Aimyhtixela at 2017-02-21 11:15:17 on Problem 2386
In Reply To:BFS解决~~0~16ms~~ Posted by:Sinit at 2016-12-27 12:07:40
```#include <iostream>
using namespace std;

int N, M, ans = 0;
char field[105][105];

void dfs(int x, int y){
field[x][y] = '.';
for (int dx = -1; dx <= 1; ++dx){
for (int dy = -1; dy <= 1; ++dy){
int nx = x + dx, ny = y + dy;
if (1 <= nx && nx <= N && 1 <= ny && ny <= M && field[nx][ny] == 'W')
dfs(nx, ny);
}
}
return ;
}

void solve(){
for (int i = 1; i <= N; ++i){
for (int j = 1; j <= M; ++j){
if (field[i][j] == 'W')
++ans, dfs(i, j);
}
}
cout << ans << endl;
}

int main() {
ios_base::sync_with_stdio(0);
cin >> N >> M;
for (int i = 1; i <= N; ++i)
for (int j = 1; j <= M; ++j)
cin >> field[i][j];
solve();
return 0;
}
DFS解决~~0~16ms~~```

Followed by: