POJ 1031--Fence

题意

题目意思是,在一个平面上,有一个由若干个栅栏围成的多边形。同时还有一个灯,灯可能在多边形里面或者外面(反正不在栅栏上),已知在空间中任何一点受到的光强为I=k/r(r为灯到这一点的连线在平面上投影的长度,其实就是说不考虑灯和这一点的高度),灯在栅栏上某一处宽为d的光强总和为I*|cosα|*d*h(α为灯到栅栏的垂线与灯到这一处的连线的夹角在平面上的投影),求灯在栅栏上的光强总和。

分析

一开始,我看错题了,以为距离是和夹角都是在空间里的,跑去求二重积分,其实都是在平面上的投影,这样只是一重积分。我们从上往下俯视,如下图:
这里写图片描述
那么在宽为d的栅栏上的光强为这里写图片描述,那么整个栅栏上的光强就是对d的积分这里写图片描述,再将d用α表示,得到:
这里写图片描述
显然本题的目标已经变成求灯到栅栏两端构成的夹角了。

由于光不能透过栅栏,所以对于后面的栅栏不必考虑。我们从灯连接一条线到栅栏上任意一点,让此点沿栅栏走一周,此线扫过的角度即是灯光能照射到栅栏上的范围。我们约定这一条线顺时针走动时,角度值正增长,角度值负增长。因此,如果灯在栅栏里面,这条线扫过的角度一定是360度。如果是在外面,最后扫过的角度会变为0,这时候设定两个值记录角度正向最大值和负向最小值,两者的差即为此线扫过的角度。

PS: 角度不会大于360度,所以当角度到达360度时,可以提前退出。
代码如下:
Memory: 248K Time: 32MS Length: 43LINES

#include <cmath>
#include <iomanip>
#include <iostream>
#define min(x,y) (((x) > (y)) ? (y) : (x))
#define max(x,y) (((x) < (y)) ? (y) : (x))
using namespace std;

const double pi = 3.1415926535898;
int main()
{
    double k, h;
    int count;
    double array[101][2] = { 0 };
    while (cin >> k >> h >> count)
    {
        for (int i = 0; i < count; ++i)
            cin >> array[i][0] >> array[i][1];
        array[count][0] = array[0][0];
        array[count][1] = array[0][1];
        double a, b;
        a = atan2(array[0][1], array[0][0]);
        double key = 1.0;
        double sum = 0.0;
        double maxsum = 0.0;
        double minsum = 0.0;
        int i = 1;
        for (; i < count + 1; ++i, a = b)
        {
            b = atan2(array[i][1], array[i][0]);
            double ang = abs(a - b);
            ang = ang > pi ? 2 * pi - ang : ang;
            if (a > b)  key = (a - b < pi) ? 1.0 : -1.0;
            else if (a < b) key = (b - a > pi) ? 1.0 : -1.0;
            else key = 0.0;
            sum += key * ang;
            minsum = min(minsum, sum);
            maxsum = max(sum, maxsum);
            if (maxsum >= 2 * pi || minsum <= -2 * pi || (maxsum - minsum) >= 2 * pi) break;
        }
        cout << setprecision(2) << setiosflags(ios::fixed) << h * k * (i > count ? maxsum - minsum : 2 * pi) << endl;
    }
    return 0;
}
解题报告:Fence 题目来源:POJ 1031 解法或类型: 计算几何 作者:杨清玄 Fence Time Limit:1S Memory Limit:1000K Total Submit:103 Accepted:26 Description There is an area bounded by a fence on some flat field. The fence has the height h and in the plane projection it has a form of a closed polygonal line (without self-intersections), which is specified by Cartesian coordinates (Xi, Yi) of its N vertices. At the point with coordinates (0, 0) a lamp stands on the field. The lamp may be located either outside or inside the fence, but not on its side as it is shown in the following sample pictures (parts shown in a thin line are not illuminated by the lamp): The fence is perfectly black, i.e. it is neither reflecting, nor diffusing, nor letting the light through. Research and experiments showed that the following law expresses the intensity of light falling on an arbitrary illuminated point of this fence: I0=k/r where k is a known constant value not depending on the point in question, r is the distance between this point and the lamp in the plane projection. The illumination of an infinitesimal narrow vertical board with the width dl and the height h is dI=I0*|cosα|*dl*h where I0 is the intensity of light on that board of the fence, α is the angle in the plane projection between the normal to the side of the fence at this point and the direction to the lamp. You are to write a program that will find the total illumination of the fence that is defined as the sum of illuminations of all its illuminated boards. Input The first line of the input file contains the numbers k, h and N, separated by spaces. k and h are real constants. N (3 <= N <= 100) is the number of vertices of the fence. Then N lines follow, every line contains two real numbers Xi and Yi, separated by a space. Output Write to the output file the total illumination of the fence rounded to the second digit after the decimal point. Sample Input 0.5 1.7 3 1.0 3.0 2.0 -1.0 -4.0 -1.0 Sample Output 5.34 Source Northeastern Europe 1998 解题思路: 本题是一道计算几何的题目。首先,由于题目可以得到dI=I0*|cosα|*dl*h 也就是说一条边的总照度为 = = =a*h*k 其中下,X1,X2为一条边的坐右端点,a为这条边对原点所张的角度 所以实际上本题是要求整个FENCE区域对原点所张开的总角度, 定义FENCE为一有向回路 那么每条边都是有向的。。如果按照边的方向对原点所张开的角度为顺时针。那么定义为正。逆时针为负。并且每输入一条边就把本边对原点张开的角度计算进去加到一个数里去 那么对于包含原点的区域。这个数应该为正负2 ; 对于不包含原点的区域,这个数在按边过程中的最大值-最小值就是这个区域对原点所张开的角度。 还有一种情况,那就是区域不包含原点,但是总共张开的角度大于2 ,那么只要计算为2 即可因为原点对任何区域最多只能张开2 。 数据结构: 用一个POINT数组来储存点的位置 时空分析: 如果有N个点 那么空间复杂度为O(N) 时间复杂度为O(N) 源程序: fence.cpp
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值