这是 meelo 原创的 IEEEXtreme极限编程大赛题解

Xtreme 10.0 - Ellipse Art

题目来源 第10届IEEE极限编程大赛

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/ellipse-art

In IEEEXtreme 9.0, you met the famous artist, I.M. Blockhead. This year we want to introduce you to another famous artist, Ivy Lipps. Unlike I.M., Ivy makes her art by painting one or more ellipses on a canvas. All of her canvases measure 100 by 100 cms.

She needs your help. When she is done with the painting, she would like to know how much of the canvas is unpainted.

Input Format

The first line of input contains t, 1 ≤ t ≤ 8, which gives the number of test cases.

Each test case begins with a single integer, n, 1 ≤ n ≤ 40, which indicates the number of ellipses that Ivy has drawn.

The following n lines give the dimensions of each ellipse, in the following format:

x1 y1 x2 y2 r

Where:

  • (x1y1) and (x2y2) are positive integers representing the location of the foci of the ellipse in cms, considering the center of the canvas to be the origin, as in the image below.

  • r is a positive integer giving the length of the ellipse's major axis

You can refer to the Wikipedia webpage for background information on ellipses.

Coordinate system for the canvas.

Constraints

-100 ≤ x1y1x2y2 ≤ 100

r ≤ 200

r ≥ ((x2 - x1)2 + (y2 - y1)2)1/2 + 1

Note that these constraints imply that a given ellipse does not need to fall completely on the canvas (or even on the canvas at all).

Output Format

For each test case, output to the nearest percent, the percent of the canvas that is unpainted.

Note: The output should be rounded to the nearest whole number. If the percent of the canvas that is unpainted is not a whole number, you are guaranteed that the percent will be at least 10% closer to the nearer percent than it is from the second closest whole percent. Therefore you will not need to decide whether a number like 23.5% should be rounded up or rounded down.

Sample Input

3
1
-40 0 40 0 100
1
10 50 90 50 100
2
15 -20 15 20 50
-10 10 30 30 100

Sample Output

53%
88%
41%

Explanation

The ellipse in the first test case falls completely within the canvas, and it has an area of approximately 4,712 cm2. Since the canvas is 10,000 cm2, 53% of the canvas is unpainted.

In the second test case, the ellipse has the same size as in the first, but only one quarter of the ellipse is on canvas. Therefore, 88% of the canvas is unpainted.

In the final testcase, the ellipses overlap, and 41% of the canvas is unpainted.

题目解析

计算几何题,无法直接计算面积。

使用撒点法/蒙特卡洛模拟,计算椭圆内的点数占总点数的比率即可。

在x轴和y轴以0.2为间隔取点可以满足精度要求。

程序

C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std; double ellip[][];
int n; double dist(double x1, double y1, double x2, double y2) {
return sqrt(pow(x2-x1, ) + pow(y2-y1, ));
}

// 判断点(x,y)是否在任意一个椭圆内
bool check(double x, double y) {
for(int i=; i<n; i++) {
if( dist(ellip[i][], ellip[i][], x, y) + dist(ellip[i][], ellip[i][], x, y) < ellip[i][]) {
return true;
}
} return false;
} int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int T;
cin >> T;
while(T--) { cin >> n;
for(int j=; j<n; j++) {
for(int k=; k<; k++)
cin >> ellip[j][k];
}
// 统计位于椭圆内点的个数
int count = ;
for(double x=-; x<; x+=0.2) {
for(double y=-; y<; y+=0.2) {
if(!check(x, y)) count++;
}
}
cout << round(count / 2500.0) << "%" << endl;
}
return ;
}

博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

IEEEXtreme 10.0 - Ellipse Art的更多相关文章

  1. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  2. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  3. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  4. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  5. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  6. IEEEXtreme 10.0 - Playing 20 Questions with an Unreliable Friend

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Playing 20 Questions with an Unreliable Friend 题目来源 第1 ...

  7. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  8. IEEEXtreme 10.0 - N-Palindromes

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - N-Palindromes 题目来源 第10届IEEE极限编程大赛 https://www.hackerra ...

  9. IEEEXtreme 10.0 - Mysterious Maze

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Mysterious Maze 题目来源 第10届IEEE极限编程大赛 https://www.hacker ...

随机推荐

  1. python基础----列表生成式、生成器表达式

    结论: 1.把列表解析的[]换成()得到的就是生成器表达式 2.列表解析与生成器表达式都是一种便利的编程方式,只不过生成器表达式更节省内存 3.Python不但使用迭代器协议,让for循环变得更加通用 ...

  2. UESTC--1682

    原题链接: 分析:求最小周期的应用. #include<cstdio> #include<cstring> #include<cmath> #include< ...

  3. ZABBIX 3.0 配置监控MYSQL性能【OK】

    Zabbix3.0自带了MySQL插件来监控mysql数据库的模板,只需要配置好agent客户端,然后在web端给主机增加模板就行了. 参考:http://www.cnblogs.com/keving ...

  4. ubuntu 16.04 镜像下载

    下载地址: http://mirror.pnl.gov/releases/xenial/ Ubuntu 14.04.5 LTS (Trusty Tahr)http://releases.ubuntu. ...

  5. xcode禁用ARC(Automatic Reference Counting)

    Automatic Reference Counting,自动引用计数,即ARC,可以说是WWDC2011和iOS5所引入的最大的变革和最激动人心的变化.ARC是新的LLVM 3.0编译器的一项特性, ...

  6. [吴恩达机器学习笔记]13聚类K-means

    13.聚类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 13.1无监督学习简介 从监督学习到无监督学习 在一个典型的监督学习中,我们有一个有标签的训练集,我们的目标是找到能够区分正样本和负 ...

  7. [DeeplearningAI笔记]序列模型3.3集束搜索

    5.3序列模型与注意力机制 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.3 集束搜索Beam Search 对于机器翻译来说,给定输入的句子,会返回一个随机的英语翻译结果,但是你想要一 ...

  8. [DeeplearningAI笔记]卷积神经网络2.3-2.4深度残差网络

    4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [残差网络]--He K, Zhang X, Ren S, et al. Deep Residual Learni ...

  9. javascript 实现 A-star 寻路算法

    在游戏开发中,又一个很常见的需求,就是让一角色从A点走到B点,而我们期望所走的路是最短的,最容易想到的就是两点之间直线最短,我们可以通过勾股定理来求出两点之间的距离,但这个情况只能用于两点之间没有障碍 ...

  10. [USACO14MAR] Counting Friends

    题目描述 Farmer John's N cows (2 <= N <= 500) have joined the social network "MooBook". ...