1066 -- Treasure Hunt

  题意是,在一个金字塔中有一个宝藏,金字塔里面有很多的墙,要穿过墙壁才能进入到宝藏所在的地方。可是因为某些原因,只能在两个墙壁的交点连线的中点穿过墙壁。问最少要穿过多少墙壁才能得到宝藏。

  比较容易想到的一个办法就是直接用中点构图,然后判断点与点之间是否能够直接相连,最后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)的更多相关文章

  1. POJ 1066 Treasure Hunt(线段相交判断)

    Treasure Hunt Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4797   Accepted: 1998 Des ...

  2. POJ 1066 Treasure Hunt(相交线段&amp;&amp;更改)

    Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...

  3. POJ 1066 - Treasure Hunt - [枚举+判断线段相交]

    题目链接:http://poj.org/problem?id=1066 Time Limit: 1000MS Memory Limit: 10000K Description Archeologist ...

  4. poj 1066 Treasure Hunt

    http://poj.org/problem?id=1066 #include <cstdio> #include <cstring> #include <cmath&g ...

  5. POJ 1066 Treasure Hunt [想法题]

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

  6. POJ 1066 Treasure Hunt (线段相交)

    题意:给你一个100*100的正方形,再给你n条线(墙),保证线段一定在正方形内且端点在正方形边界(外墙),最后给你一个正方形内的点(保证不再墙上) 告诉你墙之间(包括外墙)围成了一些小房间,在小房间 ...

  7. POJ 1066 Treasure Hunt【线段相交】

    思路:枚举四边墙的门的中点,与终点连成一条线段,判断与其相交的线段的个数.最小的加一即为答案. 我是傻逼,一个数组越界调了两个小时. #include<stdio.h> #include& ...

  8. POJ 1066 Treasure Hunt --几何,线段相交

    题意: 正方形的房子,给一些墙,墙在区域内是封闭的,给你人的坐标,每穿过一道墙需要一把钥匙,问走出正方形需要多少把钥匙. 解法: 因为墙是封闭的,所以绕路也不会减少通过的墙的个数,还不如不绕路走直线, ...

  9. 简单几何(线段相交) POJ 1066 Treasure Hunt

    题目传送门 题意:从四面任意点出发,有若干障碍门,问最少要轰掉几扇门才能到达终点 分析:枚举入口点,也就是线段的两个端点,然后选取与其他线段相交点数最少的 + 1就是答案.特判一下n == 0的时候 ...

随机推荐

  1. WordPress不同分类使用不同的文章模板

    倡萌昨天分享的 Custom Post Template 和 Single Post Template 可以让你自定义每篇文章的文章模板,今天来说说WordPress不同分类使用不同的文章模板. 方法 ...

  2. Mysql的CMD操作

    一.MySQL登录和退出——在CMD模式操作 l  语法格式:mysql.exe –h主机名 –u用户名 –p密码 l  参数说明:   mysql.exe是mysql服务器的主应用程序.   -h代 ...

  3. CB Insights,201608月174家独角兽榜单出炉,上榜的33家中国公司都是谁?

    全球最新独角兽榜单出炉,上榜的33家中国公司都是谁? Monica  2016-09-15   近日,美国市场调研公司CB Insights发布了全球独角兽榜单(估值10亿美元以上),共有来自21个国 ...

  4. Total Number of Unicorn Companies: 188

    https://www.cbinsights.com/research-unicorn-companies   Total Number of Unicorn Companies: 188 Total ...

  5. LintCode_133 最长单词

    题目 给一个词典,找出其中所有最长的单词. 样例 在词典 { "dog", "google", "facebook", "inte ...

  6. Linux下下载安装jdk1.7和IDEA

    一.安装JDK1.7 准备: 到Oracle官网下载下载jdk1.7,参考博客 https://blog.csdn.net/H12KJGJ/article/details/79240984 官网地址: ...

  7. mysql的三种连接方式

    SQL的三种连接方式分为:左外连接.右外连接.内连接,专业术语分别为:LEFT JOIN.RIGHT JOING.INNER JOIN 内连接INNER JOIN:使用比较运算符来根据指定的连接的每个 ...

  8. PHP学习(数组)

    数组就是一个键值对组成的语言结构,键类似于酒店的房间号,值类似于酒店房间里存储的东西. PHP有两种数组:索引数组.关联数组. 索引和关联两个词都是针对数组的键而言的. 索引数组 先介绍下索引数组,索 ...

  9. C89标准库函数手册

    http://zh.cppreference.com/w/c 前言 ANSI C(C89)标准库函数共有15个头文件.这15个头文件分别为: 1.<assert.h>            ...

  10. MUI - 封装localStorage与plus.storage

    MUI - 封装localStorage与plus.storage 2.0版本 在使用plus.storage频繁地存取数据时,可以感觉到明显的卡顿,而且很耗内存, 在切换到localstorage时 ...