Spring-2-B Save the Students(SPOJ AMR11B)解题报告及测试数据
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
题解:
因为数据范围不大,避免重复计数,使用一个数组直接标记法即可。
是否在圆内的判断使用到圆心的距离,遍历范围x±r,y±r,是否在正方形内直接判断,数据范围x->x+r,y->y+r,是否在三角形内部,需要用到一定的数学知识,一般有以下两种方法,一是利用面积来判断,对于三角形ABC,任取一个点M,连接M与ABC三个顶点,构成了三个三角形,三个三角形的和若等于原三角形的和,则在内部,否则在外部,但是利用海伦公式求面积时,浮点数会引起误差,一般推荐使用另一种方法,即是计算MA*MB、MB*MC、MC*MA的大小,若这三个值同号,那么在三角形的内部,异号在外部,本文代码使用第二种方法,避免了浮点数的运算 。
避免在遍历圆内的点时出现负数,例如(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)解题报告及测试数据的更多相关文章
- Spring-2-J Goblin Wars(SPOJ AMR11J)解题报告及测试数据
Goblin Wars Time Limit:432MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Th ...
- Spring-2-H Array Diversity(SPOJ AMR11H)解题报告及测试数据
Array Diversity Time Limit:404MS Memory Limit:0KB 64bit IO Format:%lld & %llu Descript ...
- Spring-2-A Magic Grid(SPOJ AMR11A)解题报告及测试数据
Magic Grid Time Limit:336MS Memory Limit:0KB 64bit IO Format:%lld & %llu Description Tha ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- spring spring data jpa save操作事务
整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...
- SPOJ - AMR11B
题目链接:https://www.spoj.com/problems/AMR11B/en/ 题目大意就是要你求图形覆盖的格点数,标记每个图形里的未标记格点(包括边界),总标记数就是覆盖的总格点数. # ...
- SPOJ - AMR11B 判断是否在三角形 正方形 圆形内
Hogwarts is under attack by the Dark Lord, He-Who-Must-Not-Be-Named. To protect the students, Harry ...
- codeforces A. Group of Students 解题报告
题目链接:http://codeforces.com/problemset/problem/357/A 题目意思:将一堆人分成两组:beginners 和 intermediate coders .每 ...
- SPOJ QTREE 系列解题报告
题目一 : SPOJ 375 Query On a Tree http://www.spoj.com/problems/QTREE/ 给一个树,求a,b路径上最大边权,或者修改a,b边权为t. #in ...
随机推荐
- [数据库]sql之行顺序
这个文章主要是防止我忘了sql的执行顺序,解释的东西我都没怎么看懂.数据库渣如我- 逻辑查询处理阶段简介 FROM:对FROM子句中的前两个表执行笛卡尔积(Cartesian product)(交叉联 ...
- Android 常见工具类封装
1,MD5工具类: public class MD5Util { public final static String MD5(String s) { char hexDigits[] = { '0' ...
- python网络编程socket /socketserver
提起网络编程,不同于web编程,它主要是C/S架构,也就是服务器.客户端结构的.对于初学者而言,最需要理解的不是网络的概念,而是python对于网络编程都提供了些什么模块和功能.不同于计算机发展的初级 ...
- 说说jsonp
什么是jsonp jsonp充其量只能说是一种"方法".它可以让页面从其他域中获取资料. 首先要知道的是同源策略,在javascript中使用http请求(ajax)是会受到同 ...
- 【Coding地址汇总】2016年沈航软工学生项目主页
同学们把自己的coding主页链接贴在评论里,要求格式"班号+学号+coding主页链接",如: "1301+13061193 + https://coding.net/ ...
- 使用SQLite数据库和Access数据库的一些经验总结
在我的<Winform开发框架>中,可使用多种数据库作为程序的数据源,除了常规的Oracle数据库.SqlServer.MySql数据库,其中还包括了SQLite数据库.Access数据库 ...
- 【iOS】编译静态库
与java和.net一样,objc也由类库的概念,不过在在objc上一般叫库,库表示程序代码集合,可以共享给其他程序使用,库是编译后的二进制文件,因此不能看到源代码,多用于一些开放sdk(如百度地图s ...
- 调试报“The source file is different from when the module was built.”问题的解决
It is related to the checksums which is used to ensure that you are stepping in matching source. You ...
- [CLR via C#]9. 参数
一.可选参数和命名参数 在设计一个方法的参数时,可为部分或全部参数分配默认值.然后,调用这些方法的代码时可以选择不指定部分实参,接受默认值.此外,调用方法时,还可以通过指定参数名称的方式为其传递实参. ...
- asp.net mvc UpdateModel 更新对象后出现null
在用asp.net mvc 4.0做项目的时候遇到的这种情况 在填写表单的时候,有一些表单没有填写,留空,然后直接post 提交表单,action中用UpdateModel 来更新model, 结果发 ...