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;
}

转载于:https://www.cnblogs.com/mrclr/p/9977665.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值