Treasure Hunt

题目大意:在一个正方形的迷宫里有一些交错墙,墙的两端都在迷宫的边缘墙上面,现在得知迷宫的某个位置有一个宝藏,所以需要砸开墙来获取宝藏(只能砸一段墙的中点),问最少要砸开几面墙。
 
分析:这个题意刚开始理解错了,以为只能砸整面墙的中点,而实际上使一段墙的中点,也就是两个交点之间的墙,这样问题就变得比较容易了,中点的意义也就不存在了,因为所有的墙都是连接的边缘,所以如果从起点到终点连线有这堵墙,那么这堵墙一定无法绕过去,所以枚举所有的边缘点到终点有几面墙即可,注意不管怎样,边缘墙一定是要砸的。
 
代码如下:
===========================================================================================================================
#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;

const int MAXN = 1e3+7;
const int oo = 1e9+7;
const double EPS = 1e-8;

struct point
{
    double x, y;
    point(double x=0, double y=0):x(x), y(y){}
    point operator - (const point &t) const{
        return point(x-t.x, y-t.y);
    }
    int operator *(const point &t) const{
        double ans = x * t.y - y * t.x;

        if(ans > EPS)return 1;
        if(fabs(ans) < EPS)return 0;
        return -1;
    }
    bool operator == (const point &t) const{
        return fabs(x-t.x)<EPS && fabs(y-t.y)<EPS;
    }
};
struct segment
{
    point A, B;
    segment(point A=0, point B=0):A(A), B(B){}
    bool inter(const segment &t) const{
        return (A-B)*(t.A-B) + (A-B)*(t.B-B) == 0;
    }
};
int Find(segment a, segment sg[], int N)
{
    int sum = 0;

    for(int i=0; i<N; i++)
    {
        if(a.inter(sg[i]) && sg[i].inter(a))
            sum++;
    }

    return sum;
}

int main()
{
    int N;

    while(scanf("%d", &N) != EOF)
    {
        int k=0, Min = oo;
        segment sg[MAXN];
        point p[MAXN], End, A, B;

        for(int i=0; i<N; i++)
        {
            scanf("%lf%lf%lf%lf", &A.x, &A.y, &B.x, &B.y);
            p[k++] = A, p[k++] = B;
            sg[i] = segment(A, B);
        }
        scanf("%lf%lf", &End.x, &End.y);

        for(int i=0; i<k; i++)
        {
            if(p[i] == End)
                continue;
            Min = min(Min, Find(segment(End, p[i]), sg, N)+1);
        }

        if(Min == oo)Min = 1;

        printf("Number of doors = %d
", Min);
    }

    return 0;
}
原文地址:https://www.cnblogs.com/liuxin13/p/4792196.html