位图像素的颜色
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.youkuaiyun.com/twlkyao/article/details/23471741 】
如果您有更好的算法,欢迎交流。