POJ 1066——计算几何初步(此题其实很简单,由于题目看错,导致悲剧)

这题初看就想到用BFS+计算几何,不过WA了几次后。

把题目看了一遍。。。尼玛啊,题目理解错了。。。。。。

唉~~~~很水的题被我想复杂了。

只要枚举每个边界上的点与终点的线段,最少相交的点数。

View Code
 1 #include<iostream>
2 #include<queue>
3 #define EPS 1e-8
4 using namespace std;
5
6 struct point {
7 double x, y;
8 }wall[30][2];
9
10 point tag;
11
12 double cross(double x1, double y1, double x2, double y2)
13 {
14 return y2*x1 - x2*y1;
15 }
16
17 bool intersect(int p, point *st, point *ed)
18 {
19 double dx1 = st->x - wall[p][0].x;
20 double dy1 = st->y - wall[p][0].y;
21 double dx2 = wall[p][1].x - wall[p][0].x;
22 double dy2 = wall[p][1].y - wall[p][0].y;
23 double dx3 = ed->x - wall[p][0].x;
24 double dy3 = ed->y - wall[p][0].y;
25 if (cross(dx1,dy1,dx2,dy2)*cross(dx2,dy2,dx3,dy3) > EPS)return true;
26 return false;
27 }
28
29 int check(point *st, point *ed, int n)
30 {
31 int cnt = 0;
32 for (int i(0); i<n; ++i) {
33 if (intersect(i,st,ed)) {
34 cnt++;
35 }
36 }
37 return cnt;
38 }
39
40 int main()
41 {
42 int T;
43 while (scanf("%d",&T) != EOF) {
44 for (int i(0); i<T; ++i) {
45 scanf("%lf%lf%lf%lf",&wall[i][0].x,&wall[i][0].y,&wall[i][1].x,&wall[i][1].y);
46 }
47 scanf("%lf%lf",&tag.x,&tag.y);
48 int cnt = INT_MAX;
49 for (int i(0); i<T; ++i) {
50 for (int j(0); j<2; ++j) {
51 cnt = min(check(&tag,&wall[i][j],T),cnt);
52 }
53 }
54 if (T != 0)printf("Number of doors = %d\n",cnt+1);
55 else printf("Number of doors = 1\n");
56 }
57 return 0;
58 }



posted on 2012-01-28 22:25  Dev-T  阅读(216)  评论(0编辑  收藏  举报