Posted: Tue Oct 02, 2007 11:58 am
Can someone please help me with this one? I keep getting WA!
I checked for lots of input data I found on this forum, and I always get correct answer!
Thanks!
I checked for lots of input data I found on this forum, and I always get correct answer!
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct Point
{
int x;
int y;
}TPoint;
typedef struct K
{
int dest;
int n;
int h;
TPoint *cords;
TPoint *hull;
}TK;
double findAngle(int i, TPoint* data, int minx, int miny)
{
double ret = 0;
int xdif;
xdif = (data[i].x - minx);
if (xdif == 0)
{
if (data[i].y == miny)
{
ret = -1;
}
else
{
ret = 1000;
}
}
else
{
ret = ((double)(data[i].y - miny)/xdif);
if (ret < 0)
{
ret*=-1;
ret = 2000 - ret;
}
}
return ret;
}
void sortByAngle2(TPoint *data, int l, int r, int minx, int miny)
{
int i,j;
TPoint swap;
if (data[l].x == data[l+1].x)
{
for (i=l; i<r; i++)
{
for (j=i+1; j<=r; j++)
{
if (data[i].y>data[j].y)
{
swap = data[i];
data[i] = data[j];
data[j] = swap;
}
}
}
}
else if (data[l].y == data[l+1].y)
{
for (i=l; i<r; i++)
{
for (j=i+1; j<=r; j++)
{
if (data[i].x>data[j].x)
{
swap = data[i];
data[i] = data[j];
data[j] = swap;
}
}
}
}
else
{
for (i=l; i<r; i++)
{
for (j=i+1; j<=r; j++)
{
if ((double)sqrt((data[i].x-minx)*(data[i].x-minx)+(data[i].y-miny)*(data[i].y-miny)) > (double)sqrt((data[j].x-minx)*(data[j].x-minx)+(data[j].y-miny)*(data[j].y-miny)))
{
swap = data[i];
data[i] = data[j];
data[j] = swap;
}
}
}
}
}
void sortByAngle(int lo, int hi, TPoint* data, int minx, int miny)
{
int left, right, pivI;
TPoint swap;
double pivot;
left = lo;
right = hi;
pivI = (left+right)/2;
pivot = findAngle(pivI, data, minx, miny);
while (left<=right)
{
while ((findAngle(left, data, minx, miny) < pivot) && (left<hi)) left++;
while ((findAngle(right, data, minx, miny) > pivot) && (right>lo)) right--;
if (left<=right)
{
swap = data[left];
data[left] = data[right];
data[right] = swap;
left++;
right--;
}
}
if (left>lo) sortByAngle(lo, right, data, minx, miny);
if (right<hi) sortByAngle(left, hi, data, minx, miny);
}
double crossProduct(TPoint *p1, TPoint *p2, TPoint *p3)
{
double ret = 0;
ret = (p2->x - p1->x) * (p3->y - p1->y) - (p3->x - p1->x) * (p2->y - p1->y);
return ret;
}
void findHull(TK* K, int sort)
{
int minx, miny;
int i, j, k;
int top;
minx = 5000; /* INF */
miny = 5000; /* INF */
for (i=0; i<K->n; i++)
{
if (K->cords[i].y < miny)
{
minx = K->cords[i].x;
miny = K->cords[i].y;
}
else if (K->cords[i].y == miny)
{
if (K->cords[i].x < minx)
{
minx = K->cords[i].x;
miny = K->cords[i].y;
}
}
}
if (sort)
{
sortByAngle(0, K->n-1, K->cords, minx, miny);
}
i = 0;
while (i<K->n-1)
{
j = i;
k = j;
while (j<K->n-1 && findAngle(j, K->cords, minx, miny) == findAngle(j+1, K->cords, minx, miny))
{
j++;
}
sortByAngle2(K->cords, k, j, minx, miny);
i = j+1;
}
K->cords[K->n] = K->cords[0];
K->hull[0] = K->cords[0];
K->hull[1] = K->cords[1];
top = 1;
for (i=2; i<=K->n; i++)
{
while ((crossProduct(&(K->hull[top-1]), &(K->hull[top]), &(K->cords[i])) <= 0) && (top>=1))
{
top--;
}
top++;
K->hull[top] = K->cords[i];
}
K->h = top+1;
}
int main()
{
int n, nk, i, j, b;
int missX, missY;
double total, temp;
TPoint missle;
TK K[21];
/*freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);*/
nk = 0;
scanf("%d", &n);
while (n != -1)
{
K[nk].n = n;
K[nk].h = 0;
K[nk].dest = 0;
K[nk].cords = (TPoint*)malloc((n+5)*sizeof(TPoint));
K[nk].hull = (TPoint*)malloc((n+5)*sizeof(TPoint));
for (i=0; i<n; i++)
{
scanf("%d %d", &(K[nk].cords[i].x), &(K[nk].cords[i].y));
}
findHull(&(K[nk]), 1);
nk++;
scanf("%d", &n);
}
K[20].cords = (TPoint*)malloc(105*sizeof(TPoint));
K[20].hull = (TPoint*)malloc(105*sizeof(TPoint));
while (scanf("%d %d", &missX, &missY) == 2)
{
b = 0;
for (i=0; i<nk && !b; i++)
{
if (K[i].dest == 0)
{
K[20].n = K[i].n+1;
for (j=0; j<K[i].n; j++)
{
K[20].cords[j] = K[i].cords[j];
}
missle.x = missX;
missle.y = missY;
K[20].cords[j] = missle;
findHull(&(K[20]),1);
b = 1;
if (K[i].h != K[20].h)
{
b = 0;
}
else if (K[i].h == K[20].h)
{
for (j=0; j<K[i].h; j++)
{
if ((K[i].hull[j].x != K[20].hull[j].x) || (K[i].hull[j].y != K[20].hull[j].y))
{
b = 0;
}
}
}
if (b)
{
K[i].dest = 1;
}
}
}
}
total = 0.0;
for (i=0; i<nk; i++)
{
if (K[i].dest == 1)
{
temp = 0.0;
for (j=1; j<K[i].h; j++)
{
temp += (K[i].hull[j-1].x * K[i].hull[j].y) - (K[i].hull[j].x * K[i].hull[j-1].y);
}
temp/=2;
if (temp<0)
{
temp*=-1;
}
total+=temp;
}
}
printf("%.2lf\n",total);
return 0;
}