牛客多校第十场 1009 (hdu6699) Block Breaker bfs/模拟

题意:

紧密排列的方块因为摩擦力一个一个稳定地挤在一起,但当一个方块的四个邻居中,上下两个至少空缺一个,左右两个至少空缺一个,则这个方块也将掉落。

每次锤掉一个方块,求多少个方块受牵连落下。

题解:

可能掉落的方块总在刚刚掉落的方块上下左右四个方向出现,暴力bfs的话一个方块最多被访问4次,此复杂度可以接受。

#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
int xc[4]={1,-1,0,0};
int yc[4]={0,0,1,-1};
bool mapp[2005][2005];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,m,q;
        scanf("%d %d %d",&n,&m,&q);
//        memset(mapp,1,sizeof mapp);
        for(int i=0;i<=n+1;i++){
            for(int j=0;j<=m+1;j++){
                mapp[i][j]=1;
            }
        }
        queue<int> xque,yque;
        for(int i=1;i<=q;i++){
            int x,y;
            int count=0;
            scanf("%d %d",&x,&y);
            while(!xque.empty())xque.pop();
            while(!yque.empty())yque.pop();
            
//            printf("%d\n",mapp[x][y]);
            
            if(mapp[x][y]==1){
                mapp[x][y]=0;
                for(int j=0;j<4;j++){
                    xque.push(x+xc[j]);
                    yque.push(y+yc[j]);
                }
                count++;
            }
            
            while(!xque.empty()){
                x=xque.front();y=yque.front();
                xque.pop();yque.pop();
                if(mapp[x][y]==1 && (mapp[x+1][y]&&mapp[x-1][y])==0 && (mapp[x][y+1]&&mapp[x][y-1])==0 ){
                    mapp[x][y]=0;
                    for(int j=0;j<4;j++){
                        if(x+xc[j]>=1 && x+xc[j]<=n && y+yc[j]>=1 && y+yc[j]<=m && mapp[x+xc[j]][y+yc[j]]){
                            xque.push(x+xc[j]);
                            yque.push(y+yc[j]);
                        }
                    }
                    count++;
                }
            }
            
            printf("%d\n",count);
        }
    }
    
    
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/isakovsky/p/11409611.html