UVa 12171 题解

英文题面不怎么友好,大家还是自行通过紫书了解题面吧。。。
解题思路:
1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题。
2. 由于我们要求的总体积包括内空部分的体积,我们可以用flood_fill来解决。
注意事项:(几个细节问题)
1. unique函数的正确使用姿势:如果是从一开始存数,注意最后要多减去1.
2. 染色时每个三维坐标上的点代表一个长方形,所以右端点不能染色。
3. 可以用结构体封装一下各个操作。
AC代码:
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cstdio>
#include <cstdlib> using namespace std; const int dx[] = {, -, , , , };
const int dy[] = {, , , -, , };
const int dz[] = {, , , , , -}; int T, n, v, s;
int x1[], y1[], z1[], x0[], y0[], z0[]; int nx = , ny = , nz= ;
int lx[], ly[], lz[];
int color[][][]; struct cell{
int x, y, z; cell(int x = , int y = , int z = ): x(x), y(y), z(z) {} void setvis() const { color[x][y][z] = ; } bool getvis() const { return color[x][y][z] == ; } bool issolid() const { return color[x][y][z] == ; } bool invalid() const {
if (x <= || y <= || z <= || x >= nx || y >= ny || z >= nz ) return true;
return false;
} int area(int dir) const {
if (dx[dir] != ) return (ly[y + ] - ly[y]) * (lz[z + ] - lz[z]);
else if (dy[dir] != ) return (lx[x + ] - lx[x]) * (lz[z + ] - lz[z]);
return (lx[x + ] - lx[x]) * (ly[y + ] - ly[y]);
} int volume() const { return (lx[x + ] - lx[x]) * (ly[y + ] - ly[y]) * (lz[z + ] - lz[z]); } }; int read()
{
int x = ;
int k = ;
char c = getchar(); while (c > '' || c < '')
if (c == '-') k = -, c = getchar();
else c = getchar();
while (c >= '' && c <= '')
x = x * + c - '',
c = getchar(); return k * x;
} void discretization(int* x, int& l)
{
sort(x + , x + l + );
l = unique(x + , x + l + ) - x - ;
} int get_ID(int *x, int l, int k)
{
return lower_bound(x + , x + l + , k) - x;
} void flood_fill()
{
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.x + dx[i], c.y + dy[i], c.z + dz[i]);
if (c2.invalid()) continue;
if (c2.issolid()) s += c2.area(i);
else if (!c2.getvis())
{
c2.setvis();
q.push(c2);
}
}
}
v = * * - v;
} int main()
{
T = read();
while (T--)
{
v = , s = ;
memset(color, , sizeof(color));
n = read();
lx[] = ly[] = lz[] = ;
lx[] = ly[] = lz[] = ;
nx = ny = nz = ;
for (int i = ; i <= n; ++i)
x0[i] = read(),
y0[i] = read(),
z0[i] = read(),
x1[i] = read(),
y1[i] = read(),
z1[i] = read(),
lx[++nx] = x0[i],
lx[++nx] = x0[i] + x1[i],
ly[++ny] = y0[i],
ly[++ny] = y0[i] + y1[i],
lz[++nz] = z0[i],
lz[++nz] = z0[i] + z1[i]; discretization(lx, nx);
discretization(ly, ny);
discretization(lz, nz); for (int t = ; t <= n; ++t)
{
int sx = get_ID(lx, nx, x0[t]);
int ex = get_ID(lx, nx, x0[t] + x1[t]);
int sy = get_ID(ly, ny, y0[t]);
int ey = get_ID(ly, ny, y0[t] + y1[t]);
int sz = get_ID(lz, nz, z0[t]);
int ez = get_ID(lz, nz, z0[t] + z1[t]);
for (int i = sx; i < ex; ++i)
for (int j = sy; j < ey; ++j)
for (int l = sz; l < ez; ++l)
color[i][j][l] = ;
} flood_fill(); printf("%d %d\n", s, v);
}
}
UVa 12171 题解的更多相关文章
- Uva 12171 Sculpture - 离散化 + floodfill
题目连接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 12171 Sculpture
https://vjudge.net/problem/UVA-12171 题目 某人设计雕塑,用的是很扯的方法:把一堆长方体拼起来.给出长方体的坐标和长宽高,求外表面积.因为要将这雕塑进行酸洗,需要知 ...
- hdu 2771(uva 12171) Sculpture bfs+离散化
题意: 给出一些边平行于坐标轴的长方体,这些长方体可能相交.也可能相互嵌套.这些长方体形成了一个雕塑,求这个雕塑的整体积和表面积. 题解: 最easy想到直接进行bfs或者dfs统计,但此题的麻烦之处 ...
- UVA 10131题解
第一次写动态规划的代码,整了一天,终于AC. 题目: Question 1: Is Bigger Smarter? The Problem Some people think that the big ...
- 位运算基础(Uva 1590,Uva 509题解)
逻辑运算 规则 符号 与 只有1 and 1 = 1,其他均为0 & 或 只有0 or 0 = 0,其他均为1 | 非 也就是取反 ~ 异或 相异为1相同为0 ^ 同或 相同为1相异为0,c中 ...
- 【OI】计算分子量 Molar mass UVa 1586 题解
题目:(由于UVa注册不了,还是用vjudge) https://vjudge.net/problem/UVA-1586 详细说明放在了注释里面.原创. 破题点在于对于一个元素的组合(元素+个数),只 ...
- UVa 12171 (离散化 floodfill) Sculpture
题意: 三维空间中有n个长方体组成的雕塑,求表面积和体积. 分析: 我们可以在最外边加一圈“空气”,然后求空气的连通块的体积,最后用总体积减去即是雕塑的体积. 还有一个很“严重”的问题就是5003所占 ...
- uva 12171 hdu 1771 Sculpture
//这题从十一点开始写了四十分钟 然后查错一小时+ 要吐了 这题题意是给很多矩形的左下角(x,y,z最小的那个角)和三边的长(不是x,y,z最大的那个角T-T),为组成图形的面积与表面积(包在内部的之 ...
- UVA 12171 (hdu 2771)sculptrue(离散化)
以前对离散化的理解不够,所以把端点和区间区分来考虑但是做完这题以后有了新的认识: 先来看一个问题:给你以下的网格,你需要多少空间去存储红点区间的信息呢? 只需要图上所示的1,2,3,4个点就足够表示红 ...
随机推荐
- 理解SPI
SPI 全称为 Service Provider Interface,是一种服务发现机制.SPI 的本质是将接口实现类的全限定名配置在文件中,并由服务加载器读取配置文件,加载实现类.这样可以在运行时, ...
- VRTK3.3.0-002获取手柄事件
1.首先创建VRScripts空物体,用来存放脚本,在其下创建Right空物体并添加VRTK_ControllerEvents脚本 2.Right作为右手手柄,拖拽到[VRTK_SDKManager] ...
- 微服务监控神器Prometheus的安装部署
本文涉及:如何在k8s下搭建Prometheus+grafana的监控环境 基本概念 Prometheus提供了容器和云原生领域数据搜集.存储.处理.可视化和告警一套完整的解决方案,最初时是由Soun ...
- 调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错
调试接口,返回的json数据,我定义了一个类,用来序列化,其中有一个字段定义为string 然后序列化的时候报错 在需要解析的类型类上加上声明 eg:
- Java基础笔记(四)——命名规则、数据类型
标识符即Java程序中需要自定义的名称,如变量名.方法名.类名.包名.工程名等. 标识符的命名规则: 1.可由字母.数字.下划线(_)和美元符($)组成,不能以数字开头. 2.严格区分大小写. 3.不 ...
- idea 添加yuicompressor压缩js/css
打开idea 点击file->Settings 出现如下界面 argumets项填写 : -jar F:\yui\yuicompressor-2.4.8.jar $FilePath$ -o $F ...
- mac下配置php+mysql+nginx遇到的问题
1.mysql:没有useradd和groupadd命令,好在原来的/etc/passwd有了mysql,www用户,/etc/group下也有了mysql,www用户组 2.nginx:编译ngin ...
- namedJDBC查询
import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.spring ...
- 通过代码理解Asp.net4中的几种ClientIDMode设置.
以前我们可以通过ClientID在JavaScript脚本中服务器端控件.比如: document.getElementById("<%=控件.ClientID %>" ...
- springboot 学习笔记(三)
(三)用jar包启动springboot项目 1.首先需要在pom文件中添加依赖,spring-boot-starter-parent包含有打包的默认配置,如果要修改的话要可以进行重新定义,具体内容参 ...