位圖像素的顏色——杭電攜程編程大賽 (預賽第二場)

位圖像素的顏色

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
有一個在位圖上畫出矩形程序,一開始位圖都被初始化爲白色(RGB顏色表示爲R=G=B=255)。該程序能夠按照順序繪出N個矩形。新繪製的矩形能夠覆蓋位圖上原有的顏色。程序執行完畢後,需要查詢M個點的顏色,輸出這些點的RGB值。 每組數據都是在初始化後開始繪製。
 

Input
第一行包含參數N和M,分別表示矩形個數和需要查詢的像素個數(1 ≤N, M≤ 1000 ); 剩下N行每行包含7個參數x1, y1, x2, y2, r, g, b,表示繪製一個(x1,y1),(x2,y2)爲頂點的矩形,填充顏色爲RGB(r, g, b),其中x1≤x2, y1≤y2數據在整型範圍;0≤ r,g,b ≤ 255; 最後M行分別包含參數X和Y,表示需要查詢的像素位置。 如果某行N=M=0就表示輸入結束。
 

Output
對於每個用例,按行輸出查詢的像素的RGB值,每行包含3個整數,分別表示RGB值。
 

Sample Input
1 2 0 0 2 3 127 196 200 1 2 3 0 2 3 8 16 32 64 0 255 128 8 48 32 64 255 0 0 12 47 13 48 14 64 0 0
 

Sample Output
127 196 200 255 255 255 0 255 128 255 0 0 255 0 0


    先來說下思路:

    這道題可以說是最簡單的題目了,因爲後邊的矩形直接覆蓋前邊的矩形的顏色,而不是疊加,這樣我們在查找相應點的顏色的時候就只需要找到最後覆蓋該點的矩形就可以了,如果某個點一直找到最開始的矩形都沒有被覆蓋,則該點的顏色爲白色。

    草圖如下:


    有了思路,程序應該就出來了,需要定義幾個結構體(struct),矩形結構體,點結構體,顏色結構體,方便數據操作。

    C代碼如下(時間:93ms,內存:240KB):

#include<stdio.h>

typedef struct { // The struct of color.
    int r;
    int g;
    int b;
}color;

typedef struct { // The struct of points.
    int x;
    int y;
}point;

typedef struct { // The struct of rectangle.
    point start;
    point end;
    color c;
}rec;

int main() {
    int n,m;
    int i,j;
    point pois[1000];
    rec recs[1000];
    while(scanf("%d %d", &n, &m) != EOF) {
        if(0 == n && 0 == m)
            return 0;
        for(i = 0; i < n; i++) { // Get the rectangles.
            scanf("%d %d %d %d %d %d %d", &recs[i].start.x, &recs[i].start.y, &recs[i].end.x, &recs[i].end.y, &recs[i].c.r, &recs[i].c.g, &recs[i].c.b);
        }
        
        for(j = 0; j < m; j++) { // Get the points.
            scanf("%d %d", &pois[j].x, &pois[j].y);
        }

        for(j = 0; j < m; j++) { // Check the points.
            for(i = n - 1; i >= 0; i--) {
 	        	if( pois[j].x >= recs[i].start.x && pois[j].x <= recs[i].end.x 
				 	&& pois[j].y >= recs[i].start.y && pois[j].y <= recs[i].end.y) { // In the rectangle.
		    		printf("%d %d %d\n", recs[i].c.r, recs[i].c.g, recs[i].c.b);
		    		break; // Get out of the loop.
				}
	    	}
	    	if(-1 == i) { // Not in any rectangle.
				printf("255 255 255\n");
	    	}
		}
   }    
   return 0;
}

    【本文出自:http://blog.csdn.net/twlkyao/article/details/23471741

    如果您有更好的算法,歡迎交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章