Description
JYY has placed N bombs on the plane. We assume that the firepower area of each bomb is circle whose radius is one unit. Can you tell JYY the total firepower overlay area, just have a try ^_^
Input
Standard input will contain multiple test cases. For each test case, the first line is an integer N(N <= 100), which is the total number of the bombs. Then N lines followed and each line contains two integers X and Y, which means the 2-D coordinate of each bomb.
Output
For each test cases, output the total firepower overlay area(accurate to two fractional digits) in one line.
Sample Input
31 11 22 2
Sample Output
6.84
解题思路:1.首先想直接将N个圆的面积N*π,再减去相交的部分,由于A∪B∪C=A+B+C-A∩B-A∩C-B∩C+A∩B∩C比较麻烦。要记住所有相交的位置,所以本题采用相加的策略。
2.相加:分析每个圆会占据4个正方形的位置,则记录四个正方形的形状,共九种状态:如下依次为123456789:
然后1/2/3/4对应的面积为π/4,
5/6/7/8对应的面积为π/6+sin(π/3)/2
9的面积为1
则粘贴代码如下:
#include
#include
#include
#define Pi 3.1415926
typedef struct square{
int x;
int y;
int t;
struct square * next;
}square;
square * search(int x, int y, square * head, int type){
square * cur = head;
int flag = 0;
while (cur->next != NULL){
cur = cur->next;
if ((x == cur->x) && (y == cur->y)){
flag = 1;
if (type == 1){
if (cur->t == 1) cur->t = 1;
else if (cur->t == 2 || cur->t == 5) cur->t = 5;
else if (cur->t == 3 || cur->t == 6) cur->t = 6;
else if (cur->t == 4 || cur->t == 7 || cur->t == 8 || cur->t == 9) cur->t = 9;
break;
}
else if (type == 2){
if (cur->t == 1 || cur->t == 5) cur->t = 5;
else if (cur->t == 2) cur->t = 2;
else if (cur->t == 3 || cur->t == 6 || cur->t == 8 || cur->t == 9) cur->t = 9;
else if (cur->t == 4 || cur->t == 7)cur->t = 7;
break;
}
else if (type == 3){
if (cur->t == 1 || cur->t == 6) cur->t = 6;
else if (cur->t == 2 || cur->t == 7 || cur->t == 5 || cur->t == 9) cur->t = 9;
else if (cur->t == 3) cur->t = 3;
else if (cur->t == 4 || cur->t == 8) cur->t = 8;
break;
}
else if (type == 4){
if (cur->t == 1 || cur->t == 5 || cur->t == 6 || cur->t == 9) cur->t = 9;
else if (cur->t == 2 || cur->t == 7) cur->t = 7;
else if (cur->t == 3 || cur->t == 8) cur->t = 8;
else if (cur->t == 4) cur->t = 4;
break;
}
}
}
if ((cur->next == NULL) && (flag == 0)){
square * temp = malloc(sizeof(square));
temp->next = NULL;
temp->x = x;
temp->y = y;
temp->t = type;
cur->next = temp;
}
return head;
}
double cal(square * head){
double ans = 0;
square * pres = head;
while (pres->next != NULL){
pres = pres->next;
if ((pres->t >= 1) && (pres->t <= 4)){
ans += Pi / 4.0;
}
else if ((pres->t >= 5) && (pres->t <= 8)){
ans += Pi / 6.0 + ((double)sin(Pi / 3.0)) / 2.0;
}
else if (pres->t == 9){
ans += 1;
}
}
return ans;
}
int main(){
int N;
int i;
int x, y;
square * head = malloc(sizeof(square));
//printf("%lf", ((double)sin(Pi / 3.0)) / 2.0);
head->next = NULL;
while (scanf_s("%d", &N) != EOF){
double res = 0;
for (i = 0; i < N; i++){
scanf_s("%d %d", &x, &y);
res = cal(head);
search(x - 1, y - 1, head, 1);
search(x, y - 1, head, 2);
search(x - 1, y, head, 3);
search(x, y, head, 4);
square * temp = head;
square * cut = temp;
while (temp->next != NULL){
printf("x=%d,y=%d,type=%d ", temp->next->x, temp->next->y, temp->next->t);
temp = temp->next;
}
}
res = cal(head);
printf("res=%.2f", res);
square * temp = head;
square * cut = temp;
while (temp->next != NULL){
cut = temp->next;
free(temp);
temp = cut;
}
}
system("pause");
}