poj1066 Treasure Hunt【计算几何】
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8192 | Accepted: 3376 |
Description
An example is shown below:
Input
Output
Sample Input
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
Sample Output
Number of doors = 2
Source
题意:
一个正方形中有n面墙,告诉你一个宝藏所在的坐标。问最少要砸穿多少墙才能到达宝藏。
思路:
枚举宝藏坐标和所有的墙的端点构成一个线段。判断这个线段和多少墙相交,因为这个就是从某一个点进入的最短距离,相交的个数就是穿墙数。
这个端点就是最开始进入的点。
相当于那些墙的端点把外围墙划分成了很多区域,我们从某一个点进去。两个端点之间任何一个位置进去都是一样的,所以枚举端点就行了。
找一个最小值就行了。要注意有可能是从角进去的。
刚开始inter函数写错了。
#include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const double eps = 1e-;
int sgn(double x)
{
if(fabs(x) < eps)return ;
if(x < )return -;
else return ;
} struct point{
double x, y;
point(){}
point(double _x, double _y)
{
x = _x;
y = _y;
}
point operator -(const point &b)const
{
return point(x - b.x, y - b.y);
}
point operator +(const point &b)const
{
return point(x + b.x, y + b.y);
}
point operator /(double d)const
{
return point(x / d, y / d);
}
double operator ^(const point &b)const
{
return x * b.y - y * b.x;
}
double operator *(const point &b)const
{
return x * b.x + y * b.y;
}
//绕原点旋转B
void transXY(double b)
{
double tx = x, ty = y;
x = tx * cos(b) - ty * sin(b);
y = tx * sin(b) + ty * cos(b);
}
};
struct line{
point s, e;
double k;
line(){}
line(point _s, point _e)
{
s = _s;
e = _e;
k = atan2(e.y - s.y, e.x - s.x);
}
pair<int, point>operator &(const line &b)const
{
point res = s;
if(sgn((s - e) ^ (b.s - b.e)) == ){
if(sgn((s - b.e) ^ (b.s - b.e)) == ){
return make_pair(, res);
}
else return make_pair(, res);
}
double t = ((s - b.s) ^ (b.s - b.e)) / ((s - e) ^ (b.s - b.e));
res.x += (e.x - s.x) * t;
res.y += (e.y - s.y) * t;
return make_pair(, res);
}
}; double dist(point a, point b)
{
return sqrt((a - b) * (a - b));
} bool inter(line l1,line l2)
{
return
max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn((l2.s-l1.s)^(l1.e-l1.s))*sgn((l2.e-l1.s)^(l1.e-l1.s)) <= &&
sgn((l1.s-l2.s)^(l2.e-l1.s))*sgn((l1.e-l2.s)^(l2.e-l2.s)) <= ;
} const int maxn = ;
line l[maxn];
point treasure, p[maxn];
int n; int main()
{
l[] = line(point(, ), point(, ));
l[] = line(point(,), point(, ));
l[] = line(point(, ), point(, ));
l[] = line(point(, ), point(, ));
while(scanf("%d", &n) != EOF){
for(int i = ; i <= n + ; i++){
scanf("%lf%lf%lf%lf", &l[i].s.x, &l[i].s.y, &l[i].e.x, &l[i].e.y);
//[2 * i - 1] = l[i].s;
//p[2 * i] = l[i].e;
}
scanf("%lf%lf", &treasure.x, &treasure.y); int ans = inf;
for(int i = ; i <= ; i++){
line l1 = line(treasure, l[i].s), l2 = line(treasure, l[i].e);
int cnt1 = , cnt2 = ;
for(int j = ; j <= n + ; j++){
if(inter(l1, l[j])){
cnt1++;
//cout<<cnt1<<endl;
}
if(inter(l2, l[j])){
cnt2++;
//cout<<cnt1<<endl;
}
}
ans = min(ans, cnt1 + );
ans = min(ans, cnt2 + );
}
for(int i = ; i <= n + ; i++){
line l1 = line(treasure, l[i].s), l2 = line(treasure, l[i].e);
int cnt1 = , cnt2 = ;
for(int j = ; j <= n + ; j++){
if(inter(l1, l[j])){
cnt1++;
}
if(inter(l2, l[j])){
cnt2++;
}
}
ans = min(ans, cnt1);
ans = min(ans, cnt2);
}
/*for(int i = 1; i <= n * 2; i++){
line l1 = line(treasure, p[i]);
int cnt = 0;
for(int j = 1; j <= n; j++){
if(inter(l1, l[j])){
cnt++;
}
}
ans = min(ans, cnt);
//cout<<ans<<endl;
}
line l1 = line(treasure, point(0, 0));
int cnt = 0;
for(int i = 1; i <= n; i++){
if(inter(l1, l[i])){
cnt++;
}
}
ans = min(ans, cnt + 1);
cnt = 0;
l1 = line(treasure, point(0, 100));
for(int i = 1; i <= n; i++){
if(inter(l1, l[i])){
cnt++;
}
}
ans = min(ans, cnt + 1);
cnt = 0;
l1 = line(treasure, point(100, 0));
for(int i = 1; i <= n; i++){
if(inter(l1, l[i])){
cnt++;
}
}
ans = min(ans, cnt + 1);
cnt = 0;
l1 = line(treasure, point(100, 100));
for(int i = 1; i <= n; i++){
if(inter(l1, l[i])){
cnt++;
}
}
ans = min(ans, cnt + 1);*/
printf("Number of doors = %d\n", ans);
}
return ;
}
poj1066 Treasure Hunt【计算几何】的更多相关文章
- POJ1066 Treasure Hunt
嘟嘟嘟 题意看题中的图就行:问你从给定的点出发最少需要穿过几条线段才能从正方形中出去(边界也算). 因为\(n\)很小,可以考虑比较暴力的做法.枚举在边界中的哪一个点离开的.也就是枚举四周的点\((x ...
- zoj Treasure Hunt IV
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- POJ 1066 Treasure Hunt(线段相交判断)
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4797 Accepted: 1998 Des ...
- ZOJ3629 Treasure Hunt IV(找到规律,按公式)
Treasure Hunt IV Time Limit: 2 Seconds Memory Limit: 65536 KB Alice is exploring the wonderland ...
- POJ 1066 Treasure Hunt(相交线段&&更改)
Treasure Hunt 大意:在一个矩形区域内.有n条线段,线段的端点是在矩形边上的,有一个特殊点,问从这个点到矩形边的最少经过的线段条数最少的书目,穿越仅仅能在中点穿越. 思路:须要巧妙的转换一 ...
- Treasure Hunt
Treasure Hunt time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- zoj 3629 Treasure Hunt IV 打表找规律
H - Treasure Hunt IV Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- ZOJ 3626 Treasure Hunt I 树上DP
E - Treasure Hunt I Time Limit:2000MS Memory Limit:65536KB Description Akiba is a dangerous country ...
- 湖南大学ACM程序设计新生杯大赛(同步赛)I - Piglet treasure hunt Series 1
题目描述 Once there was a pig, which was very fond of treasure hunting. The treasure hunt is risky, and ...
随机推荐
- tomcat启动时出现了Failed to start component [StandardEngine[Catalina].StandardHost[localhost]]
https://blog.csdn.net/imjcoder/article/details/78725267 <dependency> <groupId>org.spring ...
- c++const关键字---15
原创博文,转载请标明出处--周学伟 http://www.cnblogs.com/zxouxuewei/ const是一个C++语言的限定符,它限定一个变量不允许被改变.使用const在一定程度上可以 ...
- IOS 基于APNS消息推送原理与实现(JAVA后台)--转
Push的原理: Push 的工作机制可以简单的概括为下图 图中,Provider是指某个iPhone软件的Push服务器,这篇文章我将使用.net作为Provider. APNS 是Apple ...
- web -- 前端访问后台跨区问题解决
package com.xindatai.ibs.web.filter; import java.io.IOException; import javax.servlet.Filter; import ...
- sublime自定义代码段
打开tools>developer>new snippet 默认代码 <snippet> <content><![CDATA[ Hello, ${1:this ...
- Kafka controller重设计
本文主要参考社区0.11版本Controller的重设计方案,试图给大家梳理一下Kafka controller这个组件在设计上的一些重要思考.众所周知,Kafka中有个关键组件叫controller ...
- Java API方式调用Kafka各种协议
众所周知,Kafka自己实现了一套二进制协议(binary protocol)用于各种功能的实现,比如发送消息,获取消息,提交位移以及创建topic等.具体协议规范参见:Kafka协议 这套协议的具 ...
- oracle如何链接到另外一个数据库DB_LINK
命令创建从一个库连接的另外一个库: create database link DB_XXX --创建连接的名字connect to db_xxx --数据库名identified by DB-XXX ...
- spring定时任务配置,以及不执行的解决办法
前几天,同事问了我一个问题,我告诉他用spring的定时任务解决,并给他配置了spring的定时任务.当时随便找了一个bean写了一段代码,验证定时任务正确执行后,就没再管,昨天下午,同事写代码的时候 ...
- Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempt
第一次用 Spring Starter Project 创建一个Spring应用时,POM 文件报错: Project build error: Non-resolvable parent POM f ...