Problem E: Hotter Colder

Problem E: Hotter Colder

时间: 1ms        内存:128M

描述:

Problem E: Hotter Colder

The children's game Hotter Colder is played as follows. Player A leaves the room while player B hides an object somewhere in the room. Player A re-enters at position (0,0) and then visits various other positions about the room. When player A visits a new position, player B announces "Hotter" if this position is closer to the object than the previous position; player B announces "Colder" if it is farther and "Same" if it is the same distance.

Input consists of up to 50 lines, each containing an x,y coordinate pair followed by "Hotter", "Colder", or "Same". Each pair represents a position within the room, which may be assumed to be a square with opposite corners at (0,0) and (10,10). For each line of input print a line giving the total area of the region in which the object may have been placed, to 2 decimal places. If there is no such region, output 0.00.

输入:

输出:

示例输入:

10.0 10.0 Colder
10.0 0.0 Hotter
0.0 0.0 Colder
10.0 10.0 Hotter

示例输出:

50.00
37.50
12.50
0.00

提示:

参考答案(内存最优[760]):

#include <math.h>

linepoints(double x1, double y1, double x2, double y2,
           double *a, double *b, double *c) {
   *a = y2 - y1;
   *b = x1 - x2;
   *c = *a * x1 + *b * y1;
}

bi(double x1, double y1, double x2, double y2, double *a, double *b, double *c){
   *a = 2*(x2-x1);
   *b = 2*(y2-y1);
   *c = x2*x2 + y2*y2 - x1*x1 - y1*y1;
}

isct(double a, double b, double c, double aa, double bb, double cc,
     double *x, double *y){
   double det = a*bb - b*aa;
   if (fabs(det) < 1e-10) return 0;
   *x = (-b*cc + c*bb)/det;
   *y = (a*cc - c*aa)/det;
   return 1;
}

int nd, done[200];
int i,j,k;

struct pp {
   double a, b, c;
} p[100];

double x,y,xx,yy,a,b,c;

int np, i,j,j;

char hc[20];

double xa, xb, ya, yb, area, triarea;
int nx;

main(){
   linepoints(0,0,0,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(0,10,10,10,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,10,10,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;
   linepoints(10,0,0,0,&a,&b,&c);
   p[np].a = a; p[np].b = b; p[np++].c = c;

   while (3 == scanf("%lf%lf %s",&xx,&yy,hc)) {
      if (strcmp(hc,"Colder")) bi(x,y,xx,yy,&p[np].a,&p[np].b,&p[np].c),np++;
      if (strcmp(hc,"Hotter")) bi(xx,yy,x,y,&p[np].a,&p[np].b,&p[np].c),np++;
      area = 0;
      nd = 0;
      for (i=0;i<np;i++) {
         nx = 0;
         for (j=0;j<np;j++) {
            if (isct(p[i].a,p[i].b,p[i].c,p[j].a,p[j].b,p[j].c,&x,&y)){
               int isin = 1;
               for (k=0;k<np;k++) {
                  if (p[k].a*x + p[k].b*y - p[k].c < -.000001) isin = 0;
               }
               if (isin) {
                  if (!nx) {
                    nx++;
                    xa = x;
                    ya = y;
                  } else if (nx<2 && (fabs(x-xa)>.000001 || fabs(y-ya)>.000001)) {
                    nx++;
                    xb = x;
                    yb = y;
                  }
               }
            }
         }
         if (nx == 2) {
            for (j=0;j<nd;j++) {
               if (fabs(p[i].a*p[done[j]].b - p[i].b*p[done[j]].a) < .000001
                 && fabs(p[i].a*p[done[j]].c - p[i].c*p[done[j]].a) < .000001
                 && fabs(p[i].b*p[done[j]].c - p[i].c*p[done[j]].b) < .000001)
                 break;
            }
            if (j == nd) {
               done[nd++] = i;
               triarea = -p[i].c / hypot(p[i].a,p[i].b) * hypot(xa-xb,ya-yb) / 2;
               area += triarea;
            }
         }
      }
      if (nd < 3 || area < .000001) area = 0;
      printf("%0.2lf\n",area);
      x = xx; y = yy;
   }
}

参考答案(时间最优[8]):

#include <stdio.h>
#include <math.h>

char reach[1<<24];

double w[24], x[24], len, wt, sumw, sumx, cg;
int n;

int solve(int m, double sx, double sw) {
   int i;
   double sumt1, sumt2;
   if (reach[m]) return 0;
   reach[m] = 1;
   cg = sx / sw;
   if (cg < -1.5 || cg > 1.5) return 0;
   if (m == (1<<n)-1) return 1;
   for (i=0;i<n;i++) {
      if (!(m & (1<<i)) && solve(m|(1<<i),sx+x[i]*w[i],sw+w[i])) {
         printf("%lg %lg\n",x[i],w[i]);
         return 1;
      }
   }
   return 0;
}

main(){
   int cc,i;
   for (cc=1; 3 == scanf("%lf%lf%d",&len,&wt,&n) && n; cc++) {
      memset(reach,0,1<<n);
      for (i=0;i<n;i++) scanf("%lf%lf",&x[i],&w[i]);
      printf("Case %d:\n",cc);
      if (!solve(0,0,wt)) printf("Impossible\n");
   }
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注