2014
02-17

# Islands

There are so many islands in the sea. Some islands are enclosed by other ones. So, we can classify all the islands to several levels. An island has a level of 0 if it is contains no other islands. An island has a level of K+1 if it contains one or more islands and the highest level of the contained island(s) is K. Now, the map of the sea is given. Your task is to calculate the areas of islands in each level.

The input contains several test cases. Each test case starts with a line contains two numbers N and M(1<=N<=100,1<=M<=100) which indicate the size of the map. The next N lines will contain M characters each to represent the map. Each character is either ‘x’ or ‘.’. A sea is defined as a maximal connected group of ‘.’ cells, where two ‘.’ cells are connected if they are vertically or horizontally adjacent. An island is defined as a maximal connected group of ‘x’ cells, where two ‘x’ cells are connected if they are vertically, horizontally, or diagonally adjacent. An island A contains island B if A and B are different and if you start sailing from any point of island B, you won’t be able to sail out of island A.

The input contains several test cases. Each test case starts with a line contains two numbers N and M(1<=N<=100,1<=M<=100) which indicate the size of the map. The next N lines will contain M characters each to represent the map. Each character is either ‘x’ or ‘.’. A sea is defined as a maximal connected group of ‘.’ cells, where two ‘.’ cells are connected if they are vertically or horizontally adjacent. An island is defined as a maximal connected group of ‘x’ cells, where two ‘x’ cells are connected if they are vertically, horizontally, or diagonally adjacent. An island A contains island B if A and B are different and if you start sailing from any point of island B, you won’t be able to sail out of island A.

1 1
x
5 7
.xxxxx.
x.....x
x.x.x.x
x.....x
.xxxxx.

1
2 16

http://acm.hit.edu.cn/hoj/problem/view?id=2808

①  :以w为高 以h-2r为周长

h-2r>=2*pi*r → r<=h/(2*pi+2)

r1=min(h/(2*pi+2),w/2)

v1=r1^2*pi*w

②  ：以h-2r为高以w为周长

w>=2*pi*r → r<=w/2*pi

r2=w/2*pi

v2=w^2 * (h-(w/pi))/(4*pi)

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

double min(double a, double b);

int main()
{
const double pi = acos(-1);
double h, w, r1, v1, v2;

while (scanf("%lf %lf", &w, &h) != EOF)
{
if ( (h == 0) && (w == 0) )
break;

r1 = min(0.5 * h / (pi + 1), w / 2);
v1 = r1 * r1 * w * pi;

v2 = w * w * (h - (w / pi) ) / (4 * pi);

if (v1 <= v2)
printf("%.3lf\n",v2);
else
printf("%.3lf\n",v1);
}

return 0;
}

double min(double a, double b)
{
if(a <= b)
return a;
else
return b;
}

1. 给你一组数据吧：29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的，耗时却不短。这种方法确实可以，当然或许还有其他的优化方案，但是优化只能针对某些数据，不太可能在所有情况下都能在可接受的时间内求解出答案。