24h購物| | PChome| 登入
2013-12-15 19:06:01| 人氣3,214| 回應0 | 上一篇 | 下一篇

[UVA][線段交] 393 - The Doors

推薦 0 收藏 0 轉貼0 訂閱站台


 The Doors 

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 file 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

題目描述:

給一個 10x10 大小的地圖,以及一些阻擋的門。
問求
(0,5) 到 (10,5) 的最短路徑。
輸入的每一組門,在同一個 x 座標上,會有 4 個 y 軸值,代表開啟兩道門。

題目解法:
窮舉線段端點到端點之間是否有交於其他牆壁,沒有的話可以拉成一線。
因為可以保證最短路徑一定會經過端點與端點。

由於點數很少,可以直接 floyd。
n = 0 也是要輸出的,完全忘了這檔事得到許多 WA。

#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <iostream>
#include <sstream>
using namespace std;

struct Pt {
double x, y;
Pt(double a=0, double b=0) :
x(a), y(b) {}
};
#define eps 1e-8
double cross(Pt o, Pt a, Pt b) {
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
int intersection(Pt as, Pt at, Pt bs, Pt bt) {
if(cross(as, at, bs)*cross(as, at, bt) < 0 &&
cross(at, as, bs)*cross(at, as, bt) < 0 &&
cross(bs, bt, as)*cross(bs, bt, at) < 0 &&
cross(bt, bs, as)*cross(bt, bs, at) < 0)
return 1;
return 0;
}
double g[505][505];
Pt gpt[505];
Pt seg[4096][2];
int main() {
int n;
int i, j, k;
char s[1024];
while(scanf("%d", &n) == 1) {
if(n == -1)
break;
int m = 0, pn = 0;
gpt[pn++] = Pt(0, 5);
gpt[pn++] = Pt(10, 5);
for(i = 0; i < n; i++) {
double x, y1, y2, y3, y4;
scanf("%lf %lf %lf %lf %lf", &x, &y1, &y2, &y3, &y4);
seg[m][0] = Pt(x, 0);
seg[m][1] = Pt(x, y1);
gpt[pn++] = Pt(x, y1);
m++;
seg[m][0] = Pt(x, y2);
gpt[pn++] = Pt(x, y2);
seg[m][1] = Pt(x, y3);
gpt[pn++] = Pt(x, y3);
m++;
seg[m][0] = Pt(x, y4);
gpt[pn++] = Pt(x, y4);
seg[m][1] = Pt(x, 10);
m++;
}
for(i = 0; i < pn; i++) {
for(j = 0; j < pn; j++) {
g[i][j] = 1e+30;
int ok = 1;
for(k = 0; k < m; k++) {
ok &= !intersection(gpt[i], gpt[j], seg[k][0], seg[k][1]);
}
if(ok) {
g[i][j] = hypot(gpt[i].x-gpt[j].x, gpt[i].y-gpt[j].y);
}
}
}
for(k = 0; k < pn; k++)
for(i = 0; i < pn; i++)
for(j = 0; j < pn; j++)
g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
printf("%.2lf\n", g[0][1]);
}
return 0;
}

台長: Morris
人氣(3,214) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][greedy] 11493 - The Club Ballroom
此分類上一篇:[UVA][dp] 11499 - Longest Increasing Sequence

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文