盒子里的气球及USACO1.2章部分题解

转载了枚举总结,又回顾了USACO1.2章,感觉自己还是要多看点东西。

盒子里的气球

Problem 1515 Balloons in a Box

Accept: 265    Submit: 1430
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

You must write a program that simulates placing spherical balloons into a rectangular box.

The simulation scenario is as follows. Imagine that you are given a rectangular box and a set of points inside the box. Each point represents a position where you might place a balloon. To place a balloon at a point, center it at the point and inflate the balloon until it touches a side of the box or a previously placed balloon. You may use the points in any order you like, and need not use every point. Your objective is to place balloons in the box in an order that maximizes the total volume occupied by the balloons.

You are required to calculate the volume within the box that is not enclosed by the balloons.

All integer will be in [-1000, 1000].

 Input

The input consists of several test cases. The first line of each test case contains a single integer n that indicates the number of points in the set (n ≤ 6). The second line contains three integers that represent the (x, y, z) integer coordinates of a corner of the box, and the third line contains the (x, y, z) integer coordinates of the opposite corner of the box. The next n lines of the test case contain three integers each, representing the (x, y, z) coordinates of the points in the set. The box has non-zero length in each dimension and its sides are parallel to the coordinate axes.

 Output

For each test case print one line which indicates the volume of the box not occupied by balloons. Round the volume to the nearest integer.

 Sample Input

2 0 0 0 10 10 10 3 3 3 7 7 7

 Sample Output

774

 Source

FZU 2007 ICPC Qualification Round I
 
思路:来自刘汝佳的黑书,最多有6个气球,因此最后最多只有6!=720次放气球的顺序,因此枚举出每一中顺序,比较出最好的一种即可(虽然知道思路,但就是写不出来,无奈下查了代码)
 
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

double x[15],y[15],z[15],r[15];
double X1,Y1,Z1,X2,Y2,Z2,maxV;
int n,sel[15];
bool vis[15];
const double pi=acos(-1.0);


double getside(int i)
{
    double dis1,dis2,dis3;
    dis1=min(fabs(x[i]-X1),fabs(x[i]-X2));
    dis2=min(fabs(y[i]-Y1),fabs(y[i]-Y2));
    dis3=min(fabs(z[i]-Z1),fabs(z[i]-Z2));
    return min(dis1,min(dis2,dis3));
}

double distances(int a,int b)
{
    return sqrt((x[a]-x[b])*(x[a]-x[b])+
                (y[a]-y[b])*(y[a]-y[b])+
                (z[a]-z[b])*(z[a]-z[b]));
}

void getV()
{
    double a,v=0;
    for(int i=0;i<n;i++)
    {
        a=getside(sel[i]);
        for(int j=0;j<i;j++)
            a=min(a,distances(sel[i],sel[j])-r[sel[j]]);
        r[sel[i]]=(a<0)?0:a;
        if(a<=0) continue;
        v+=(4.0/3*pi*r[sel[i]]*r[sel[i]]*r[sel[i]]);
    }
    maxV=max(maxV,v);
}

void dfs(int cnt)
{
    if(cnt==n)
        getV();
    else
    {
        for(int i=0;i<n;i++)
        {
            if(vis[i]) continue;
            sel[cnt]=i;
            vis[i]=1;
            dfs(cnt+1);
            vis[i]=0;
        }
    }
}

int main()
{
    int te=1;
    while(~scanf("%d",&n)&&n)
    {
        scanf("%lf%lf%lf",&X1,&Y1,&Z1);
        scanf("%lf%lf%lf",&X2,&Y2,&Z2);
        for(int i=0;i<n;i++)
            scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
        maxV=0;
        memset(vis,0,sizeof(vis));
        dfs(0);
        printf("Box %d: %.0lf\n\n",te++,((fabs((X1-X2)*(Y1-Y2)*(Z1-Z2)))-maxV));

    }
    return 0;
}
 

USACO 1.2--Palindromic Squares

有关二十进制和回文数的枚举题,长长二十进制的知识。。。
 
#include <stdio.h>
 
int B;
int x[20],l,y[20],L;
 
void Change(int n)
{
   int m;
   m=n, l=0; while(m) x[++l]=m%B, m/=B;
   m=n*n, L=0; while(m) y[++L]=m%B, m/=B;
}
 
int pd()
{
   int i,j;
   for(i=1,j=L;i<=j;++i,--j)
      if(y[i]!=y[j]) return 0;
   return 1;
}
 
int main()
{
   freopen("palsquare.in","r",stdin);
   freopen("palsquare.out","w",stdout);
 
   int i,j;
   char c[20]={ '0','1','2','3','4','5','6','7','8','9',
                'A','B','C','D','E','F','G','H','I','J' };
 
   scanf("%d",&B);
 
   for(i=1;i<=300;++i)
   {
      Change(i);
      if(pd())
      {
         for(j=l;j>=1;--j) printf("%c",c[x[j]]); printf(" ");
         for(j=L;j>=1;--j) printf("%c",c[y[j]]); printf("\n");
      }
   }
 
   return 0;
}
 

USACO 1.2--namenum

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char a[26] = {'2','2','2','3','3','3','4','4','4','5','5','5', '6','6','6','7','0','7','7','8','8','8','9','9','9','0'};
char s[13];
char name[13];
int main()
{
    freopen ("namenum.in", "r",stdin);
    freopen ("namenum.out", "w",stdout);
    int i;
    int flag = 1;
    int len;
    scanf ("%s", &s);
    len = strlen(s);
    freopen ("dict.txt", "r",stdin);
    while(scanf("%s", name) != EOF)
    {
        if(strlen(name) == len)
        {
            int flg = 1;
            for(i = 0; i < len && flg; i++)
            {
                if(a[name[i]-'A'] != s[i])
                {
                    flg = 0;
                }
            }
            if(flg)
            {
                printf ("%s\n", name);
                flag = 0;
            }
        }
    }

    if(flag)
    {
        printf ( "NONE\n");
    }
    return 0;
}

 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值