团体程序设计天梯赛(L3-004 肿瘤诊断 (30 分))

题目:

思路分析:

一道简单的图的连通块搜索题目

推荐bfs dfs容易段错误

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */
int d[6][3]{
   
   {0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
int a[61][1300][130];
int vis[61][1300][130];
int di[6]={0,0,0,0,-1,1};
int dj[6]={0,0,-1,1,0,0};
int dk[6]={-1,1,0,0,0,0};
int m,n,t,l;
struct node{
    int x,y,z;
}p;
queue<node>q;

int ch(int x,int y,int z){
    if(x<1||x>l||y<1||y>n||z<1||z>m){
        return 0;
    }
    if(vis[x][y][z]) return 0;
    if(a[x][y][z]==0) return 0;
    return 1;
}
int ans;
void bfs(int x,int y,int z){
    while (!q.empty()) {
        q.pop();
    }
    q.push({x,y,z});
    vis[x][y][z]=1;
    int cnt=1;
    while (!q.empty()) {
        node now=q.front();
        q.pop();
        x=now.x;
        y=now.y;
        z=now.z;
        for(int i=0;i<6;i++){
            int x1=x+d[i][0];
            int y1=y+d[i][1];
            int z1=z+d[i][2];
//            int x1=x+di[i];
//            int y1=y+dj[i];
//            int z1=z+dk[i];
            if(ch(x1,y1,z1)){
                q.push({x1,y1,z1});
                vis[x1][y1][z1]=1;
                cnt++;
            }
        }
    }
//    cout<<cnt<<endl;
    if(cnt>=t) ans+=cnt;
}
int main(){
    mms(vis,0);
    cin>>n>>m>>l>>t;
    for(int i=1;i<=l;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=m;k++){
                cin>>a[i][j][k];
            }
        }
    }
    for(int i=1;i<=l;i++){
        for(int j=1;j<=n;j++){
            for(int k=1;k<=m;k++){
                if(ch(i,j,k)){
                    bfs(i,j,k);
                }
            }
        }
    }
    cout<<ans<<endl;
}



猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121217544