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的时候 ...
随机推荐
- oracle-PL/SQL2
一 存储过程 存储过程是SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.在 ORACLE SERVER上建立存储过程,可以被多个应用程序调用,可以向存储过程传递参数,也可以 ...
- union /union all/ intersect / minus
- git pull 总提示让输入merge 信息
在生产环境拉去代码的时候,总是出现了 .git/MERGE_MSG,很烦. 虽然每次可以通过输入 :q 命令,取消,然后完成拉取.但是这样就很影响效率.解决办法一: 欺骗自己法只要我没看见这个问题,这 ...
- mysql全连接
Oracle数据库支持full join,mysql是不支持full join的,但仍然可以同过左外连接+ union+右外连接实现 SELECT * FROM t1 LEFT JOIN t2 ON ...
- NOIP模拟 17.8.18
NOIP模拟17.8.18 A.小菜一碟的背包[题目描述]Blice和阿强巴是好朋友但萌萌哒Blice不擅长数学,所以阿强巴给了她一些奶牛做练习阿强巴有 n头奶牛,每头奶牛每天可以产一定量的奶,同时也 ...
- 利用Eclipse进行远程Debug
这项功能真的十分赞,当我不想写junit test,又想调试在实际环境中才能起作用的Java程序,远程debug真的是太好用了. 参数:java -jar -Xdebug -Xrunjdwp:tran ...
- Contentprovider 注册 启动简单流程
安装app时packagemanager 读取manixfest获取provider信息 存在数据库里流程:1.加载ActivityThread main方法,创建消息队列.ActivityThrea ...
- scroll 滚动到指定位置触发事件 and 点击一按钮/链接让页面定位在指定的位置
scroll 滚动到指定位置触发事件:$(function(){ $(window).scroll(function() { var s =$(window).scrollTop(); if (s&g ...
- CDH5.13.1安装
文件下载 Cloudera Manager 地址:http://archive.cloudera.com/cm5/cm/5/ 这里下载的是5.13.1的版本,https://archive.cloud ...
- jsp项目中整个项目没有问题但是servlet报错
项目没问题但是serverlet报错 项目右键 buildPath-->configure build path -->Myeclipse Library-->J2EE 1.3 Li ...