• SPOJ AMR11B


    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.
    Time Limit: 3 s
    Memory Limit: 32 MBytes
    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

    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即可。

    4.   1 /*************************************************************************
        2     > File Name: trainaa.cpp
        3     > Author: 
        4     > Mail: 
        5     > Created Time: 2016年07月20日 星期三 12时07分14秒
        6  ************************************************************************/
        7 
        8 #include<iostream>
        9 #include<bits/stdc++.h>
       10 using namespace std;
       11 struct Point
       12 {
       13     int x,y;
       14     //Point(int x = 0,int y = 0) : x(x),y(y) {}
       15 }a,b,c;
       16 //typedef Point Vector;
       17 int vis[210][210];
       18 
       19 //Vector operator - (Point A,Point B) { return Vector(A.x- B.x,A.y - B.y);}
       20 int Cross(Point A,Point B,Point C)
       21 {
       22     return (B.x-A.x)*(C.y-A.y)-(C.x-A.x)*(B.y-A.y);
       23 }
       24 
       25 bool judge(int i,int j)
       26 {
       27     Point d;
       28     d.x = i;d.y = j;
       29     //Vector t;
       30     int x1,x2,x3;
       31     x1 = Cross(d,a,b);
       32     x2 = Cross(d,b,c);
       33     x3 = Cross(d,c,a);
       34     if(x1 >= 0 && x2 >= 0 && x3 >= 0) return true;
       35     if(x1 <= 0 && x2 <= 0 && x3 <= 0) return true;
       36     return false;
       37 }
       38 int main()
       39 {
       40     int t;
       41     cin >> t;
       42    // memset(vis,0,sizeof(vis));
       43     while(t--)
       44     {
       45         int n;
       46         char st;
       47         cin >> n;
       48         int x,y,r,l;
       49         int ans = 0;
       50         memset(vis,0,sizeof(vis));
       51         while(n--)
       52         {
       53             cin >> st;
       54             if(st == 'C')
       55             {
       56                 cin >> x >> y >> r;
       57                 x += 100; y+= 100;
       58                 for(int i = x-r; i<= x+r; i++)
       59                 {
       60                     for(int j = y-r;j <= y+r;j++)
       61                     {
       62                         if(!vis[i][j] &&((i-x)*(i-x)+(j- y)*(j-y) <= r*r))
       63                         {
       64                             ans++;
       65                             vis[i][j] = 1;
       66                         }
       67                     }
       68                 }
       69             }
       70             else if(st == 'S')
       71             {
       72                 cin >> x >> y >> l;
       73                 x += 100, y+= 100;
       74                 for(int i = x; i<= x+l; i++)
       75                 {
       76                     for(int j = y;j <= y+ l;j++)
       77                     {
       78                         if(!vis[i][j])
       79                         {
       80                             ans++;
       81                             vis[i][j] = 1;
       82                         }
       83                     }
       84                 }
       85             }
       86             else if(st == 'T')
       87             {
       88                // Point a,b,c;
       89                 cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;
       90                 a.x += 100;a.y+= 100;b.x += 100;b.y += 100;c.x += 100;c.y += 100;
       91                 //Vector A,B;
       92                 //A = b-a,B= c-a;
       93                 for(int i = 100; i<= 200 ;i++)
       94                 {
       95                     for(int j = 100; j<= 200; j++)
       96                       if(!vis[i][j] && judge(i,j))
       97                         {
       98                             ans++;
       99                             vis[i][j] = 1;
      100                         }
      101                  }
      102              }
      103          }
      104       cout << ans << endl;
      105     }
      106     return 0;
      107 }
  • 相关阅读:
    ThinkPHP中的CURD操作
    安卓自写Adapter
    安卓 报错 Check the Eclipse log for stack trace.
    web开发 关于src跳转
    javascript入门学习笔记2
    javascript入门学习笔记
    最全java的读写操作(转载)
    http请求的cookie
    java 安卓开发之文件的读与写
    转 安卓控件属性大全
  • 原文地址:https://www.cnblogs.com/PrayG/p/5697489.html
Copyright © 2020-2023  润新知