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 ...
随机推荐
- Solr搜索服务架构图
来源:http://www.open-open.com/lib/view/open1400576900081.html
- CentOS6.5菜鸟之旅:安装输入法(小呀小企鹅)
一.前言 假如在登录系统的时候语言选择了中文,那么是系统会自带ibus的中文输入法.但由于我打算用英文版,于是就被小企鹅输入法(FCITX)折腾了两个晚上. 二.检查系统编码 在bash中输入 loc ...
- iOS实现书架布局样式【一些电子书的首页】
本文实现了类似电子书首页,用来展示图书或小说的布局页面,书架列表[iPhone6模拟器],屏幕尺寸还没进行适配,只是做个简单的demo[纯代码实现方式] 实现采用的是UICollectionView和 ...
- C#多态--虚方法实现多态
1.虚方法提供一种默认实现,子类可以选择是否重写,如果不重写,那么就使用父类已经实现的方法.(重写可以改变方法的指针) 如果需要改变类型指针,那么需要做方法的重写: 1.如果子类方法是重写方法,那么系 ...
- P6 EPPM手动安装指南(Oracle数据库)(一)
P6 EPPM手动安装指南(Oracle数据库) P6 EPPM Manual Installation Guide (Oracle Database) 1. 内容... 1 1.1. ...
- Winform开发框架之权限管理系统功能介绍
权限管理系统的重要特性总结: 1) 高度集成的权限系统.独立模块,能快速整合使用.2) 符合权限的国际通用标准,基于RBAC(基于角色的访问控制)的角色权限控制.3) 多数据库架构支持,内置支持Sql ...
- WinForm公共控件
公共控件:1.Button:按钮 用户点击时触发事件 行为属性 Enabled -是否启用 Visible -是否隐藏2.CheckBox .CheckListBox - 复选框 复选框组 3.Com ...
- winform去掉右上角关闭按钮
一种方法是可以在窗体的属性面板将窗体的 ControlBox属性设置为false,或者在窗体的构造函数中这样写: public Form1() { InitializeComponent(); thi ...
- Python基础:开篇
一.概述 Python基础 是对 Python(2.7版本)基本语法的要点总结,主要参考了以下资料: Python核心编程(第二版) Learning Python (3rd Edition) Pyt ...
- Android开发总是难以入门
发现自己很难入门,是真的太难,还是自己主观拒绝.