poj 1066 Treasure Hunt (Geometry + BFS)
题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方。可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁。问最少要穿过多少墙壁才能得到宝藏。
比较容易想到的一个办法就是直接用中点构图,然后判断点与点之间是否能够直接相连,最后bfs得到最小距离。
我的代码也是这样做,结果相当险的900+ms通过。
代码如下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath> using namespace std; const double EPS = 1e-;
const int N = ;
inline int sgn(double x) { return (x > EPS) - (x < -EPS);} struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && y < a.y;}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.y) == ;}
Point operator + (Point a) { return Point(x + a.x, y + a.y);}
Point operator - (Point a) { return Point(x - a.x, y - a.y);}
Point operator * (double p) { return Point(x * p, y * p);}
Point operator / (double p) { return Point(x / p, y / p);}
} ;
inline double cross(Point a, Point b) { return a.x * b.y - a.y * b.x;}
inline double dot(Point a, Point b) { return a.x * b.x + a.y * b.y;}
inline double veclen(Point a) { return sqrt(dot(a, a));}
inline Point vecunit(Point x) { return x / veclen(x);}
inline Point normal(Point x) { return Point(-x.y, x.x) / veclen(x);}
inline bool onseg(Point x, Point a, Point b) { return sgn(cross(a - x, b - x)) == && sgn(dot(a - x, b - x)) <= ;} struct Line {
Point s, t;
Line() {}
Line(Point s, Point t) : s(s), t(t) {}
Point vec() { return t - s;}
Point point(double p) { return s + vec() * p;}
} ;
inline bool onseg(Point x, Line l) { return onseg(x, l.s, l.t);}
inline Point llint(Line a, Line b) { return a.point(cross(b.vec(), a.s - b.s) / cross(a.vec(), b.vec()));} Point ips[N][N], trs, vex[N * N];
bool out[N * N];
int ipcnt[N], vexcnt;
Line ls[N]; void input(int &n) {
Point tmp[];
for (int i = ; i < n; i++) {
for (int j = ; j < ; j++) cin >> tmp[j].x >> tmp[j].y;
ls[i] = Line(tmp[], tmp[]);
}
ls[n++] = Line(Point(0.0, 0.0), Point(100.0, 0.0));
ls[n++] = Line(Point(100.0, 0.0), Point(100.0, 100.0));
ls[n++] = Line(Point(100.0, 100.0), Point(0.0, 100.0));
ls[n++] = Line(Point(0.0, 100.0), Point(0.0, 0.0));
cin >> trs.x >> trs.y;
} inline Point mid(Point a, Point b) { return (a + b) / 2.0;} int makevex(int n) {
Point tmp;
memset(ipcnt, , sizeof(ipcnt));
for (int i = ; i < n; i++) {
for (int j = ; j < i; j++) {
if (sgn(cross(ls[i].vec(), ls[j].vec()))) {
tmp = llint(ls[i], ls[j]);
if (onseg(tmp, ls[i])) ips[i][ipcnt[i]++] = tmp;
if (onseg(tmp, ls[j])) ips[j][ipcnt[j]++] = tmp;
}
}
}
vexcnt = ;
vex[vexcnt++] = trs;
memset(out, , sizeof(out));
for (int i = ; i < n; i++) {
ips[i][ipcnt[i]++] = ls[i].s;
ips[i][ipcnt[i]++] = ls[i].t;
sort(ips[i], ips[i] + ipcnt[i]);
for (int j = ; j < ipcnt[i]; j++) {
if (ips[i][j - ] == ips[i][j]) continue;
vex[vexcnt++] = mid(ips[i][j - ], ips[i][j]);
}
}
// cout << vexcnt << endl;
for (int i = ; i < vexcnt; i++) {
for (int j = n - ; j < n; j++) {
if (onseg(vex[i], ls[j])) {
out[i] = true;
break;
}
}
}
// for (int i = 0; i < vexcnt; i++) cout << out[i]; cout << endl;
return vexcnt;
} inline bool ssint(Line a, Line b) { return sgn(cross(a.s - b.s, a.t - b.s)) * sgn(cross(a.s - b.t, a.t - b.t)) <
&& sgn(cross(b.s - a.s, b.t - a.s)) * sgn(cross(b.s - a.t, b.t - a.t)) < ;} bool mat[N * N][N * N]; bool test(int i, int j, int n) {
Line tmp = Line(vex[i], vex[j]);
for (int a = ; a < n; a++)
if (onseg(vex[i], ls[a]) && onseg(vex[j], ls[a])) return false;
for (int a = ; a < n; a++)
if (ssint(tmp, ls[a])) return false;
return true;
} void makemat(int n, int m) {
memset(mat, , sizeof(mat));
for (int i = ; i < n; i++) {
for (int j = ; j < i; j++) {
if (test(i, j, m)) mat[i][j] = mat[j][i] = true;
}
}
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) cout << mat[i][j]; cout << endl;
// }
} int q[N * N * N];
bool vis[N * N]; int bfs(int n) {
int qh, qt;
memset(vis, , sizeof(vis));
qh = qt = ;
q[qt++] = ;
vis[] = true;
int cnt = ;
while (qh < qt) {
int sz = qt - qh;
cnt++;
for (int i = ; i < sz; i++) {
int cur = q[qh++];
// cout << cur << endl;
// cout << vex[cur].x << ' ' << vex[cur].y << endl;
for (int j = ; j < n; j++) {
if (vis[j] || !mat[cur][j]) continue;
q[qt++] = j;
vis[j] = true;
if (out[j]) return cnt;
}
}
}
return -;
} int main() {
// freopen("in", "r", stdin);
int n;
while (cin >> n) {
input(n);
int m;
makemat(m = makevex(n), n);
cout << "Number of doors = " << bfs(m) << endl;
}
return ;
}
——written by Lyon
poj 1066 Treasure Hunt (Geometry + BFS)的更多相关文章
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- POJ 1066 Treasure Hunt(相交线段&&更改)
Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...
- POJ 1066 - Treasure Hunt - [枚举+判断线段相交]
题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...
- poj 1066 Treasure Hunt
http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...
- POJ 1066 Treasure Hunt [想法题]
题目链接: http://poj.org/problem?id=1066 --------------------------------------------------------------- ...
- POJ 1066 Treasure Hunt (线段相交)
题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...
- POJ 1066 Treasure Hunt【线段相交】
思路:枚举四边墙的门的中点,与终点连成一条线段,判断与其相交的线段的个数.最小的加一即为答案. 我是傻逼,一个数组越界调了两个小时. #include<stdio.h> #include& ...
- POJ 1066 Treasure Hunt --几何,线段相交
题意: 正方形的房子,给一些墙,墙在区域内是封闭的,给你人的坐标,每穿过一道墙需要一把钥匙,问走出正方形需要多少把钥匙. 解法: 因为墙是封闭的,所以绕路也不会减少通过的墙的个数,还不如不绕路走直线, ...
- 简单几何(线段相交) POJ 1066 Treasure Hunt
题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...
随机推荐
- 2019.9.20 csp-s模拟测试48 反思总结
头疼,不说废话了,祝大家rp++. T1: 暴力枚举,n3. 枚举两个串开始匹配的位置,每一次尽量修改. #include<iostream> #include<cstdio> ...
- UVA1204 Fun Game
Fun Game https://odzkskevi.qnssl.com/8d698323a1e07d605cdeea708ee8a01d?v=1508703139 [题解] 不难发现如果一个串的原串 ...
- codevs1214 线段覆盖
1214 线段覆盖 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 给定x轴上的N(0<N<100)条线段, ...
- PHPStorm 批量选择,多光标同时编辑相同的内容
一直按Alt+J
- Vue2.0史上最全入坑教程(上)—— 搭建Vue脚手架(vue-cli)
ps: 想了解更多vue相关知识请点击VUE学习目录汇总 Vue作为前端三大框架之一截至到目前在github上以收获44,873颗星,足以说明其以悄然成为主流.16年10月Vue发布了2.x版本,经过 ...
- PHP配置环境中开启GD库
下配置好的PHP环境中,GD库不像windows那样可以直接用,而是默认关闭,需要把它打开,去到php.ini文件中 找到php_gd2.dll把分号去掉即可.(注:GD库跟绘制二维码等有关)
- web前端学习(四)JavaScript学习笔记部分(1)-- JavaScript基础教程
1.JavaScript基础教程 1.1.Javascript基础-介绍.实现.输出 1.1.1.JavaScript是互联网上最流行的脚本语言,这门语言可用于web和HTML,更可广泛用于服务端.p ...
- windows下MySQL 5.7.19版本sql_mode=only_full_group_by问题
用到GROUP BY 语句查询时出现 which is not functionally dependent on columns in GROUP BY clause; this is incomp ...
- c:if标签判断不为空和其他的值判断
今天用<c:if test=""></c:if>标签时 <c:if test="${sl.chc_status==1 }"> ...
- PHPCMS快速建站系列之在线留言
有两种方法 第一种方法: 利用留言板插件,在后台模板中,安装留言板插件使用,这里先不展开. 第二种方法: 表单向导的适用场合: 如果一个前台页面只是为了提交表单数据,那么就非常适合适用表单向导的功能, ...