poj 1046 ——Color Me Less
Color Me Less
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 32987 | Accepted: 16037 |
Description
A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this problem requires that you perform just such a mapping in a standard twenty-four bit RGB color space. The input consists of a target set of sixteen RGB color values, and a collection of arbitrary RGB colors to be mapped to their closest color in the target set. For our purposes, an RGB color is defined as an ordered triple (R,G,B) where each value of the triple is an integer from 0 to 255. The distance between two colors is defined as the Euclidean distance between two three-dimensional points. That is, given two colors (R1,G1,B1) and (R2,G2,B2), their distance D is given by the equation
Input
The input is a list of RGB colors, one color per line, specified as three integers from 0 to 255 delimited by a single space. The first sixteen colors form the target set of colors to which the remaining colors will be mapped. The input is terminated by a line containing three -1 values.
Output
For each color to be mapped, output the color and its nearest color from the target set.
If there are more than one color with the same smallest distance, please output the color given first in the color set.
If there are more than one color with the same smallest distance, please output the color given first in the color set.
Sample Input
0 0 0 255 255 255 0 0 1 1 1 1 128 0 0 0 128 0 128 128 0 0 0 128 126 168 9 35 86 34 133 41 193 128 0 128 0 128 128 128 128 128 255 0 0 0 1 0 0 0 0 255 255 255 253 254 255 77 79 134 81 218 0 -1 -1 -1
Sample Output
(0,0,0) maps to (0,0,0) (255,255,255) maps to (255,255,255) (253,254,255) maps to (255,255,255) (77,79,134) maps to (128,128,128) (81,218,0) maps to (126,168,9)
译文:
题意:先输入16种色彩,称之为集合A,然后再输入N种色彩,这些色彩为集合B,遇到色彩为输入为-1,-1,-1时,结束输入。然后,要求从B到A有个映射,这个映射的距离最短,即为下面的公式的值最小
然后,按照标准格式输出。
分析:思路就是想用一个数组将B集合到A集合的映射的所有距离全部存起来,然后按照从小到大的顺序排序。当然,要先用另外一个数组保存一下原始的数组,用于后续的判断。注意题目中的一句话If there are more than one color with the same smallest distance, please output the color given first in the color set.如果有多个同样的距离,按照顺序输出第一个就好。所以在输出的过程中要注意循环的终止。
做这种题目思路多是如此。
1 /*暴力枚举,没啥好讲的*/ 2 #include<cstdio> 3 #include<cmath> 4 #include<iostream> 5 6 using namespace std; 7 8 int a[20],b[20],c[20],x,y,z,n; 9 double d,s; 10 11 int main() 12 { 13 for(int i=0;i<16;i++) 14 scanf("%d%d%d",&a[i],&b[i],&c[i]); 15 do 16 { 17 scanf("%d%d%d",&x,&y,&z); 18 if(x==-1&&y==-1&&z==-1) break; 19 d=(x-a[0])*(x-a[0])+(y-b[0])*(y-b[0])+(z-c[0])*(z-c[0]); 20 n=0; 21 for(int i=0;i<16;i++) 22 { 23 s=(x-a[i])*(x-a[i])+(y-b[i])*(y-b[i])+(z-c[i])*(z-c[i]); 24 if(s<d) 25 { 26 n=i; 27 d=s; 28 } 29 } 30 printf("(%d,%d,%d) maps to (%d,%d,%d)\n",x,y,z,a[n],b[n],c[n]); 31 }while(1); 32 return 0; 33 }