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

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

Posted by Aimyhtixela at 2017-02-21 11:15:15 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:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator