• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            POJ 1151 Atlantis 離散化+掃描線

            Problem Description
            There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.
             

            Input
            The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

            The input file is terminated by a line containing a single 0. Don’t process it.
             

            Output
            For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

            Output a blank line after each test case.
             

            Sample Input
            2
            10 10 20 20
            15 15 25 25.5
            0
             

            Sample Output
            Test case #1
            Total explored area: 180.00 
                題目的意思是給定n個(gè)矩形的2n個(gè)坐標(biāo),求矩形的覆蓋面積。如果開一個(gè)大的bool數(shù)組,將覆蓋過的部分更新為true,再從頭到尾掃描一遍,在坐標(biāo)范圍比較小的情況下,可以求解。但是如果坐標(biāo)x,y的取值范圍很大,比如[-10^8,10^8],用上面這個(gè)方法就不能求解了;而且坐標(biāo)還有可能是實(shí)數(shù),上面的方法就更加不可行了,需要尋找一種新的解法,就是下面要說到的“離散化”。
                注意到要表示一個(gè)矩形,只需要知道其2個(gè)頂點(diǎn)的坐標(biāo)就可以了(最左下,最右上)。可以用2個(gè)數(shù)組x[0...2n-1],y[0...2n-1]記錄下矩形Ri的2個(gè)坐標(biāo)(x1,y1),(x2,y2),然后將數(shù)組x[0...xn-1],y[0...2n-1]排序,為下一步的掃描線作準(zhǔn)備,這就是離散化的思想。這題還可以用線段樹做進(jìn)一步優(yōu)化,但是這里只介紹離散化的思想。
                看下面這個(gè)例子:有2個(gè)矩形(1,1),(3,3)和(2,2),(4,4)。如圖:
                圖中虛線表示掃描線,下一步工作只需要將這2個(gè)矩形覆蓋過的部分的bool數(shù)組的對應(yīng)位置更新為true,接下去用掃描線從左到右,從上到下掃描一遍,就可以求出矩形覆蓋的總面積。
                這個(gè)圖對應(yīng)的bool數(shù)組的值如下:
                1 1 0                       1 2 3
                1 1 1       <---->       4 5 6
                0 1 1                       7 8 9
             1 #include <iostream>
             2 #include <cmath>
             3 using namespace std;
             4 
             5 const int N = 101;
             6 const double eps = 1e-6;
             7 double ans,x[2*N],y[2*N];
             8 double pos[N][4];
             9 bool hash[2*N][2*N];
            10 
            11 int cmp(const void *a,const  void *b){
            12     double *aa = (double *)a;
            13     double *bb = (double *)b;
            14     if(fabs(*aa-*bb)<=eps) return 0;
            15     else if(*aa-*bb>0return 1;
            16     else return -1;
            17 }
            18 int main(){
            19     int i,j,k,n,x1,x2,y1,y2,ca=1;
            20     while(scanf("%d",&n),n){
            21         for(ans=i=k=0;i<n;i++,k+=2){
            22             scanf("%lf %lf %lf %lf",&pos[i][0],&pos[i][1],&pos[i][2],&pos[i][3]);
            23             x[k]=pos[i][0],y[k]=pos[i][1],x[k+1]=pos[i][2],y[k+1]=pos[i][3];
            24         }
            25         memset(hash,false,sizeof(hash));
            26         qsort(x,2*n,sizeof(x[0]),cmp);
            27         qsort(y,2*n,sizeof(y[0]),cmp);
            28         for(i=0;i<n;i++){
            29             for(k=0;fabs(x[k]-pos[i][0])>eps;k++); x1=k;
            30             for(k=0;fabs(y[k]-pos[i][1])>eps;k++); y1=k;
            31             for(k=0;fabs(x[k]-pos[i][2])>eps;k++); x2=k;
            32             for(k=0;fabs(y[k]-pos[i][3])>eps;k++); y2=k;
            33             for(j=x1;j<x2;j++for(k=y1;k<y2;k++)
            34                 hash[j][k]=true;
            35         }
            36         for(i=0;i<2*n-1;i++)
            37             for(j=0;j<2*n-1;j++)
            38                 ans+=hash[i][j]*(x[i+1]-x[i])*(y[j+1]-y[j]);            
            39         printf("Test case #%d\n",ca++);
            40         printf("Total explored area: %.2lf\n",ans);
            41         printf("\n");
            42     }
            43     return 0;
            44 }

            posted on 2009-04-26 19:43 極限定律 閱讀(719) 評論(0)  編輯 收藏 引用 所屬分類: ACM/ICPC

            <2009年4月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(10)

            隨筆分類

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久久久久国产a免费观看不卡| 无码人妻久久一区二区三区免费丨| 久久九九精品99国产精品| 精品久久久久香蕉网| 久久久中文字幕| | 久久久久久久久66精品片| 久久综合给合综合久久| 亚洲AV日韩AV天堂久久| 91精品国产91久久综合| 亚洲国产精品无码久久青草| 久久天天躁狠狠躁夜夜网站| 伊人久久免费视频| 无码国内精品久久人妻蜜桃| 99热成人精品免费久久| 影音先锋女人AV鲁色资源网久久 | 久久久国产打桩机| 国产精品VIDEOSSEX久久发布| 手机看片久久高清国产日韩| .精品久久久麻豆国产精品| 久久精品人妻一区二区三区| 人妻精品久久久久中文字幕一冢本| 日韩亚洲欧美久久久www综合网| 99久久夜色精品国产网站| 久久久WWW免费人成精品| 久久91综合国产91久久精品| 久久久久亚洲AV成人网人人网站| 久久99精品国产麻豆蜜芽| 波多野结衣中文字幕久久| 波多野结衣AV无码久久一区| 色偷偷91久久综合噜噜噜噜| 国产精品嫩草影院久久| 久久精品男人影院| 成人免费网站久久久| 72种姿势欧美久久久久大黄蕉| 久久综合亚洲欧美成人| 亚洲伊人久久精品影院| 99久久精品国产一区二区 | 国产精品久久久久久久久| 亚洲国产精品无码久久久蜜芽 | 成人久久免费网站|