题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少。
解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短路SPFA,即可得出答案。
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <algorithm> #include <string> #include <vector> #include <queue> #define Mod 1000000007 #define eps 1e-8 using namespace std; #define N 100017 struct Point{ double x,y; Point(double x=0, double y=0):x(x),y(y) {} void input() { scanf("%lf%lf",&x,&y); } }; typedef Point Vector; bool operator < (const Line &L)const { return ang < L.ang; } }; int dcmp(double x) { if(x < -eps) return -1; if(x > eps) return 1; return 0; } template <class T> T sqr(T x) { return x * x;} Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); } Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); } bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; } bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; } bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == 0 && dcmp(a.y-b.y) == 0; } double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; } double Length(Vector A) { return sqrt(Dot(A, A)); } double Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); } double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } Vector VectorUnit(Vector x){ return x / Length(x);} Vector Normal(Vector x) { return Point(-x.y, x.x) / Length(x);} double angle(Vector v) { return atan2(v.y, v.x); } double DisP(Point A,Point B){ return Length(B-A); } bool SegmentIntersection(Point A,Point B,Point C,Point D) { if(dcmp(Cross(C-A,B-A)*Cross(D-A,B-A)) < 0 && dcmp(Cross(A-C,D-C)*Cross(B-C,D-C)) < 0) return true; return false; } //data segment struct node{ Point P[2]; }line[206]; Point p[206]; vector<pair<int,double> > G[206]; double dis[206]; int vis[206],tot,Ltot,S,E; //data ends void SPFA() { for(int i=1;i<=tot;i++) dis[i] = Mod; memset(vis,0,sizeof(vis)); queue<int> q; q.push(S); vis[S] = 1, dis[S] = 0; while(!q.empty()) { int u = q.front(); q.pop(); vis[u] = 0; for(int i=0;i<G[u].size();i++) { int v = G[u][i].first; double w = G[u][i].second; if(dis[v] > dis[u] + w) { dis[v] = dis[u] + w; if(!vis[v]) { q.push(v), vis[v] = 1; } } } } } int main() { int n,i,j,k,h; double x,a,b,c,d; while(scanf("%d",&n)!=EOF && n!=-1) { tot = 1,Ltot = 0; p[1] = Point(0,5); for(i=1;i<=n;i++) { scanf("%lf%lf%lf%lf%lf",&x,&a,&b,&c,&d); p[++tot] = Point(x,a); p[++tot] = Point(x,b); p[++tot] = Point(x,c); p[++tot] = Point(x,d); line[++Ltot].P[0] = Point(x,0), line[Ltot].P[1] = Point(x,a); line[++Ltot].P[0] = Point(x,b), line[Ltot].P[1] = Point(x,c); line[++Ltot].P[0] = Point(x,d), line[Ltot].P[1] = Point(x,10); } p[++tot] = Point(10,5); Point A,B; for(i=0;i<=tot;i++) G[i].clear(); for(i=1;i<=tot;i++) //start { for(j=i+1;j<=tot;j++) //end { A = p[i], B = p[j]; for(k=1;k<=Ltot;k++) { if(SegmentIntersection(A,B,line[k].P[0],line[k].P[1])) break; } if(k == Ltot+1) { G[i].push_back(make_pair(j,DisP(A,B))); G[j].push_back(make_pair(i,DisP(A,B))); } } } S = 1, E = tot; SPFA(); printf("%.2f\n",dis[E]); } return 0; }