Hihocoder 1275 扫地机器人 计算几何
题意:
有一个房间的形状是多边形,而且每条边都平行于坐标轴,按顺时针给出多边形的顶点坐标
还有一个正方形的扫地机器人,机器人只可以上下左右移动,不可以旋转
问机器人移动的区域能不能覆盖整个房间
分析:
#include <cstdio>
#include <cmath>
#include <algorithm>
using std::abs;
using std::swap;
const int maxn = 1010;
struct Point
{
int x, y;
Point(int x = 0, int y = 0): x(x), y(y) {}
void read() { scanf("%d%d", &x, &y); }
Point operator - (const Point& t) const {
return Point(x - t.x, y - t.y);
}
};
int n, m;
Point p[maxn], dir[maxn]; //dir是边的单位方向向量
int angle[maxn]; //用叉积判断内外角,angle为正是外角,为负是内角
int sign(int x) { if(!x) return 0; return x > 0 ? 1 : -1; }
Point Normalize(Point A) {
return Point(sign(A.x), sign(A.y));
}
int Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; }
int Length(Point A) { if(A.x) return abs(A.x); return abs(A.y); }
int left, right, top, bottom; //机器人沿某条边扫过的矩形
//判断扫描区域是否与房间的某条边相交
bool intersect() {
for(int i = 1; i <= n; i++) {
if(p[i].x <= left && p[i+1].x <= left) continue;
if(p[i].x >= right && p[i+1].x >= right) continue;
if(p[i].y <= bottom && p[i+1].y <= bottom) continue;
if(p[i].y >= top && p[i+1].y >= top) continue;
return true;
}
return false;
}
int main()
{
int q; scanf("%d", &q);
while(q--) {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++) p[i].read();
p[n + 1] = p[1];
for(int i = 1; i <= n; i++) dir[i] = Normalize(p[i+1]-p[i]);
dir[0] = dir[n];
for(int i = 1; i <= n; i++) angle[i] = Cross(dir[i - 1], dir[i]);
angle[n + 1] = angle[1];
bool ok = true;
for(int i = 1; i <= n; i++) {
if(angle[i] < 0 && angle[i+1] < 0 && Length(p[i+1]-p[i]) < m) {
ok = false; break;
}
if(dir[i].x) {
int delta = m * dir[i].x;
left = p[i].x; right = p[i+1].x;
if(angle[i] > 0) left -= delta;
if(angle[i+1] > 0) right += delta;
top = p[i].y; bottom = top - delta;
} else {
int delta = m * dir[i].y;
top = p[i].y; bottom = p[i+1].y;
if(angle[i] > 0) top -= delta;
if(angle[i+1] > 0) bottom += delta;
left = p[i].x; right = left + delta;
}
if(left > right) swap(left, right);
if(bottom > top) swap(bottom, top);
if(intersect()) { ok = false; break; }
}
printf("%s\n", ok ? "Yes" : "No");
}
return 0;
}
Hihocoder 1275 扫地机器人 计算几何的更多相关文章
- [LeetCode] Robot Room Cleaner 扫地机器人
Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...
- 【BZOJ5318】[JSOI2018]扫地机器人(动态规划)
[BZOJ5318][JSOI2018]扫地机器人(动态规划) 题面 BZOJ 洛谷 题解 神仙题.不会.... 先考虑如果一个点走向了其下方的点,那么其右侧的点因为要被访问到,所以必定只能从其右上方 ...
- Java实现第十届蓝桥杯JavaC组第十题(试题J)扫地机器人
扫地机器人 时间限制: 1.0s 内存限制: 512.0MB 本题总分:25 分 [问题描述] 小明公司的办公区有一条长长的走廊,由 N 个方格区域组成,如下图所 示. 走廊内部署了 K 台扫地机器人 ...
- [LeetCode] 489. Robot Room Cleaner 扫地机器人
Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. Th ...
- LGP4588[JSOI2018]扫地机器人
题解 需要先说明一点东西: 1 同一副对角线方向相同,共有$gcd(n,m)$条不同的副对角线,机器人的行为是一个$gcd(n,m)$的循环:: 如果左上方是$(1,1)$,容易看出所有的路径是从左或 ...
- 489. Robot Room Cleaner扫地机器人
[抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...
- hihocoder 1582 : Territorial Dispute (计算几何)(2017 北京网络赛E)
题目链接 题意:给出n个点.用两种颜色来给每个点染色.问能否存在一种染色方式,使不同颜色的点不能被划分到一条直线的两侧. 题解:求个凸包(其实只考虑四个点就行.但因为有板子,所以感觉这样写更休闲一些. ...
- 除了ROS, 机器人定位导航还有其他方案吗?
利用ROS进行机器人开发,我想大多数企业是想借助ROS实现机器人的导航.定位与路径规划,它的出现大大降低了机器人领域的开发门槛,开发者无需向前人一样走众多弯路,掌握多种知识才能开始实现机器人设计的梦想 ...
- 除了ROS ,机器人自主定位导航还能怎么做?
博客转载自:https://www.leiphone.com/news/201609/10QD7yp7JFV9H9Ni.html 雷锋网(公众号:雷锋网)按:本文作者科技剪刀手,思岚科技技术顾问. 随 ...
随机推荐
- Linux 下安装使用 oh-my-zsh
Ubuntu 下安装 oh-my-zsh 安装 zsh sudo apt install zsh 安装 git(如果你的系统没有自带的话) sudo apt install git 安装 oh-my- ...
- powershell解决win10开始菜单和通知中心无法打开
然后通过 Ctrl + Shift + Esc 弹出任务管理器点击文件-->运行新任务 在打开的填写框里面输入 "powershell"同时勾选下方的"以管理员身份 ...
- 创建React工程:React工程模板
这是本人初学React做的学习笔记;讲的不是很深,只算是简单的进行介绍. 这是一个小系列.都是在同一个模板中搭建的,但是代码是不能正常执行的. >>index.js <!DOCTYP ...
- 如何使用ABAP代码反序列化JSON字符串成ABAP结构
假设我有这个JSON字符串如下图所示: 我的任务是解析出上图黑色方框里的几个字段,比如ObjectID, ETag, BuyerID, DateTime, ID, Name等等,把它们的值存储到对应A ...
- Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
backtracking and invariant during generating the parathese righjt > left (open bracket and cloas ...
- 使用 NetBackup 命令创建 Hyper-V 策略(命令创建其他策略也是如此)
Veritas NetBackup™ for Hyper-V 管理指南 Product(s): NetBackup (8.1) 使用 NetBackup 命令创建 Hyper-V 策略 本主题介绍如何 ...
- Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple Task Points
Design and Implementation of Global Path Planning System for Unmanned Surface Vehicle among Multiple ...
- cudaMemcpy2D介绍
cudaMemcpy2D( d_A, // 目的指针 d_pitch, // 目的pitch bmp1, // 源指针 sizeof(int)*2, // 源数据pitch sizeof(int)*2 ...
- C-net总结
SMB服务器信息块 DHCP动态主机配置协议 STMP简单邮件传输协议 POP(邮件协议) Gnutella 网络分析数据 nslookup DNS(域名系统) 请求注释(RFC)文件 ...
- using namespace std 是什么意思?
摘录CSDN上面大牛的回答简要意思就是使用标准库,想知道更清楚的继续读下面的. using namespace std 意思: using 和namespace都是C++的关键词. ...