-
Notifications
You must be signed in to change notification settings - Fork 0
/
ballbox.c
167 lines (154 loc) · 3.11 KB
/
ballbox.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*=============================================================================
# FileName: ballbox.c
# Desc:
# Author: LiChenda
# Email: lichenda1996@gmail.com
# HomePage: https://github.com/LiChenda
# Version: 0.0.1
# LastChange: 2016-05-29 21:08:30
# History:
=============================================================================*/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define true 1
#define false 0
typedef struct _Ball {
double x;
double y;
double r;
} Ball;
typedef struct _listNode {
struct _listNode * next;
int isblock;
Ball ball;
} listNode;
void insert(Ball ball, int isblock);
double distance(Ball b1, Ball b2);
double m_abs(double num);
int judge(Ball b);
void freeBox();
void putBall();
void putBlock(double x, double y);
listNode *head = NULL;
double step = 0.01;
int main(int argc, char *argv[])
{
int n, blocks, i;
while(scanf("%d%d", &n, &blocks) != EOF)
{
for(i = 0; i < blocks; i ++)
{
double x, y;
scanf("%lf%lf", &x, &y);
putBlock(x, y);
}
for(i = 0; i < n; i ++)
{
putBall();
}
listNode *tmp = head;
double R2 = 0;
printf("X\t Y\t R\n");
while (tmp) {
if(tmp->isblock)
{
tmp = tmp->next;
continue;
}
printf("%.3lf\t %.3lf\t %.3lf\t\n",tmp->ball.x, tmp->ball.y, tmp->ball.r);
R2 += tmp->ball.r * tmp->ball.r;
tmp = tmp->next;
}
printf("\nsum of R^2: \t%lf\n", R2);
freeBox();
}
return 0;
}
void putBlock(double x, double y)
{
Ball ball = {x, y, 0};
insert(ball, true);
}
void freeBox()
{
while(head)
{
listNode *tmp = head;
head = tmp->next;
free(tmp);
}
}
void insert(Ball ball, int isblock)
{
listNode * node = (listNode *)malloc(sizeof(listNode));
node->isblock = isblock;
node->ball = ball;
node->next = head;
head = node;
}
void putBall()
{
Ball ball = {-1 + step, -1 + step, 0};
Ball maxBall = ball;
int i, j;
for (i = 0; ball.x < 1; ++i) {
ball.x += step;
ball.y = -1 + step;
for (j = 0; ball.y < 1; ++j) {
ball.y += step;
ball.r = 0;
double rstep = 0.1;
while(rstep > 0.00001)
{
if(ball.r > maxBall.r)
{
maxBall = ball;
}
/*printf("%lf, %lf, %lf\n",ball.x, ball.y, ball.r);*/
ball.r += rstep;
if(!judge(ball))
{
ball.r -= rstep;
rstep /= 10;
}
}
}
}
if(judge(maxBall)){
insert(maxBall, false);
printf("%.3lf\t %.3lf\t %.3lf\t\n",maxBall.x, maxBall.y, maxBall.r);
}
}
int judge(Ball b)
{
if((m_abs(b.x) + b.r) > 1 || (m_abs(b.y) + b.r) > 1)
{
return false;
}
listNode *tmp = head;
while(tmp)
{
Ball ball = tmp->ball;
if(distance(b, ball) < b.r + ball.r )
{
return false;
}
tmp = tmp->next;
}
return true;
}
double m_abs(double num)
{
if(num > 0)
return num;
return 0 - num;
}
double distance(Ball b1, Ball b2)
{
double x1 = b1.x;
double y1 = b1.y;
double x2 = b2.x;
double y2 = b2.y;
return pow((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2), 0.5);
}