• + 0 comments

    O(n^4)

    vector<int> specialSubCubes(int n, const vector<vector<vector<int>>>& cube) {
        vector<int> result;
        vector<vector<vector<vector<int>>>> M = {{{{}}}};
        M.push_back(cube);
        int counter;
        for (int k=2; k <= n; k++) {
            counter = 0;
            M.emplace_back(vector<vector<vector<int>>>(n-k+2, vector<vector<int>>(n-k+2, vector<int>(n-k+2))));
            for (int x=1; x <= n-k+1; x++) {
                for (int y=1; y <= n-k+1; y++) {
                    for (int z=1; z <= n-k+1; z++) {
                        auto v = {M[k-1][x][y][z], M[k-1][x][y][z+1], M[k-1][x][y+1][z], M[k-1][x][y+1][z+1],
                        M[k-1][x+1][y][z], M[k-1][x+1][y][z+1], M[k-1][x+1][y+1][z], M[k-1][x+1][y+1][z+1]};
                        M[k][x][y][z] = *max_element(v.begin(), v.end());
                        if (M[k][x][y][z] == k) counter++;
                    }
                }
            }
            result.push_back(counter);
        }
        return result;
    }
    
    int main()
    {
        int q, n, temp;
        cin >> q;
        for (int i=1; i <= q; i++) {
            cin >> n;
            int counter = 0;
            vector<vector<vector<int>>> cube(n+1, vector<vector<int>>(n+1, vector<int>(n+1)));
            for (int x=1; x <= n; x++) {
                for (int y=1; y <= n; y++) {
                    for (int z=1; z <= n; z++) {
                        cin >> temp;
                        cube[x][y][z] = temp;
                        if (temp == 1) counter++;
                    }
                }
            }
            cout << counter << ' ';
            for (int x : specialSubCubes(n, cube)) cout << x << ' ';
            cout << '\n';
        }
    }