poj1556 The Doors (计算几何+最短路)

The Doors

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 11497 Accepted: 4227
Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length.

Input

The input data for the illustrated chamber would appear as follows.

2
4 2 7 8 9
7 3 4.5 6 7

The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.
Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.
Sample Input

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1
Sample Output

10.00
10.06
Source

Mid-Central USA 1996

问从(0.5) 到(10.5)且不能穿过墙的最短路径
将每个点和每条线段 存起来依次判每两点之间的连线是否与线段相交 但是要把含当前点的线段先去掉

#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 4e3 + 5;
const double eps = 1e-8;
int sgn(double x){
	if (fabs(x) < eps) return 0;
	if (x < 0) return -1;
	else return 1;
}
struct Point{
	double x, y;
	Point(){}
	Point(double _x, double _y){
		x = _x; y = _y;
	}
	double operator^(const Point &a)const{
		return x*a.y - y*a.x;
	}

	Point operator-(const Point &a)const{
		return Point(x - a.x, y - a.y);
	}
	bool operator<(const Point &a)const{
		if (x == a.x) return y < a.y;
		return x < a.x;
	}
}P[maxn];
struct Line{
	Point s, e;
	Line(){}
	Line(Point _s, Point _e){
		s = _s; e = _e;
	}
}E[maxn];
bool inter(Line l1, Line l2){
	return	max(l1.s.x, l1.e.x) >= min(l2.s.x, l2.e.x) &&
		max(l2.s.x, l2.e.x) >= min(l1.s.x, l1.e.x) &&
		max(l1.s.y, l1.e.y) >= min(l2.s.y, l2.e.y) &&
		max(l2.s.y, l2.e.y) >= min(l1.s.y, l1.e.y) &&
		sgn((l2.s - l1.s) ^ (l1.e - l1.s))*sgn((l2.e - l1.s) ^ (l1.e - l1.s)) <= 0 &&
		sgn((l1.s - l2.s) ^ (l2.e - l2.s))*sgn((l1.e - l2.s) ^ (l2.e - l2.s)) <= 0;
}
const double INF = 0x3f3f3f3f;
int n, m;//m记录边数
double g[maxn][maxn];//两点之间的代价
int vis[maxn];//是否被访问
double cot[maxn];//代价

int cnt = 0, num = 0;//num记录点的格式

double dijkstra(){
	for (int i = 1; i <= num; i++)
		cot[i] = g[1][i], vis[i] = 0;
	vis[0] = 1;
	for (int v = 1; v <= num; v++){
		double _min = INF; int k = 0;
		for (int i = 1; i <= num; i++)
		if (!vis[i] && _min > cot[i])	_min = cot[i], k = i;
		vis[k] = 1;
		for (int i = 1; i <= num; i++)
		if (!vis[i] && cot[i] > _min + g[k][i]){
			cot[i] = _min + g[k][i];
		}
	}
	return cot[num];
}
double dis(Point a, Point b){
	return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
int main(){
	while (~scanf("%d", &n)){
		if (n == -1) break;
		num = 0; cnt = 0;
		P[++num].x = 0.0, P[num].y = 5.0;//第一个点
		double x, y1, y2, y3, y4;
		for (int i = 1; i <= n; i++){
			scanf("%lf%lf%lf%lf%lf", &x, &y1, &y2, &y3, &y4);
			//每次三条线段
			E[++cnt].s.x = x; E[cnt].s.y = 0;
			E[cnt].e.x = x; E[cnt].e.y = y1;

			E[++cnt].s.x = x; E[cnt].s.y = y2;
			E[cnt].e.x = x; E[cnt].e.y = y3;

			E[++cnt].s.x = x; E[cnt].s.y = y4;
			E[cnt].e.x = x; E[cnt].e.y = 10.0;

			//将每次的门的端点存起来 然后判断能否不穿过墙走到
			P[++num].x = x, P[num].y = y1;
			P[++num].x = x, P[num].y = y2;
			P[++num].x = x, P[num].y = y3;
			P[++num].x = x, P[num].y = y4;

		}
		P[++num].x = 10.0, P[num].y = 5.0;//加到最后一个点

		for (int i = 1; i <= num; i++)
		for (int j = 1; j <= num; j++) g[i][j] = INF;

		for (int i = 1; i <= num; i++){//建边
			for (int j = i + 1; j <= num; j++){
				Line a = Line(P[i], P[j]);//连线
				int flag = 0;
				for (int k = 1; k <= cnt; k++){//判断两点连线是否交于线段
					if (E[k].s.x == P[j].x || E[k].s.x == P[i].x) continue;
					if (inter(a, E[k])){//相交
						flag = 1; break;
					}
				}
				if (!flag)  g[i][j] = dis(P[i], P[j]);
			}
		}
		double D = dijkstra();
		printf("%.2f\n", D);
	}
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值