UVa 12171 (离散化 floodfill) Sculpture
题意:
三维空间中有n个长方体组成的雕塑,求表面积和体积。
分析:
我们可以在最外边加一圈“空气”,然后求空气的连通块的体积,最后用总体积减去即是雕塑的体积。
还有一个很“严重”的问题就是5003所占的空间太大,因此需要离散化。而在计算体积和表面积的时候要用原坐标。
离散化以后的坐标分别保存在xs、ys、zs,坐标为(x, y, z)的格子代表([xs[x], ys[y], zs[z]) ~ (xs[x+1], ys[y+1], zs[z+1]) 这一个小长方体。
这个题的难度对我来说属于大概思路比较明白,但是很多代码细节处理不好那种。
把节点和相关的函数封装在一个结构体里面是个狠不错的技巧,使编码思路清晰,代码可读性也很好。
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
using namespace std; const int maxn = + ;
const int maxc = + ; int n, x0[maxn], y0[maxn], z0[maxn], x1[maxn], y1[maxn], z1[maxn]; int nx, ny, nz;
int xs[maxn*], ys[maxn*], zs[maxn*]; const int dx[] = {,-,,,,};
const int dy[] = {,,,-,,};
const int dz[] = {,,,,,-};
int color[maxn*][maxn*][maxn*]; struct Cell
{
int x, y, z;
Cell(int x=, int y=, int z=):x(x), y(y), z(z) {}
bool valid() const { return x >= && x < nx- && y >= && y < ny- && z >= && z < nz-;}
bool solid() const { return color[x][y][z] == ; }
bool getVis() const { return color[x][y][z] == ; }
void setVis() const { color[x][y][z] = ; }
Cell neighbor(int dir) const
{ return Cell(x+dx[dir], y+dy[dir], z+dz[dir]); }
int volume()
{ return (xs[x+]-xs[x]) * (ys[y+]-ys[y]) * (zs[z+]-zs[z]); }
int area(int dir)
{
if(dx[dir]) return (ys[y+]-ys[y]) * (zs[z+]-zs[z]);
if(dy[dir]) return (xs[x+]-xs[x]) * (zs[z+]-zs[z]);
return (xs[x+]-xs[x]) * (ys[y+]-ys[y]);
}
}; void discrectize(int* x, int& n)
{
sort(x, x + n);
n = unique(x, x + n) - x;
} int ID(int* x, int n, int x0)
{
return lower_bound(x, x + n, x0) - x;
} void floodfill(int& v, int& s)
{
v = s = ;
Cell c;
c.setVis();
queue<Cell> q;
q.push(c);
while(!q.empty())
{
Cell c = q.front(); q.pop();
v += c.volume();
for(int i = ; i < ; ++i)
{
Cell c2 = c.neighbor(i);
if(!c2.valid()) continue;
if(c2.solid()) s += c.area(i);
else if(!c2.getVis())
{
c2.setVis();
q.push(c2);
}
}
}
v = maxc*maxc*maxc - v;
} int main()
{
//freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while(T--)
{
memset(color, , sizeof(color));
nx = ny = nz = ;
xs[] = ys[] = zs[] = ;
xs[] = ys[] = zs[] = maxc;
scanf("%d", &n);
for(int i = ; i < n; ++i)
{
scanf("%d%d%d%d%d%d", &x0[i], &y0[i], &z0[i], &x1[i], &y1[i], &z1[i]);
x1[i] += x0[i]; y1[i] += y0[i]; z1[i] += z0[i];
xs[nx++] = x0[i]; xs[nx++] = x1[i];
ys[ny++] = y0[i]; ys[ny++] = y1[i];
zs[nz++] = z0[i]; zs[nz++] = z1[i];
}
discrectize(xs, nx);
discrectize(ys, ny);
discrectize(zs, nz); for(int i = ; i < n; ++i)
{
int X1 = ID(xs, nx, x0[i]), X2 = ID(xs, nx, x1[i]);
int Y1 = ID(ys, ny, y0[i]), Y2 = ID(ys, ny, y1[i]);
int Z1 = ID(zs, nz, z0[i]), Z2 = ID(zs, nz, z1[i]);
for(int X = X1; X < X2; X++)
for(int Y = Y1; Y < Y2; ++Y)
for(int Z = Z1; Z < Z2; ++Z)
color[X][Y][Z] = ;
} int v, s;
floodfill(v, s);
printf("%d %d\n", s, v);
} return ;
}
代码君
UVa 12171 (离散化 floodfill) Sculpture的更多相关文章
- uva 12171 hdu 1771 Sculpture
//这题从十一点开始写了四十分钟 然后查错一小时+ 要吐了 这题题意是给很多矩形的左下角(x,y,z最小的那个角)和三边的长(不是x,y,z最大的那个角T-T),为组成图形的面积与表面积(包在内部的之 ...
- Uva 12171 Sculpture - 离散化 + floodfill
题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- hdu 2771(uva 12171) Sculpture bfs+离散化
题意: 给出一些边平行于坐标轴的长方体,这些长方体可能相交.也可能相互嵌套.这些长方体形成了一个雕塑,求这个雕塑的整体积和表面积. 题解: 最easy想到直接进行bfs或者dfs统计,但此题的麻烦之处 ...
- UVa Sculpture(离散化 floodfill)
题意: 给定n个立方体的一个顶点坐标和3边长度, 问这些立方体组成的雕塑的表面积和体积, 坐标都是整数,n最大为50, 最大为500, 边长最大也是500. 分析: 继UVa221后又一道离散 ...
- UVA 12171 (hdu 2771)sculptrue(离散化)
以前对离散化的理解不够,所以把端点和区间区分来考虑但是做完这题以后有了新的认识: 先来看一个问题:给你以下的网格,你需要多少空间去存储红点区间的信息呢? 只需要图上所示的1,2,3,4个点就足够表示红 ...
- UVA 12171 Sculpture
https://vjudge.net/problem/UVA-12171 题目 某人设计雕塑,用的是很扯的方法:把一堆长方体拼起来.给出长方体的坐标和长宽高,求外表面积.因为要将这雕塑进行酸洗,需要知 ...
- UVA12171-Sculpture(离散化+floodfill)
Problem UVA12171-Sculpture Accept: 196 Submit: 1152 Time Limit: 3000 mSec Problem Description Imagi ...
- 【紫书】Urban Elevations UVA - 221 离散化
题意:给你俯视图,要求依次输出正视图中可以看到的建筑物 题解:任意相邻的x间属性相同,所以离散化. 坑:unique只能对数组用.下标易错 list不能找某元素的next.用了个很麻烦的处理 数组: ...
- UVa 12171 题解
英文题面不怎么友好,大家还是自行通过紫书了解题面吧... 解题思路: 1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题. 2. 由于我们要求的总体积包括内空部分的体积,我们可以 ...
随机推荐
- AVL树的python实现
AVL树是带有平衡条件的二叉查找树,一般要求每个节点的左子树和右子树的高度最多差1(空树的高度定义为-1). 在高度为h的AVL树中,最少的节点数S(h)由S(h)=S(h-1)+S(h-2)+1得出 ...
- wpf 实现全屏与取消全屏
/// <summary> /// 全屏 /// </summary> public void ToFullscreen() { //存储窗体信息 m_WindowState ...
- WPF常用数据绑定控件集合
1.怎么用ListView控件把XML中的数据在界面上显示出来? <?xml version="1.0" encoding="utf-8" ?> & ...
- 【BZOJ 1085】 [SCOI2005]骑士精神
Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...
- Homework3
1. 切换路径到想要保存的目录下,使用git init创建仓库: 2. 在仓库下创建文件Readme.txt,使用git add命令添加到暂存区和 git commit 命令提交到仓库: 3. 修改 ...
- 一个 XSD 实例
一个 XSD 实例 本节会为您演示如何编写一个 XML Schema.您还将学习到编写 schema 的不同方法. XML 文档 让我们看看这个名为 "shiporder.xml" ...
- spoj 237
好牛的题 哈哈 #include <cstdio> #include <algorithm> #define S(n) scanf("%d",&n ...
- Tiny6410 交叉编译helloworld程序
在工作目录下建立helloworld.c文件 #include <stdio.h> main() { printf("helloworld!\n"); } 保存关闭后. ...
- ValueError: Attempted relative import in non-package
执行:python deom/scripts/populate.py ValueError: Attempted relative import in non-package solve:python ...
- C++11新特性:Lambda函数(匿名函数)
声明:本文参考了Alex Allain的文章http://www.cprogramming.com/c++11/c++11-lambda-closures.html 加入了自己的理解,不是简单的翻译 ...