惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
博客园 - 叶小钗
爱范儿
爱范儿
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
S
SegmentFault 最新的问题
Apple Machine Learning Research
Apple Machine Learning Research
美团技术团队
T
Tailwind CSS Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
小众软件
小众软件
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
V
Visual Studio Blog
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell

OhYee 博客

小鹏辅助驾驶测评|OhYee 博客 小鹏非支持手机开启自动解锁|OhYee 博客 使用函数计算实现 301 重定向|OhYee 博客 针对 HTML 内容使用 Ant Design 图片弹框|OhYee 博客 博客进程泄露及僵尸进程解决|OhYee 博客 蓝易云服务器体验|OhYee 博客 SSH 调起本地 VSCode|OhYee 博客 【2022 秋招内推】阿里云后端研发工程师|OhYee 博客 使用函数计算获取 IP 地址信息|OhYee 博客 正确获取客户端 IP/HTTP Header 也可能重复|OhYee 博客 评测 Oculus Quest2 及 BigScreen|OhYee 博客 NextJS 热重载保留状态|OhYee 博客 如何优雅地贴 gist 代码|OhYee 博客 Linux 精细化文件权限|OhYee 博客 VSCode 容器开发环境|OhYee 博客 Clash 的不兼容更新排查|OhYee 博客 Zeek 导出 PCAP|OhYee 博客 记一次 ssh 配置问题|OhYee 博客 Git Commit 规范化工具|OhYee 博客 谈谈《星之卡比-探索发现》|OhYee 博客 VSCode 快捷键绑定 Shell 命令|OhYee 博客 ASN.1 语法及 X.509 证书格式解析解析|OhYee 博客 腾讯企业邮箱忽略 MX 记录发信|OhYee 博客 Chrome/Edge 标签组插件|OhYee 博客 【应届内推】阿里云后端研发工程师|OhYee 博客 损坏的 Typecho 备份处理为 JSON|OhYee 博客 VS Code VIM 插件高效使用|OhYee 博客 SSH 正反向代理|OhYee 博客 Let's Encrypt 根证书过期引发的问题|OhYee 博客 OpenWRT 忽略内核依赖|OhYee 博客
POJ 1066.Treasure Hunt|OhYee 博客
2017-09-13 · via OhYee 博客

题目

{% fold 点击显/隐题目 %}

Archeologists from the Antiquities and Curios Museum (ACM) have flown to Egypt to examine the great pyramid of Key-Ops. Using state-of-the-art technology they are able to determine that the lower floor of the pyramid is constructed from a series of straightline walls, which intersect to form numerous enclosed chambers. Currently, no doors exist to allow access to any chamber. This state-of-the-art technology has also pinpointed the location of the treasure room. What these dedicated (and greedy) archeologists want to do is blast doors through the walls to get to the treasure room. However, to minimize the damage to the artwork in the intervening chambers (and stay under their government grant for dynamite) they want to blast through the minimum number of doors. For structural integrity purposes, doors should only be blasted at the midpoint of the wall of the room being entered. You are to write a program which determines this minimum number of doors. An example is shown below:

The input will consist of one case. The first line will be an integer n (0 <= n <= 30) specifying number of interior walls, followed by n lines containing integer endpoints of each wall x1 y1 x2 y2 . The 4 enclosing walls of the pyramid have fixed endpoints at (0,0); (0,100); (100,100) and (100,0) and are not included in the list of walls. The interior walls always span from one exterior wall to another exterior wall and are arranged such that no more than two walls intersect at any point. You may assume that no two given walls coincide. After the listing of the interior walls there will be one final line containing the floating point coordinates of the treasure in the treasure room (guaranteed not to lie on a wall).

Print a single line listing the minimum number of doors which need to be created, in the format shown below.

7 20 0 37 100 40 0 76 100 85 0 0 75 100 90 0 90 0 71 100 61 0 14 100 38 100 47 47 100 54.5 55.4

Number of doors = 2

{% endfold %}

题解

一开始没仔细读题,没有考虑中点,直接求了向各个方向相交最小值
(一般按照套路也只能这样算)

注意到中点后,推了一下,发现好像不需要刻意考虑中点
大概理由如下:
从任意一个位置到宝藏处,要通过的墙是确定的,不需要可以考虑墙的先后顺序
如果直接走过来不穿这个墙,就说明这个墙对我们毫无影响
如果直接走过来穿了这个墙,不管先穿后穿只打一个洞就行了(相当于通过了这一层)

因此直接跑就行了

最后记得记上边界墙
同时判断相交不考虑端点情况
特判 n==0

代码

{% fold 点击显/隐代码 %}```cpp Treasure Hunt https://github.com/OhYee/sourcecode/tree/master/ACM 代码备份
#include
#include
#include
#include
#include
using namespace std;

#define Log(format, ...) // printf(format, ##VA_ARGS)

/* 计算几何模板 */
const double eps = 1e-8;
const double INF = 9e50;
typedef complex Vector;
typedef Vector Point;
struct Segment {
Point a, b;
Segment() {}
Segment(Point _a, Point _b) : a(_a), b(_b) {}
Vector toVector() { return b - a; }
};

inline Point read_Point() {
double x, y;
scanf("%lf%lf", &x, &y);
return Point(x, y);
}
inline int sgn(const double &x) {
if (fabs(x) < eps)
return 0;
return x > 0 ? 1 : -1;
}
inline bool operator==(const Vector &vec1, const Vector &vec2) {
return sgn(vec1.real() - vec2.real()) == 0 &&
sgn(vec1.imag() - vec2.imag()) == 0;
}
inline bool operator!=(const Vector &vec1, const Vector &vec2) {
return !(vec1 == vec2);
}

inline int Cross(const Vector &a, const Vector &b) {
return a.real() * b.imag() - a.imag() * b.real();
}
inline int Dot(const Vector &a, const Vector &b) {
return a.real() * b.real() + a.imag() * b.imag();
}

inline double Distance(const Point &a, const Point &b) { return abs(a - b); }

inline int Point_Segment(const Vector &p, const Segment &L) {
return sgn(Cross(L.b - L.a, p - L.a));
}
inline int Segment_Segment(const Segment L1, const Segment L2) {
double a = L1.b.real() - L1.a.real();
double b = L2.b.real() - L2.a.real();
double c = L1.b.imag() - L1.a.imag();
double d = L2.b.imag() - L2.a.imag();
double f = a * d - b * c;

// 平行或重叠
if (sgn(f) == 0)
    return -1;

double g = L2.b.real() - L1.a.real();
double h = L2.b.imag() - L1.a.imag();
double t = (d * g - b * h) / f;
double s = (-c * g + a * h) / f;

// 在延长线上
if (t <= 0 || t >= 1 || s <= 0 || s >= 1)
    return 0;

// 线段相交
return 1;

}

const int maxn = 35;
int n;
Segment segments[maxn];

int calc(Segment L) {
int cnt = 0;
for (int i = 0; i < n; ++i)
cnt += (Segment_Segment(L, segments[i]) > 0);
return cnt;
}

int main() {
while (~scanf("%d", &n)) {
for (int i = 0; i < n; ++i) {
segments[i].a = read_Point();
segments[i].b = read_Point();
}
Point target = read_Point();

    int ans = 9999999;
    for (int i = 0; i < n; i++) {
        ans = min(ans, calc(Segment(target, segments[i].a)));
        ans = min(ans, calc(Segment(target, segments[i].b)));
    }
    if (n == 0)
        ans = 0;
    printf("Number of doors = %d\n", ans + 1);
}
return 0;

}

{% endfold %}