zoukankan      html  css  js  c++  java
  • uva 12171 hdu 1771 Sculpture

    //这题从十一点开始写了四十分钟 然后查错一小时+ 要吐了

    这题题意是给很多矩形的左下角(x,y,z最小的那个角)和三边的长(不是x,y,z最大的那个角T-T),为组成图形的面积与表面积(包在内部的之算体积不算表面积)

    解法:离散化+bfs,先把范围扩大(相当于在周围加上空气),然后bfs,遇到表面积直接加入,遇到非长方体的部分也直接加入,最后用总体积减去空气的体积,这样就可以把内部的体积计算进来而不计算其表面积。因为坐标范围比较大,要先离散化。

    //其实我对这题一直耿耿于怀,当年没进省队多少与这题有关

    //昨天没出题 明天周五课少 出两题

      1 #include<cstdio>
      2 #include<iostream>
      3 #include<cmath>
      4 #include<algorithm>
      5 #include<cstring>
      6 #include<cstdlib>
      7 #include<queue>
      8 #include<vector>
      9 #include<map>
     10 #include<stack>
     11 #include<string>
     12 
     13 using namespace std;
     14 
     15 const int u[6]={0,0,0,0,1,-1};
     16 const int v[6]={1,-1,0,0,0,0};
     17 const int w[6]={0,0,1,-1,0,0};
     18 
     19 struct rec{
     20     int x1,y1,z1,x2,y2,z2;
     21 };
     22 
     23 struct POINT{
     24     int x,y,z;
     25 };
     26 
     27 bool vis[103][103][103];
     28 bool loc[103][103][103];
     29 int n,T,ans1,ans2;
     30 int x[103],y[103],z[103];
     31 rec p[103];
     32 int numx,numy,numz;
     33 
     34 int IDX(int aim){
     35     return lower_bound(x,x+numx,aim)-x;
     36 }
     37 
     38 int IDY(int aim){
     39     return lower_bound(y,y+numy,aim)-y;
     40 }
     41 
     42 int IDZ(int aim){
     43     return lower_bound(z,z+numz,aim)-z;
     44 }
     45 
     46 int solve(int flag,int nowx,int nowy,int nowz){//计算表面积
     47     if (flag==0) return (x[nowx+1]-x[nowx])*(z[nowz+1]-z[nowz]);
     48     if (flag==1) return (x[nowx+1]-x[nowx])*(z[nowz+1]-z[nowz]);
     49     if (flag==2) return (x[nowx+1]-x[nowx])*(y[nowy+1]-y[nowy]);
     50     if (flag==3) return (x[nowx+1]-x[nowx])*(y[nowy+1]-y[nowy]);
     51     if (flag==4) return (z[nowz+1]-z[nowz])*(y[nowy+1]-y[nowy]);
     52     if (flag==5) return (z[nowz+1]-z[nowz])*(y[nowy+1]-y[nowy]);
     53     return -1;
     54 }
     55 
     56 void bfs(){
     57     queue<POINT> q;
     58     while (!q.empty()) q.pop();
     59     q.push((POINT){0,0,0});
     60     ans2=(x[1]-x[0])*(y[1]-y[0])*(z[1]-z[0]);
     61     vis[0][0][0]=1;
     62     while (!q.empty()){
     63             POINT now=q.front();
     64             q.pop();
     65             for (int i=0;i<6;i++){
     66                     int tx=now.x+u[i];
     67                     int ty=now.y+v[i];
     68                     int tz=now.z+w[i];
     69                     if (tx<0 || tx>=numx-1 || ty<0 || ty>=numy-1 || tz<0 || tz>=numz-1 || vis[tx][ty][tz]) continue;
     70                     if (loc[tx][ty][tz]){
     71                             ans1+=solve(i,now.x,now.y,now.z);
     72                     }
     73                     else{
     74                             ans2+=(x[tx+1]-x[tx])*(y[ty+1]-y[ty])*(z[tz+1]-z[tz]);
     75                             vis[tx][ty][tz]=1;
     76                             q.push((POINT){tx,ty,tz});
     77                     }
     78             }
     79     }
     80 }
     81 
     82 int main(){
     83     scanf("%d",&T);
     84     for (int cas=1;cas<=T;cas++){
     85             scanf("%d",&n);
     86             for (int i=0;i<n;i++){
     87                     scanf("%d%d%d%d%d%d",&p[i].x1,&p[i].y1,&p[i].z1,&p[i].x2,&p[i].y2,&p[i].z2);
     88                     p[i].x2+=p[i].x1;
     89                     p[i].y2+=p[i].y1;
     90                     p[i].z2+=p[i].z1;
     91                     x[2*i+1]=p[i].x1;
     92                     x[2*i+2]=p[i].x2;
     93                     y[2*i+1]=p[i].y1;
     94                     y[2*i+2]=p[i].y2;
     95                     z[2*i+1]=p[i].z1;
     96                     z[2*i+2]=p[i].z2;//先把坐算出来
     97             }
     98             x[0]=0;
     99             y[0]=0;
    100             z[0]=0;
    101             x[2*n+1]=1001;
    102             y[2*n+1]=1001;
    103             z[2*n+1]=1001;//拓展范围
    104             sort(x,x+2*n+2);
    105             sort(y,y+2*n+2);
    106             sort(z,z+2*n+2);
    107             numx=unique(x,x+2*n+2)-x;
    108             numy=unique(y,y+2*n+2)-y;
    109             numz=unique(z,z+2*n+2)-z;//离散化
    110             memset(loc,0,sizeof(loc));
    111             memset(vis,0,sizeof(vis));
    112             for (int now=0;now<n;now++){
    113                     for (int i=IDX(p[now].x1);i<IDX(p[now].x2);i++){
    114                             for (int j=IDY(p[now].y1);j<IDY(p[now].y2);j++){
    115                                     for (int k=IDZ(p[now].z1);k<IDZ(p[now].z2);k++){
    116                                         loc[i][j][k]=1;//记录矩形位置
    117                                     }
    118                             }
    119                     }
    120             }
    121             ans1=0;//^2
    122             ans2=0;//^3
    123             bfs();
    124             ans2=x[numx-1]*y[numy-1]*z[numz-1]-ans2;//总体积减去空气体积
    125             printf("%d %d
    ",ans1,ans2);
    126     }
    127     return 0;
    128 }
    129 /*
    130 1
    131 2
    132 1 2 3 3 4 5
    133 6 2 3 3 4 5
    134 
    135 1
    136 7
    137 1 1 1 5 5 1
    138 1 1 10 5 5 1
    139 1 1 2 1 4 8
    140 2 1 2 4 1 8
    141 5 2 2 1 4 8
    142 1 5 2 4 1 8
    143 3 3 4 1 1 1
    144 
    145 2
    146 2
    147 1 2 3 3 4 5
    148 6 2 3 3 4 5
    149 7
    150 1 1 1 5 5 1
    151 1 1 10 5 5 1
    152 1 1 2 1 4 8
    153 2 1 2 4 1 8
    154 5 2 2 1 4 8
    155 1 5 2 4 1 8
    156 3 3 4 1 1 1
    157 */
  • 相关阅读:
    程序员的最大挑战
    12个有效的提高编程技能的方法
    风雨20年:我所积累的20条编程经验
    java的继承机制
    Java中获得程序当前路径的4中方法
    关于String的hashCode
    使用三目运算符时注意的一个问题
    linux查找符合条件的文件并删除
    Tomcat性能优化及JVM内存工作原理
    Linux(Centos)下调整分区大小(以home和根分区为例)
  • 原文地址:https://www.cnblogs.com/baby-mouse/p/4452298.html
Copyright © 2011-2022 走看看