SPOJ - AMR11B 判断是否在三角形 正方形 圆形内
Hogwarts is under attack by the Dark Lord, He-Who-Must-Not-Be-Named. To protect the students, Harry Potter must cast protective spells so that those who are protected by the spells cannot be attacked by the Dark Lord.
Harry has asked all the students to gather on the vast quidditch sports field so that he can cast his spells. The students are standing in a 2D plane at all grid points - these are the points (x,y) such that both x and y are integers (positive, negative or 0). Harry's spell can take the shapes of triangle, circle or square, and all who fall within that shape (including its boundaries) are protected.
Given the types of spells and the details regarding where Harry casts the spell, output the number of people saved by Harry's spells.
Input (STDIN):
The first line contains the number of test cases T. T test cases follow.
Each case contains an integer N on the first line, denoting the number of spells Harry casts. N lines follow, each containing the description of a spell.
If the ith spell is a triangle, then the line will be of the form "T x1 y1 x2 y2 x3 y3". Here, (x1,y1), (x2,y2) and (x3,y3) are the coordinates of the vertices of the triangle.
If the ith spell is a circle, then the line will be of the form "C x y r". Here, (x,y) is the center and r is the radius of the circle.
If the ith spell is a square, then the line will be of the form "S x y l". Here, (x,y) denotes the coordinates of the bottom-left corner of the square (the corner having the lowest x and y values) and l is the length of each side.
Output (STDOUT):
Output T lines, one for each test case, denoting the number of people Harry can save.
Constraints:
All numbers in the input are integers between 1 and 50, inclusive.
The areas of all geometric figures will be > 0.
Sample Input:
4
1
C 5 5 2
1
S 3 3 4
1
T 1 1 1 3 3 1
3
C 10 10 3
S 9 8 4
T 7 9 10 8 8 10
Sample Output:
13
25
6
34
是否在圆内的判断使用到圆心的距离,遍历范围x±r,y±r,是否在正方形内直接判断,数据范围x->x+r,y->y+r,是否在三角形内部,一般有以下两种方法,一是利用面积来判断,对于三角形ABC,任取一个点M,连接M与ABC三个顶点,构成了三个三角形,三个三角形的和若等于原三角形的和,则在内部,否则在外部,但是利用海伦公式求面积时,浮点数会引起误差,一般推荐使用另一种方法,即是计算MA*MB、MB*MC、MC*MA的大小,若这三个值同号,那么在三角形的内部,异号在外部
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <cstdio>
using namespace std; #define ss(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define ff(i,s,e) for(int i=s;i<e;i++)
#define fe(i,s,e) for(int i=s;i<=e;i++)
#define write() freopen("1.in","r",stdin) int m[][];
struct Point{
int x,y;
}a,b,c,d;
int x,y,r;
int calmul(Point aa,Point bb,Point cc){ // 计算向量AB 与向量AC的叉乘
return (bb.x-aa.x)*(cc.y-aa.y)-(cc.x-aa.x)*(bb.y-aa.y);
}
bool intr(int i,int j){//如果DA*DB、DB*DC、DC*DA同号,则在三角形内部
int t1,t2,t3;
d.x = i;d.y = j;
t1 = calmul(d,a,b);
t2 = calmul(d,b,c);
t3 = calmul(d,c,a);
if(t1<= && t2 <= && t3 <=)return ;
if(t1>= && t2 >= && t3 >=)return ;
return ;
}
void solve(){
char str[];
int n,cnt=;
memset(m,,sizeof(m));
ss(n);
while(n--){
scanf("%s",str);
switch(str[]){
case'C'://圆通过到圆心的距离判断
scanf("%d%d%d",&x,&y,&r);
x+=;y+=;//避免坐标为负值,输入全部加上100
fe(i,x-r,x+r)
fe(j,y-r,y+r)
if(!m[i][j]&& ((x-i)*(x-i)+(y-j)*(y-j)<=r*r)){
cnt++;
m[i][j]=;
}
break;
case'T'://三角形通过叉乘来判断
scanf("%d%d%d%d%d%d",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
a.x+=;b.x+=;c.x+=;a.y+=;b.y+=;c.y+=;
fe(i,,)
fe(j,,)
if(!m[i][j] && intr(i,j)){
cnt++;
m[i][j]=;
}
break;
case'S'://正方形的判断
scanf("%d%d%d",&x,&y,&r);
x+=;y+=;
fe(i,x,x+r)
fe(j,y,y+r)
if(!m[i][j]){
cnt++;
m[i][j]=;
}
}
}
print(cnt);
}
int main(){
//write();
int T;
ss(T);
while(T--){
solve();
}
}
SPOJ - AMR11B 判断是否在三角形 正方形 圆形内的更多相关文章
- CSS 三角形与圆形
1. 概述 1.1 说明 通过边框(border)的宽度与边框圆角(border-radius)来设置所需的三角形与圆形. 1.2 边框 宽高都为0时,边框设置的不同结果也不同,如下: 1.四个边框都 ...
- POJ 2986 A Triangle and a Circle(三角形和圆形求交)
Description Given one triangle and one circle in the plane. Your task is to calculate the common are ...
- [fzu 2273]判断两个三角形的位置关系
首先判断是否相交,就是枚举3*3对边的相交关系. 如果不相交,判断包含还是相离,就是判断点在三角形内还是三角形外.两边各判断一次. //http://acm.fzu.edu.cn/problem.ph ...
- 叉积_判断点与三角形的位置关系 P1355 神秘大三角
题目描述 判断一个点与已知三角形的位置关系. 输入输出格式 输入格式: 前三行:每行一个坐标,表示该三角形的三个顶点 第四行:一个点的坐标,试判断该点与前三个点围成三角形的位置关系 (详见样例) 所有 ...
- 如何实现将拖动物体限制在某个圆形内--实现方式vue3.0
如何实现蓝色小圆可拖动,并且边界限制在灰色大圆内?如下所示 需求源自 业务上遇到一个组件需求,设计师设计了一个"脸型整合器"根据可拖动小圆的位置与其它脸型的位置关系计算融合比例 如 ...
- JS判断日期是否在同一个星期内,和同一个月内
今天要用到判断日期是否在同一个星期内和是否在同一个月内,在网上找了好一会儿也没找到合适的,然后自己写了一个方法来处理这个问题,思路就不详细介绍了,直接附上代码,自己测试了一下 没有问题,若有问题请在评 ...
- java/c# 判断点是否在多边形区域内
java/c# 判断点是否在多边形区域内 年06月29日 ⁄ 综合 ⁄ 共 1547字 ⁄ 字号 小 中 大 ⁄ 评论关闭 最近帮别人解决了一个问题,如何判断一个坐标点,是否在多边形区域内(二维). ...
- 百度地图WEB端判断用户是否在网格范围内
在pc端设置商家的配送范围,用户在下单时,根据用户设置的配送地点判断是否在可配送范围内,并给用户相应的提示. 下面说下我的实现思路: 1.用百度地图在PC端设置配送范围,可拖拽选择 2.根据用户设置的 ...
- C# 判断点是否在矩形框内
欢迎加群交流 QQ群 830426796 用 System.Drawing.Drawing2D.GraphicsPath 和 Region 类联合起来,然后用 Region.IsVisible(poi ...
随机推荐
- AWK---linux系统三剑客(三)
grep .sed.awk被称为linux中的"三剑客". grep 更适合单纯的查找或匹配文本 sed 更适合编辑匹配到的文本 awk 更适合格式化文本,对文本进行较复杂格式 ...
- Impacket网络协议工具包介绍
转载自FreeBuf.COM Impacket是一个Python类库,用于对SMB1-3或IPv4 / IPv6 上的TCP.UDP.ICMP.IGMP,ARP,IPv4,IPv6,SMB,MSRPC ...
- 读rfc HTTP 协议
这是IETF ( 国际互联网工程任务组(The Internet Engineering Task Force,简称 IETF))制定的协议之一. 互联网工程任务组,成立于1985年底,是全球互联网最 ...
- awk工具的基本用法
awk文本过滤的基本用法 1)基本操作方法 格式:awk [选项] '[条件]{指令}' 文件 其中,print 是最常用的编辑指令:若有多条编辑指令,可用分号分隔. Awk过滤数据时支持仅打印某一列 ...
- Paid consultation (currently free 20190901)
Master of Electrical Engineering, Chongqing University Range:01 College entrance examination, major, ...
- ssh速度慢
原因:DNS解析默认开了 解决方法: vi /etc/ssh/sshd_config, 将#UseDNS yes 改为 : UseDNS no 然后重启ssh服务即可. PS: 其实不好,因为毕竟 ...
- sleep(0) 的作用
思考下面这两个问题: 假设现在是 2019-5-18 12:00:00.00,如果我调用一下 Thread.Sleep(1000) ,在 2019-5-18 12:00:01.00 的时候,这个线程会 ...
- 配置了Ubuntu环境变量,系统启动不了
修改了etc/init.d/rcS文件后重启后Ubuntu起不来了, 开启按shift+e或者直接选择,进入恢复模式 进入root shell 执行这个命令 可以有写入权限,重新挂载 mount - ...
- Docker下mysql容器开启binlog日志(保留7天)
现有需求开启用Docker容器启动的mysql数据库的binlog,以作为 日志记录 和 数据恢复,我们了解了MySQL的binlog日志的开启方式以及binlog日志的一些原理和常用操作,我们知道, ...
- HttpServletResponse对象(转)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...