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 
poj 1046 ——Color Me Less

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.

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有个映射,这个映射的距离最短,即为下面的公式的值最小

poj 1046 ——Color Me Less

然后,按照标准格式输出。

分析:思路就是想用一个数组将B集合到A集合的映射的所有距离全部存起来,然后按照从小到大的顺序排序。当然,要先用另外一个数组保存一下原始的数组,用于后续的判断。注意题目中的一句话If there are more than one color with the same smallest distance, please output the color given first in the color set.如果有多个同样的距离,按照顺序输出第一个就好。所以在输出的过程中要注意循环的终止。

做这种题目思路多是如此。

 /*暴力枚举,没啥好讲的*/
#include<cstdio>
#include<cmath>
#include<iostream> using namespace std; int a[],b[],c[],x,y,z,n;
double d,s; int main()
{
for(int i=;i<;i++)
scanf("%d%d%d",&a[i],&b[i],&c[i]);
do
{
scanf("%d%d%d",&x,&y,&z);
if(x==-&&y==-&&z==-) break;
d=(x-a[])*(x-a[])+(y-b[])*(y-b[])+(z-c[])*(z-c[]);
n=;
for(int i=;i<;i++)
{
s=(x-a[i])*(x-a[i])+(y-b[i])*(y-b[i])+(z-c[i])*(z-c[i]);
if(s<d)
{
n=i;
d=s;
}
}
printf("(%d,%d,%d) maps to (%d,%d,%d)\n",x,y,z,a[n],b[n],c[n]);
}while();
return ;
}
上一篇:C# OleDbConnection对特定部分Excel的数据读取


下一篇:[转]在Windows下编译ffmpeg完全手册