hiho第151周 Building in Sandbox floodfill

来源:互联网 发布:侯振挺落选院士 知乎 编辑:程序博客网 时间:2024/06/05 10:07

题意:题目背景是<<我的世界>>,堆方块造房子,堆的规则是:新堆的方块必须和已有方块有重合面,而且不能往封闭空间里堆。 在三维空间中,给定一个堆的序列,判断符不符合规则。
数据范围: 1n105,1x,y,z100


思路:如果正向考虑,判断方块是否放在封闭空间很难实现。那么就逆向考虑,删除某个方块,然后用floodfill算法(其实可以想当然的用bfs实现,我就是自己实现的这个算法)。从某个点开始,格子看做障碍物,bfs找到所有可以到达的空格。如果某个格子周围没有先前被访问(bfs标记)过的点,说明一定在封闭空间中。

AC代码

#include <cstdio>#include <cmath>#include <cctype>#include <bitset>#include <algorithm>#include <cstring>#include <utility>#include <string>#include <iostream>#include <map>#include <set>#include <vector>#include <queue>#include <stack>using namespace std;#pragma comment(linker, "/STACK:1024000000,1024000000") #define eps 1e-10#define inf 0x3f3f3f3f#define pii pair<int, int> typedef long long LL;const double PI = acos(-1.0);const int maxn = 100 + 5;int Fre[maxn][maxn][maxn], pos[maxn][maxn][maxn];struct node{    int x, y, z;    node(){}    node(int a, int b, int c):x(a),y(b),z(c){    }}cube[100005];int lx, ly, lz, rx, ry, rz;const int dsize = 6;const int dx[] = {1,-1,0,0,0,0};const int dy[] = {0,0,1,-1,0,0};const int dz[] = {0,0,0,0,1,-1};void init() {    memset(pos, 0, sizeof(pos));    memset(Fre, 0, sizeof(Fre));    lx = ly = lz = inf;    rx = ry = rz = 0;}void getBorder(int x, int y, int z) {    rx = max(rx, x);    ry = max(ry, y);    rz = max(rz, z);    lx = min(lx, x);    ly = min(ly, y);    lz = min(lz, z);}bool isIn(int x, int y, int z) {    if(x < lx || x > rx || y < ly || y > ry || z < lz || z > rz) return false;    return true;}void floodfill(int x, int y, int z) {    queue<node>Q;    Fre[x][y][z] = 1;    Q.push(node(x, y, z));    while(!Q.empty()) {        node now = Q.front(); Q.pop();        x = now.x, y = now.y, z = now.z;        for(int i = 0; i < dsize; ++i) {            int px = x + dx[i], py = y + dy[i], pz = z + dz[i];            if(!isIn(px, py, pz) || pos[px][py][pz] || Fre[px][py][pz]) continue;            Fre[px][py][pz] = 1;            Q.push(node(px, py, pz));        }    }}bool adjacent(int x, int y, int z) {    int Free = 0, adj = 0;    for(int i = 0; i < dsize; ++i) {        int px = x + dx[i], py = y + dy[i], pz = z + dz[i];        if(!isIn(px, py, pz) && pz != 0) continue;        if((pos[px][py][pz] && !Fre[px][py][pz]) || pz == 0) adj = 1;        if(Fre[px][py][pz]) Free = 1;    }    return adj && Free;}bool Place(int n) {    for(int i = n-1; i >= 0; --i) {        int x = cube[i].x, y = cube[i].y, z = cube[i].z;        if(!adjacent(x, y, z)) return false;        floodfill(x, y, z);    }    return true;}void in(int &a) {    char ch;    while((ch=getchar()) < '0' || ch > '9');    for(a = 0; ch >= '0' && ch <= '9'; ch = getchar())        a = a*10 + ch - '0';}int main() {    int T, n;    scanf("%d", &T);    while(T--) {        init();        scanf("%d", &n);        int x, y, z;        for(int i = 0; i < n; ++i) {            in(x); in(y); in(z);            getBorder(x, y, z);            cube[i] = node(x, y, z);            pos[x][y][z] = 1;        }        rx++, ry++, rz++;        lx--, ly--, lz = 1;        floodfill(rx, ry, rz);        if(Place(n)) printf("Yes\n");        else printf("No\n");    }    return 0;}

如有不当之处欢迎指出!

原创粉丝点击