POJ1066 Treasure Hunt
嘟嘟嘟
题意看题中的图就行:问你从给定的点出发最少需要穿过几条线段才能从正方形中出去(边界也算)。
因为\(n\)很小,可以考虑比较暴力的做法。枚举在边界中的哪一个点离开的。也就是枚举四周的点\((x, y)\),并和起点\((x_0, y_0)\)连成线段,求和多少条线段相交。
但是因为点可以是实数,所以不知道怎么枚举。不过想想就知道,同一个区间中的点是等价的。因此我们只要枚举线段的端点即可。
至于判断线段相交,用叉积实现:对于线段\(AB\)和\(CD\),如果\((\overrightarrow{AB} \times \overrightarrow{AC}) * (\overrightarrow{AB} \times \overrightarrow{AD}) < 0\)且\((\overrightarrow{CD} \times \overrightarrow{CA}) * (\overrightarrow{CD} \times \overrightarrow{CB}) < 0\),则线段\(AB\)和\(CD\)相交。
(别忘了\(n = 0\)的情况……)
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 50;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int n;
struct Vec
{
db x, y;
db operator * (const Vec& oth)const
{
return x * oth.y - oth.x * y;
}
};
struct Point
{
db x, y;
Vec operator - (const Point& oth)const
{
return (Vec){x - oth.x, y - oth.y};
}
}a[maxn], b[maxn], P;
int solve(Point A, Point B)
{
Vec AB = B - A;
int ret = 0;
for(int i = 1; i <= n; ++i)
{
Vec AC = a[i] - A, AD = b[i] - A;
Vec CD = b[i] - a[i], CB = B - a[i];
if((AB * AC) * (AB * AD) < -eps && (CD * AC) * (CD * CB) > eps) ret++;
}
return ret;
}
int ans = INF;
int main()
{
n = read();
for(int i = 1; i <= n; ++i)
a[i].x = read(), a[i].y = read(), b[i].x = read(), b[i].y = read();
scanf("%lf%lf", &P.x, &P.y);
for(int i = 1; i <= n; ++i)
{
ans = min(ans, solve(a[i], P));
ans = min(ans, solve(b[i], P));
}
if(!n) ans = 0;
printf("Number of doors = ");
write(ans + 1), enter;
return 0;
}
POJ1066 Treasure Hunt的更多相关文章
- poj1066 Treasure Hunt【计算几何】
Treasure Hunt Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8192 Accepted: 3376 Des ...
- 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 ...
随机推荐
- js控制表格隔行变色
只是加载时候隔行变一个颜色,鼠标滑动上去时候没有变化 <table width="800" border="0" cellpadding="0& ...
- [转]深入Java单例模式
文章从 https://blog.51cto.com/devbean/203501 转载 问题 : doble-check 实现的单例模式有什么缺点 线程安全的单例写法应该是怎么样的 概述 在G ...
- java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/20 from pid=711, uid=10074 requires android.permission.READ_
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider ur ...
- SpringFramework中重定向
需求: 需要在两个@Controller之间跳转,实现重定向 解决: @PostMapping("/files/{path1}") public String upload(... ...
- 集合框架以及Map(一)
集合又称容器,编程思想中对其的定义为持有对象 我们在使用集合或者数组时得到最多的异常就是数组下表越界异常 Java.lang.ArrayIndexOutOfBoundsException这篇文章我们不 ...
- linux centOs中安装好数据库,客户端用plsql连接oracle
原创作品,转载请在文章显眼位置注明出处:https://www.cnblogs.com/sunshine5683/p/10030375.html 首先,回顾上篇 CenOs7安装oracle图文详细过 ...
- sprintf和sscanf
sprintf 一个可以将输入打印到字符串的函数,用法与printf差不多 可以参考这篇文章: http://blog.csdn.net/masibuaa/article/details/563488 ...
- IniHelper——INI操作辅助类
使用INI配置文件,简单便捷. 该辅助工具类为C#操作INI文件的辅助类,源码在某位师傅的基础上完善的来,因为忘记最初的来源了,因此不能提及引用,在此深感遗憾,并对贡献者表示感谢. using Sys ...
- Laravel基本使用
laravel一.简介二.运行环境要求 1.php 版本>=5.5.9 2.Mcrypt PHP扩展 php的加密扩展,提供多种加密算法 3.openssl扩展 对传输的数据进行加密 4.mbs ...
- JQuery 更改属性 JQ对象循环 each 全选反选 三元运算
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...