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 ...
随机推荐
- Google Chrome浏览器的使用方法
Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/2011/08/28/2156402.html ] 在Google Chr ...
- linux win7双系统
真恨我自己啊,刚在linux下写了这个博客,因为没有分类,添加了个linux分类.按了F5刷没了.靠,哪里有心情复述啊 一直想装直接装linux系统,现在实现他,以后也要跟上linux的笔记,不然都对 ...
- 使用WinSetupFromUSB来U盘安装windowsXP(不使用win PE系统)
目前用U盘安装XP的多数方法都要借助WINPE,比较麻烦.使用WinSetupFromUSB只需要下载一个6.5MB的绿色软件就可以制作好windows xp的安装U盘,方便简捷. WinSetupF ...
- dom元素和方法总结
主要是参考<精通javascript>. 全局变量有: document.这个变量包含浏览器的html dom文档的引用. HTMElement 这个变量是所要html dom 元素的超类 ...
- 关于iOS7越狱的整理
目前越狱非常的不稳定,已经白苹果第三次了.中途遇见了不少问题,去各大论坛找了下解决办法,算是搬运工. iOS7越狱过程中打开手机上的“evasi0n7”闪退,怎么办?1. 请先尝试卸载手机“evasi ...
- MyEclipse配色字体等配置的解决方案
Myeclipse黑色配色方案(精心修改版) http://download.csdn.net/detail/rehongchen/6579945 如何改变Myeclipse编辑区背景色 http:/ ...
- Android开发实例之闹钟提醒
本实例通过TimePickerDialog时间选择对话框让用户设置闹钟.并通过AlarmManager全局定时器在指定的时间启动闹钟Activity . 程序执行效果图: 实例代码: package ...
- jeecms 2012 源码分析(2) 前台栏目页静态化分析
还是要说到web.xml文件 <welcome-file-list> <welcome-file>index.html</welcome-file> <wel ...
- Android Navigation Drawer(导航抽屉)
Google I/O 2013 Android 更新了Support库,新版本的Support库中新加入了几个比较重要的功能. 添加 DrawerLayout 控件,支持创建 Navigation ...
- android 抽屉式滑动demo
下载地址:https://github.com/asijack/AndroidDrawerDemo 直接上效果图如下: 是不是还不错的样子. 先看看布局文件吧 <android.support. ...