Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:BFS解决~~0~16ms~~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: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator