本文主要是介绍POJ 1556 The Doors 计算几何+ 最短路,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
计算几何入门
枚举任意两个点,看连成的线段之间是否交于其他线段,如果中间没交与其他线段,表明两个点可通过直线到达
然后将所有的这种都跑出来。 建图求最短路即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <map>
#include <sstream>
#include <queue>
#include <vector>
#define MAXN 100005
#define MAXM 211111
#define eps 1e-8
#define INF 500000001
using namespace std;
inline int dblcmp(double d)
{if(fabs(d) < eps) return 0;return d > eps ? 1 : -1;
}
struct point
{double x, y;point(){}point(double _x, double _y): x(_x), y(_y) {}void input(){scanf("%lf%lf", &x, &y);}bool operator ==(point a)const{return dblcmp(a.x - x) == 0 && dblcmp(a.y - y) == 0;}point sub(point p){return point(x - p.x, y - p.y);}double dot(point p){return x * p.x + y * p.y;}double det(point p){return x * p.y - y * p.x;}double distance(point p){return hypot(x - p.x, y - p.y);}
}p[111];
struct line
{point a, b;line(){}line(point _a, point _b){ a = _a; b = _b;}void input(){a.input();b.input();}int segcrossseg(line v){int d1 = dblcmp(b.sub(a).det(v.a.sub(a)));int d2 = dblcmp(b.sub(a).det(v.b.sub(a)));int d3 = dblcmp(v.b.sub(v.a).det(a.sub(v.a)));int d4 = dblcmp(v.b.sub(v.a).det(b.sub(v.a)));if ((d1 ^ d2) == -2 && (d3 ^ d4) == -2)return 2;return (d1 == 0 && dblcmp(v.a.sub(a).dot(v.a.sub(b))) <= 0||d2 == 0 && dblcmp(v.b.sub(a).dot(v.b.sub(b))) <= 0||d3 == 0 && dblcmp(a.sub(v.a).dot(a.sub(v.b))) <= 0||d4 == 0 && dblcmp(b.sub(v.a).dot(b.sub(v.b))) <= 0);}}seg[111];
int m, pc, sc;
double d[111][111];
int main()
{double x, ya, yb, yc, yd;while(scanf("%d", &m) != EOF){if(m == -1) break;pc = 0, sc = 0;p[pc++] = point(0, 5);p[pc++] = point(10, 5);seg[sc++] = line(point(0, 0), point(0, 10));seg[sc++] = line(point(0, 0), point(10, 0));seg[sc++] = line(point(10, 0), point(10, 10));seg[sc++] = line(point(10, 10), point(0, 10));for(int i = 0; i < m; i++){scanf("%lf%lf%lf%lf%lf", &x, &ya, &yb, &yc, &yd);p[pc++] = point(x, ya);p[pc++] = point(x, yb);p[pc++] = point(x, yc);p[pc++] = point(x, yd);seg[sc++] = line(point(x, 0), point(x, ya));seg[sc++] = line(point(x, yb), point(x, yc));seg[sc++] = line(point(x, yd), point(x, 10));}int n = pc;for(int i = 0; i < n; i++)for(int j = 0; j < n; j++){if(i == j) d[i][j] = 0;int flag = 1;line x = line(p[i], p[j]);for(int k = 0; k < sc; k++)if(x.segcrossseg(seg[k]) == 2){flag = 0;break;}if(flag) d[i][j] = p[i].distance(p[j]);else d[i][j] = INF;}for(int k = 0; k < n; k++)for(int i = 0; i < n; i++)for(int j = 0; j < n; j++){if(d[i][k] + d[k][j] < d[i][j])d[i][j] = d[i][k] + d[k][j];}printf("%.2f\n", d[0][1]);}return 0;
}
这篇关于POJ 1556 The Doors 计算几何+ 最短路的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!