Save the Students

Time Limit:134MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

 

Description

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

题解:

  1. 因为数据范围不大,避免重复计数,使用一个数组直接标记法即可。

  2. 是否在圆内的判断使用到圆心的距离,遍历范围x±r,y±r,是否在正方形内直接判断,数据范围x->x+r,y->y+r,是否在三角形内部,需要用到一定的数学知识,一般有以下两种方法,一是利用面积来判断,对于三角形ABC,任取一个点M,连接M与ABC三个顶点,构成了三个三角形,三个三角形的和若等于原三角形的和,则在内部,否则在外部,但是利用海伦公式求面积时,浮点数会引起误差,一般推荐使用另一种方法,即是计算MA*MB、MB*MC、MC*MA的大小,若这三个值同号,那么在三角形的内部,异号在外部,本文代码使用第二种方法,避免了浮点数的运算  。

  3. 避免在遍历圆内的点时出现负数,例如(x,y)=(1,1),r=3,所有输入均加上100即可。

以下是代码:

#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[210][210];
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<=0 && t2 <=0 && t3 <=0)return 1;
if(t1>=0 && t2 >=0 && t3 >=0)return 1;
return 0;
}
void solve(){
char str[10];
int n,cnt=0;
memset(m,0,sizeof(m));
ss(n);
while(n--){
scanf("%s",str);
switch(str[0]){
case'C'://圆通过到圆心的距离判断
scanf("%d%d%d",&x,&y,&r);
x+=100;y+=100;//避免坐标为负值,输入全部加上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]=1;
}
break;
case'T'://三角形通过叉乘来判断
scanf("%d%d%d%d%d%d",&a.x,&a.y,&b.x,&b.y,&c.x,&c.y);
a.x+=100;b.x+=100;c.x+=100;a.y+=100;b.y+=100;c.y+=100;
fe(i,100,200)
fe(j,100,200)
if(!m[i][j] && intr(i,j)){
cnt++;
m[i][j]=1;
}
break;
case'S'://正方形的判断
scanf("%d%d%d",&x,&y,&r);
x+=100;y+=100;
fe(i,x,x+r)
fe(j,y,y+r)
if(!m[i][j]){
cnt++;
m[i][j]=1;
}
}
}
print(cnt);
}
int main(){
//write();
int T;
ss(T);
while(T--){
solve();
}
}

  

 

Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据的更多相关文章

  1. Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据

    Goblin Wars Time Limit:432MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Th ...

  2. Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据

    Array Diversity Time Limit:404MS     Memory Limit:0KB     64bit IO Format:%lld & %llu   Descript ...

  3. Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据

    Magic Grid Time Limit:336MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description Tha ...

  4. 【LeetCode】881. Boats to Save People 解题报告(Python)

    [LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...

  5. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  6. SPOJ - AMR11B

    题目链接:https://www.spoj.com/problems/AMR11B/en/ 题目大意就是要你求图形覆盖的格点数,标记每个图形里的未标记格点(包括边界),总标记数就是覆盖的总格点数. # ...

  7. SPOJ - AMR11B 判断是否在三角形 正方形 圆形内

    Hogwarts is under attack by the Dark Lord, He-Who-Must-Not-Be-Named. To protect the students, Harry ...

  8. codeforces A. Group of Students 解题报告

    题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...

  9. SPOJ QTREE 系列解题报告

    题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...

随机推荐

  1. SQL Server中的事务日志管理(5/9):完整恢复模式里的日志管理

    当一切正常时,没有必要特别留意什么是事务日志,它是如何工作的.你只要确保每个数据库都有正确的备份.当出现问题时,事务日志的理解对于采取修正操作是重要的,尤其在需要紧急恢复数据库到指定点时.这系列文章会 ...

  2. dock基本使用

    通过Docker源安装最新版本通过Docker源安装最新版本 要安装最新的 Docker 版本,首先需要安装 apt-transport-https 支持,之后通过添加源来安装.要安装最新的 Dock ...

  3. 常用的android弹出对话框

    我们在平时做开发的时候,免不了会用到各种各样的对话框,相信有过其他平台开发经验的朋友都会知道,大部分的平台都只提供了几个最简单的实现,如果我们想实现自己特定需求的对话框,大家可能首先会想到,通过继承等 ...

  4. 设计模式--外观(Facade)模式

    Insus.NET在去年有写过一篇<软件研发公司,外观设计模式(Facade)>http://www.cnblogs.com/insus/archive/2013/02/27/293606 ...

  5. Winform开发框架之权限管理系统改进的经验总结(4)--用户分级管理

    在实际的系统应用环境中,用户的分级管理一般也是比较常见的功能,小的业务系统可以不需要,但是一般涉及到集团.分子公司.或者是事业单位里面的各个处室或者某某局的人员管理,这些分级管理就显得比较必要,否则单 ...

  6. 查询分页的几种Sql写法

    查询分页的几种Sql写法 摘自:http://www.cnblogs.com/zcttxs/archive/2012/04/01/2429151.html 1.创建测试环境,(插入100万条数据大概耗 ...

  7. 自定义动画方法animate

    animate的使用方法:animate(params,speed,callback); 例子:animate({ right: "-=600px",height:"+= ...

  8. DIV嵌套垂直居中

    第一记住一点:父级相对定位,子级绝对定位 下面演示CSS样式: .父级DIV{ margin:0px auto; position:relative; border:2px solid #ff0000 ...

  9. vs2012中怎样设为起始页,怎样取消

    把你要设为起始页的文件右键选择为起始页,如下:

  10. 与众不同 windows phone 8.0 & 8.1 系列文章索引

    [源码下载] [与众不同 windows phone 7.5 (sdk 7.1) 系列文章索引] 与众不同 windows phone 8.0 & 8.1 系列文章索引 作者:webabcd ...