PAT(Advance) 1091. Acute Stroke (30)

题意是在实难懂,其实就是就是包含上下左右前后的一块连通区域内的1的数量大于threshold
DFS和BFS都可以,网上说DFS递归的话可能会爆栈,要写一个非递归的形式
我是用的BFS

#include 
#include
#define ROWS 1286
#define COLS 128
#define SLI 60

int map[SLI][ROWS][COLS];
bool visited[SLI][ROWS][COLS];
int col,row,slice,threshold;
struct graph{
    int x,y,z;
};
using namespace std;
queue<struct graph> q;
int BFS(){
    int sum = 0;
    struct graph node;
    for(int i = 0;ifor(int j = 0;jfor(int k = 0;kif(!visited[i][j][k]&&map[i][j][k]){
                    visited[i][j][k] = true;
                    node.x = j;
                    node.y = k;
                    node.z = i;
                    int x,y,z;
                    q.push(node);
                    int value = 0;
                    while(q.size()){
                        node = q.front();
                        q.pop();
                        x = node.x;
                        y = node.y;
                        z = node.z;

                        if(x&&!visited[z][x-1][y]&&map[z][x-1][y])
                        {
                            node.x = x-1;
                            node.y = y;
                            node.z = z;
                            visited[z][x-1][y] = true;
                            q.push(node);
                        }
                        if(y&&!visited[z][x][y-1]&&map[z][x][y-1]){
                            node.x = x;
                            node.y = y-1;
                            node.z = z;
                            visited[z][x][y-1] = true;
                            q.push(node);
                        }
                        if(x1&&!visited[z][x+1][y]&&map[z][x+1][y]){
                            node.x = x+1;
                            node.y = y;
                            node.z = z;
                            visited[z][x+1][y] = true;
                            q.push(node);
                        }
                        if(y1&&!visited[z][x][y+1]&&map[z][x][y+1]){
                            node.x = x;
                            node.y = y+1;
                            node.z = z;
                            visited[z][x][y+1] = true;
                            q.push(node);
                        }
                        if(z&&!visited[z-1][x][y]&&map[z-1][x][y]){
                            node.x = x;
                            node.y = y;
                            node.z = z-1;
                            visited[z-1][x][y] = true;
                            q.push(node);
                        }
                        if(z1&&!visited[z+1][x][y]&&map[z+1][x][y]){
                            node.x = x;
                            node.y = y;
                            node.z = z+1;
                            visited[z+1][x][y] = true;
                            q.push(node);
                        }
                        value++;
                    }
                   if(value>=threshold)
                       sum += value;
                }
    return sum;
}

int main(){
    cin >> row >> col >> slice >> threshold;
    for(int i = 0;ifor(int j = 0;jfor(int k = 0;kcin >> map[i][j][k];
                visited[i][j][k] = false;
            }
    cout << BFS() << endl;
    return 0;
}

你可能感兴趣的:(Pat,pat)