## [编辑]格式

PROGRAM NAME: fence3

INPUT FORMAT

(ps:数据中有电网是点的情况，即 68 97 68 97，这貌似与题目叙述不符，请注意 //from Error)（路人甲：貌似照做就行了，不用管，。。。）

OUTPUT FORMAT

3
0 0 0 1
2 0 2 1
0 3 2 3

## [编辑]SAMPLE OUTPUT (file fence3.out)

1.0 1.6 3.7

1. 直接0.01精度搜索必然会爆时间，所以需要逐步提高精度
2. 首先以step = 10的精度搜索坐标(x, y)，找到最小的那些坐标(min_x, min_y)，那么全局最小的点的坐标(g_min_x, g_min_y)肯定满足：min_x - step <= g_min_x <= min_x + step，min_y - step <= g_min_y <= min_y + step，也就是以点(min_x, min_y)为中心，2 * step为边长的正方形中
3. 下一步就是以step = 1的精度在2中得出的范围中搜索，然后逐步提高精度，一直到step = 0.01
4. 求出step = 0.01中距离最小的点即可

/*
ID: zc.rene1
LANG: C
PROG: fence3
*/

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

#define MAX 100
#define MAX_DISTANCE 9999999.0

int F;
double fences[150][4];
double global_min_distance = MAX_DISTANCE, global_min_x, global_min_y;

void GetInput(FILE *fin)
{
int i, j;

fscanf(fin, "%d", &F);
for (i=0; i<F; i++)
{
for (j=0; j<4; j++)
{
fscanf(fin, "%lf", &fences[i][j]);
}
}
}

double DeltaDistance(double x, double y, int index)
{
double x1 = fences[index][0];
double y1 = fences[index][1];
double x2 = fences[index][2];
double y2 = fences[index][3];

double ret = 0.0, temp;

if (x1 > x2)
{
temp = x1;
x1 = x2;
x2 = temp;
}
if (y1 > y2)
{
temp = y1;
y1 = y2;
y2 = temp;
}

if (x < x1)
{
ret += ((x1 - x) * (x1 - x));
}
if (x > x2)
{
ret += ((x - x2) * (x - x2));
}

if (y < y1)
{
ret += ((y1 - y) * (y1 - y));
}
if (y > y2)
{
ret += ((y - y2) * (y - y2));
}

return sqrt(ret);
}

double GetDistance(double x, double y)
{
int i;
double ret = 0.0;

for (i=0; i<F; i++)
{
}

return ret;
}

void CalDistance(double min_x, double max_x, double min_y, double max_y, double step)
{
double x, y, new_step;
double temp_distance, temp_min_distance = MAX_DISTANCE;
double stack[10][2];
int i, bottom = -1, top = -1;

for (x=min_x; x<=max_x; x+=step)
{
for (y=min_y; y<=max_y; y+=step)
{
temp_distance = GetDistance(x, y);
if (temp_distance < temp_min_distance)
{
temp_min_distance = temp_distance;
top = 0;
stack[top][0] = x;
stack[top][1] = y;
}
else if (temp_distance == temp_min_distance)
{
top++;
stack[top][0] = x;
stack[top][1] = y;
}
}
}

new_step = step / 10.0;

if (new_step >= 0.01)
{
while (top > bottom)
{
x = stack[top][0];
y = stack[top][1];
CalDistance(x - step, x + step, y - step, y + step, new_step);
top--;
}
}
else
{
if (temp_min_distance < global_min_distance)
{
for (i=bottom+1; i<=top; i++)
{
global_min_distance = temp_min_distance;
global_min_x = stack[i][0];
global_min_y = stack[i][1];
}
}
}
}

int main(void)
{
FILE *fin, *fout;

fin = fopen("fence3.in", "r");
fout = fopen("fence3.out", "w");

GetInput(fin);
CalDistance(0.0, 100.0, 0.0, 100.0, 10.0);
fprintf(fout, "%.1lf %.1lf %.1lf\n", global_min_x, global_min_y, global_min_distance);

return 0;
}

#### 洛谷 P2735 电网 Electric Fences

2017-08-10 10:28:41

#### usaco 5.2 Electric Fences（模拟退火）

2012-09-24 11:50:54

#### USACOTraining 3.4.3 Electric Fences电网 题解

2013-08-13 11:08:02

#### usaco traini 5.2.2 Electric Fences 题解

2014-03-13 14:42:46

#### Section 5.2 Electric Fences

2010-04-10 09:37:00

#### usaco 5.2.2 Electric Fences

2014-06-28 14:19:52

#### usaco6.4.2 Electric Fences

2017-12-23 00:30:50

#### USACO Electric Fences 解题报告

2014-11-02 05:32:03

#### USACO6.4.2 Electric Fences(fence3)

2015-02-06 19:06:31

#### USACO-Section3.4 Electric Fences【数学】

2018-03-01 00:09:45

Electric Fences