• POJ 1066 Treasure Hunt [想法题]


    题目链接: http://poj.org/problem?id=1066

    --------------------------------------------------------------------------------------------------------

    这题刚看后可能会去纠结如何建图后进行最短路

    不过这样做不仅代码会复杂许多 还有可能出现些不好判断的部分

    不过再多想一下我们可以发现如下性质

    如果起点和终点位于某条障碍线段的两侧 那么这条线段有且仅有一次被穿过

    所以只要统计起点和终点连线穿过的线段条数即可得到答案

    又由于保证输入都是整数 所以我们只用在边界上枚举所有相邻整点的中点作为起点即可

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cmath>
     4 #include <algorithm>
     5 using namespace std;
     6 struct node
     7 {
     8     double x, y;
     9 }a[70], b, c;
    10 double cross(const node &aa, const node &bb, const node &cc)
    11 {
    12     return (bb.x - aa.x) * (cc.y - aa.y) - (bb.y - aa.y) * (cc.x - aa.x);
    13 }
    14 bool check(const node &aa, const node &bb, const node &cc, const node &dd)
    15 {
    16     return cross(aa, bb, cc) * cross(aa, bb, dd) < 0;
    17 }
    18 int n, ans = 100, cnt;
    19 int main()
    20 {
    21     scanf("%d", &n);
    22     for(int i = 1; i <= (n << 1); ++i)
    23         scanf("%lf%lf", &a[i].x ,&a[i].y);
    24     scanf("%lf%lf", &b.x , &b.y);
    25     for(int i = 0; i < 100; ++i)
    26     {
    27         cnt = 0;
    28         c.x = 0.5 + i;
    29         c.y = 0;
    30         for(int j = 1; j < (n << 1); j += 2)
    31             if(check(a[j], a[j + 1], b, c))
    32                 ++cnt;
    33         ans = min(ans, cnt);
    34         cnt = 0;
    35         c.x = 0.5 + i;
    36         c.y = 100;
    37         for(int j = 1; j < (n << 1); j += 2)
    38             if(check(a[j], a[j + 1], b, c))
    39                 ++cnt;
    40         ans = min(ans, cnt);
    41         cnt = 0;
    42         c.y = 0.5 + i;
    43         c.x = 0;
    44         for(int j = 1; j < (n << 1); j += 2)
    45             if(check(a[j], a[j + 1], b, c))
    46                 ++cnt;
    47         ans = min(ans, cnt);
    48         cnt = 0;
    49         c.y = 0.5 + i;
    50         c.x = 100;
    51         for(int j = 1; j < (n << 1); j += 2)
    52             if(check(a[j], a[j + 1], b, c))
    53                 ++cnt;
    54         ans = min(ans, cnt);
    55     }
    56     printf("Number of doors = %d
    ", ans + 1);
    57     return 0;
    58 }
  • 相关阅读:
    UIWebView长按弹出菜单显示英文解决办法
    远程推送不能获取token的原因(证书配置正确)
    汉字转拼音 汉字排序功能
    旋转360度动画
    获取wifi列表
    openssl生成私钥公钥的步骤
    JAVA后台框架优化之日志篇
    react native学习资料
    RAP, 高效前后端联调框架,接口文档管理工具
    【JMeter】JMeter在linux下运行
  • 原文地址:https://www.cnblogs.com/sagitta/p/5263811.html
Copyright © 2020-2023  润新知