---
# System prepended metadata

title: k650. 尋找蜂后 (Queen bee)
tags: [BFS, toi, Graph]

---

# k650. 尋找蜂后 (Queen bee)
##### link: https://zerojudge.tw/ShowProblem?problemid=k650
###### tags: `toi` `Graph` `BFS`
* 這題是一個**三維陣列**的 BFS 尋訪題，但依然不脫離 BFS 的基本框架 ( 程式中的 BFS 函式和一般的 BFS 幾乎無差別 )
* 這題比較 tricky 的點是陣列的**長、寬、高順序不能搞錯** => 第一層陣列是 h；第二層是 l；第三層是 w。 ( 我因為這樣白白吃了 WA )


```C++=
#include <iostream>
#include <vector>
#include <string>
#include <queue>

using namespace std;

string s;
int l,w,h;
int ans = 0;
char hive[306][306][306] = {};
bool vis[306][306][306] = {};
int d_row[6] = {0,0,0,0,1,-1};
int d_w[6] = {0,0,1,-1,0,0};
int d_l[6] = {1,-1,0,0,0,0};


void BFS(vector<int> h_w_l){
    queue<vector<int>> q;
    int counts = 1;
    q.push(h_w_l);
    while (!q.empty()){
        int row = q.front()[0];
        int w1 = q.front()[1];
        int l1 = q.front()[2];
        q.pop();
        for (int i=0;i<6;i++){
            int nh = row+d_row[i];
            int nw =  w1+d_w[i];
            int nl = l1+d_l[i];
            if ((0<=nh && nh<h) && (0<=nw && nw<w) && (0<=nl && nl<l) ){
                if (hive[nh][nl][nw] == '0' && !vis[nh][nl][nw]){
                    //if (r_c.first == 0 && r_c.second == 0) cout<<row+d_row[i]<<" "<<col+d_col[i]<<"\n";
                    q.push({nh,nw,nl});
                    counts++;
                    vis[nh][nl][nw] = true;
  
                        }  }
            
        }
    }
    ans = max(ans,counts);
}

int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>l>>w>>h;
    for (int i=0;i<h;i++){
        for (int j=0;j<l;j++){
            for (int k=0;k<w;k++){
                cin>>hive[i][j][k];
            }
        }
    }
    for (int i=0;i<h;i++){
        for (int j=0;j<l;j++){
            for (int k=0;k<w;k++){
                if (hive[i][j][k]=='0' && !vis[i][j][k]){
                vis[i][j][k] = true;
                BFS({i,k,j});
            }
            }
            
        }
    }
    cout<<ans;

    return 0;
}
```