1091 Acute Stroke (30 分) bfs,图的边界判断清楚,不然会出现段错误

temp = bfs(x, y, z);
 max1+=temp;
#include<iostream>
#include<queue>
using namespace std;
struct point {
    
    
    int x, y, z;
}node;
bool inq[1290][130][61] = {
    
     false };
int X[6] = {
    
     1,-1,0,0,0,0 };
int Y[6] = {
    
     0,0,1,-1,0,0 };
int Z[6] = {
    
     0,0,0,0,1,-1 };
int n, m, l, t, max1 = 0, v[1290][130][61];
bool judge(int x, int y, int z)
{
    
    
    if (x >= n || y >= m || z >= l || y < 0 || x < 0 || z < 0) return false;    //忘了xyz要大于0,段错误,虽然在vs中能对
    if (inq[x][y][z] == true || v[x][y][z] == 0) return false;
    return true;
}
int bfs(int x, int y, int z)
{
    
    
    int count = 0;
    queue<point> q;
    node.x = x; node.y = y; node.z = z;
    q.push(node);
    inq[x][y][z] = true;
    while (!q.empty()) {
    
    
        point temp = q.front();
        count++;
        q.pop();
        for (int i = 0; i < 6; i++) {
    
    
            /*  temp.x=x+X[i];
              temp.y=y+Y[i];
              temp.z=z+Z[i];*/                               //当处理别的点之后,x还是不会变,这样是不对的
            node.x = temp.x + X[i];
            node.y = temp.y + Y[i];
            node.z = temp.z + Z[i];
            if (judge(node.x, node.y, node.z)) {
    
    
                q.push(node);           //入队等待访问
                inq[node.x][node.y][node.z] = true;
            }
        }
    }
    if (count >= t)
        return count;
    else return 0;
}
int main()
{
    
    
    int temp = 0;
    cin >> n >> m >> l >> t;
    for (int z = 0; z < l; z++) {
    
    
        for (int x = 0; x < n; x++) {
    
    
            for (int y = 0; y < m; y++) {
    
    
                cin >> v[x][y][z];
            }
        }
    }
    for (int z = 0; z < l; z++) {
    
    
        for (int x = 0; x < n; x++) {
    
    
            for (int y = 0; y < m; y++) {
    
    
                if (judge(x, y, z)) {
    
    
                    temp = bfs(x, y, z);
                    max1+=temp;
                }
            }
        }
    }
    cout << max1;
}

猜你喜欢

转载自blog.csdn.net/qq_42835526/article/details/113622181