Xtreme 10.0 - Ellipse Art

這是 meelo 原創(chuàng)的 IEEEXtreme極限編程大賽題解

題目來源 第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:
(x1, y1) and (x2, y2) 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
Coordinate System for the Canvas

Coordinate system for the canvas.

Constraints

-100 ≤ x1, y1, x2, y2 ≤ 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

31-40 0 40 0 100110 50 90 50 100215 -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.

題目解析
計算幾何題,無法直接計算面積肺蔚。
使用撒點法/蒙特卡洛模擬练湿,計算橢圓內(nèi)的點數(shù)占總點數(shù)的比率即可闸翅。
在x軸和y軸以0.2為間隔取點可以滿足精度要求江耀。

程序
C++

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

double ellip[41][5];
int n;

double dist(double x1, double y1, double x2, double y2) {
    return sqrt(pow(x2-x1, 2) + pow(y2-y1, 2));
}

// 判斷點(x,y)是否在任意一個橢圓內(nèi)
bool check(double x, double y) {
    for(int i=0; i<n; i++) {
        if( dist(ellip[i][0], ellip[i][1], x, y) + dist(ellip[i][2], ellip[i][3], x, y) < ellip[i][4]) {
            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=0; j<n; j++) {
            for(int k=0; k<5; k++)
                cin >> ellip[j][k];
        }

        // 統(tǒng)計位于橢圓內(nèi)點的個數(shù)
        int count = 0;
        for(double x=-50; x<50; x+=0.2) {
            for(double y=-50; y<50; y+=0.2) {
                if(!check(x, y)) count++;
            }
        }
        cout << round(count / 2500.0) << "%" << endl;
    }
    return 0;
}

博客中的文章均為 meelo 原創(chuàng)萨咳,請務(wù)必以鏈接形式注明 本文地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末截粗,一起剝皮案震驚了整個濱河市垮刹,隨后出現(xiàn)的幾起案子达吞,更是在濱河造成了極大的恐慌,老刑警劉巖荒典,帶你破解...
    沈念sama閱讀 222,729評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件酪劫,死亡現(xiàn)場離奇詭異,居然都是意外死亡寺董,警方通過查閱死者的電腦和手機覆糟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來遮咖,“玉大人滩字,你說我怎么就攤上這事∮蹋” “怎么了麦箍?”我有些...
    開封第一講書人閱讀 169,461評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長陶珠。 經(jīng)常有香客問我挟裂,道長,這世上最難降的妖魔是什么背率? 我笑而不...
    開封第一講書人閱讀 60,135評論 1 300
  • 正文 為了忘掉前任话瞧,我火速辦了婚禮,結(jié)果婚禮上寝姿,老公的妹妹穿的比我還像新娘交排。我一直安慰自己,他們只是感情好饵筑,可當我...
    茶點故事閱讀 69,130評論 6 398
  • 文/花漫 我一把揭開白布埃篓。 她就那樣靜靜地躺著,像睡著了一般根资。 火紅的嫁衣襯著肌膚如雪架专。 梳的紋絲不亂的頭發(fā)上同窘,一...
    開封第一講書人閱讀 52,736評論 1 312
  • 那天,我揣著相機與錄音部脚,去河邊找鬼想邦。 笑死,一個胖子當著我的面吹牛委刘,可吹牛的內(nèi)容都是我干的丧没。 我是一名探鬼主播,決...
    沈念sama閱讀 41,179評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼锡移,長吁一口氣:“原來是場噩夢啊……” “哼呕童!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起淆珊,我...
    開封第一講書人閱讀 40,124評論 0 277
  • 序言:老撾萬榮一對情侶失蹤夺饲,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后施符,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體往声,經(jīng)...
    沈念sama閱讀 46,657評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,723評論 3 342
  • 正文 我和宋清朗相戀三年操刀,在試婚紗的時候發(fā)現(xiàn)自己被綠了烁挟。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,872評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡骨坑,死狀恐怖撼嗓,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情欢唾,我是刑警寧澤且警,帶...
    沈念sama閱讀 36,533評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站礁遣,受9級特大地震影響斑芜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜祟霍,卻給世界環(huán)境...
    茶點故事閱讀 42,213評論 3 336
  • 文/蒙蒙 一杏头、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧沸呐,春花似錦醇王、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春棘伴,著一層夾襖步出監(jiān)牢的瞬間寞埠,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人镰矿。 一個月前我還...
    沈念sama閱讀 49,304評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像怖糊,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子颇象,可洞房花燭夜當晚...
    茶點故事閱讀 45,876評論 2 361

推薦閱讀更多精彩內(nèi)容