位圖像素的顏色
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 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】
如果您有更好的算法,歡迎交流。