模拟炸弹人(bfs)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e3 + 10;
int dx[8] = {0,0,-1,1,0,0,-2,2};
int dy[8] = {1,-1,0,0,2,-2,0,0};
int n,m,p;
char a[maxn][maxn] = {0};
queue<pair<int,int > > que;
int vis[maxn][maxn] = {0};
int ans;
void bfs(int bx,int by){
    que.push(make_pair(bx,by));
    vis[bx][by] = 1;
    while(!que.empty()){
        int x,y;
        x = que.front().first;
        y = que.front().second;
        que.pop();
        for(int i = 0; i < 4*p; i++){
            int tx = x + dx[i];
            int ty = y + dy[i];
            if(tx >= 0 && tx < n && ty >= 0 && ty < m && !vis[tx][ty] && a[tx][ty] == '*'){
                que.push(make_pair(tx,ty));
                vis[tx][ty] = 1;
            }
        }

    }
}
int main(){
    ios::sync_with_stdio(0);
    cin >> n >> m >> p;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++)
            cin >> a[i][j];
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < m; j++){
            if(!vis[i][j] && a[i][j] == '*'){
                bfs(i,j);
                ans++;
            }
        }
    }
    cout << ans;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/xcfxcf/p/12629383.html