POJ 1556 The Doors(计算几何+最短路)
这题就是,处理出没两个点。假设能够到达,就连一条边,推断可不能够到达,利用线段相交去推断就可以。最后求个最短路就可以
代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; #include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; struct Point {
double x, y;
Point() {}
Point(double x, double y) {
this->x = x;
this->y = y;
}
void read() {
scanf("%lf%lf", &x, &y);
}
}; typedef Point Vector; Vector operator - (Vector A, Vector B) {
return Vector(A.x - B.x, A.y - B.y);
} const double eps = 1e-8; int dcmp(double x) {
if (fabs(x) < eps) return 0;
else return x < 0 ? -1 : 1;
} double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积 //能够不规范相交
bool SegmentProperIntersection2(Point a1, Point a2, Point b1, Point b2) {
double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1),
c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
return max(a1.x, a2.x) >= min(b1.x, b2.x) &&
max(b1.x, b2.x) >= min(a1.x, a2.x) &&
max(a1.y, a2.y) >= min(b1.y, b2.y) &&
max(b1.y, b2.y) >= min(a1.y, a2.y) &&
dcmp(c1) * dcmp(c2) <= 0 && dcmp(c3) * dcmp(c4) <= 0;
} const int N = 25; int n; struct Ban {
Point p[4];
void read() {
double a, y[4];
scanf("%lf", &a);
for (int i = 0; i < 4; i++) {
scanf("%lf", &y[i]);
p[i] = Point(a, y[i]);
}
}
} b[N]; struct Edge {
int u, v;
double w;
Edge(){}
Edge(int u, int v, double w) {
this->u = u;
this->v = v;
this->w = w;
}
}; vector<Edge> g[N * 4]; double dist(Point a, Point b) {
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt(dx * dx + dy * dy);
} void add_edge(int u, int v, double d) {
g[u].push_back(Edge(u, v, d));
g[v].push_back(Edge(v, u, d));
} bool judge(int l, int r, Point aa, Point bb) {
for (int i = l; i <= r; i++) {
if (!SegmentProperIntersection2(aa, bb, b[i].p[0], b[i].p[1]) && !SegmentProperIntersection2(aa, bb, b[i].p[2], b[i].p[3]))
return false;
}
return true;
} double d[N * 4];
int vis[N * 4]; double spfa(int s, int t) {
memset(vis, 0, sizeof(vis));
queue<int> Q;
for (int i = 0; i <= t; i++) d[i] = 1e20;
d[0] = 0;
vis[0] = 1;
Q.push(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].v;
double w = g[u][i].w;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
if (!vis[v]) {
vis[v] = 1;
Q.push(v);
}
}
}
}
return d[t];
} int main() {
while (~scanf("%d", &n) && n != -1) {
for (int i = 0; i <= n * 4 + 1; i++) g[i].clear();
for (int i = 0; i < n; i++)
b[i].read();
if (judge(0, n - 1, Point(0, 5), Point(10, 5)))
add_edge(0, n * 4 + 1, 10);
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
if (judge(0, i - 1, Point(0, 5), b[i].p[j]))
add_edge(0, i * 4 + j + 1, dist(Point(0, 5), b[i].p[j]));
if (judge(i + 1, n - 1, b[i].p[j], Point(10, 5)))
add_edge(n * 4 + 1, i * 4 + j + 1, dist(Point(10, 5), b[i].p[j]));
for (int k = i + 1; k < n; k++) {
for (int x = 0; x < 4; x++) {
if (judge(i + 1, k - 1, b[i].p[j], b[k].p[x]))
add_edge(i * 4 + j + 1, k * 4 + x + 1, dist(b[i].p[j], b[k].p[x]));
}
}
}
}
printf("%.2f\n", spfa(0, n * 4 + 1));
}
return 0;
}
POJ 1556 The Doors(计算几何+最短路)的更多相关文章
- POJ 1556 The Doors【最短路+线段相交】
思路:暴力判断每个点连成的线段是否被墙挡住,构建图.求最短路. 思路很简单,但是实现比较复杂,模版一定要可靠. #include<stdio.h> #include<string.h ...
- POJ 1556 - The Doors 线段相交不含端点
POJ 1556 - The Doors题意: 在 10x10 的空间里有很多垂直的墙,不能穿墙,问你从(0,5) 到 (10,5)的最短距离是多少. 分析: 要么直达,要么 ...
- POJ 1556 The Doors 线段交 dijkstra
LINK 题意:在$10*10$的几何平面内,给出n条垂直x轴的线,且在线上开了两个口,起点为$(0, 5)$,终点为$(10, 5)$,问起点到终点不与其他线段相交的情况下的最小距离. 思路:将每个 ...
- ●POJ 1556 The Doors(简单计算几何+最短路)
●赘述题目 10*10的房间内,有竖着的一些墙(不超过18个).问从点(0,5)到(10,5)的最短路. 按照输入样例,输入的连续5个数,x,y1,y2,y3,y4,表示(x,0--y1),(x,y2 ...
- POJ 1556 - The Doors - [平面几何+建图spfa最短路]
题目链接:http://poj.org/problem?id=1556 Time Limit: 1000MS Memory Limit: 10000K Description You are to f ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- poj 1556 The Doors(线段相交,最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7430 Accepted: 2915 Descr ...
- POJ 1556 The Doors --几何,最短路
题意: 给一个正方形,从左边界的中点走到右边界的中点,中间有一些墙,问最短的距离是多少. 解法: 将起点,终点和所有墙的接触到空地的点存下来,然后两两之间如果没有线段(墙)阻隔,就建边,最后跑一个最短 ...
- 简单几何(线段相交+最短路) POJ 1556 The Doors
题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijks ...
随机推荐
- 安装GNUstep并运行第一个objc程序
在windows环境下安装GNUstep,运行objective-c程序,今天试了一下,记录一下操作步骤, 1,登陆http://ftpmain.gnustep.org/pub/gnustep/bin ...
- DB2 相关操作
查看数据库版本:db2level DB21061E Command line environment not initialized. windows命令行下,需要先调用db2cmd,或者DB2 ...
- CSS3 Gradient-CSS3渐变
CSS3 Gradient分为linear-gradient(线性渐变)和radial-gradient(径向渐变).而我们今天主要是针对线性渐变来剖析其具体的用法.为了更好的应用CSS3 Gradi ...
- Combotree,datebox 启用 禁用
combotree <input type="checkbox" id="ckMonitor"></input> <input i ...
- C#实现邮件发送功能
发送邮件所用的核心知识点 微软封装好的MailMessage类:主要处理发送邮件的内容(如:收发人地址.标题.主体.图片等等) 微软封装好的SmtpClient类:主要处理用smtp方式发送此邮件的配 ...
- Android LocalActivityManager的用法
在开发中会碰到在一个activity中的局部(或者是activity的Fragment中)显示其他的activity 的内容,这时就用到了LocalActivityManager类. 假设这个容器是一 ...
- poj2231---暴力
#include<stdio.h> #include<stdlib.h> #include<math.h> ]; int cmp(const void *a,con ...
- gdb的user-define command
搜索: user-defined例子. # save this file in ~/.gdb or some where easy to find. # then in ~/.gdbinit add ...
- leetcode_question_115 Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- Tomcat的JVM内存大小如何设置?【转】
[转]:专家答疑 Tomcat的JVM内存大小如何设置? 本文和大家重点讨论一下如何设置Tomcat的JVM内存大小,JAVA程序启动时JVM都会分配一个初始内存和最大内存给这个应用程序.这个初始内存 ...