北大 acm 1046 Color Me Less 解题报告

Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17169 Accepted: 8069

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.

Sample Input

0 0 0255 255 2550 0 11 1 1128 0 00 128 0128 128 00 0 128126 168 935 86 34133 41 193128 0 1280 128 128128 128 128255 0 00 1 00 0 0255 255 255253 254 25577 79 13481 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个点中找出与目标点最近的那个点,如果有多个相同点则输出第一个。

代码如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
typedef struct color
{
long R,G,B;
}color;
long getDistance(color a,color b)
{
return (a.R-b.R)*(a.R-b.R)+(a.G-b.G)*(a.G-b.G)+(a.B-b.B)*(a.B-b.B);
}
int main()
{
color color16[100];
color colo;
long i,distance,num,len;
freopen("t.txt","rt",stdin);
for(i=0;i<16;i++)
cin>>color16[i].R>>color16[i].G>>color16[i].B;
while(cin>>colo.R>>colo.G>>colo.B)
{
distance=0;
if(colo.B==-1&&colo.G==-1&&colo.R==-1)
break;
distance = etDistance(color16[0],colo);
num=0;
for(i=1;i<16;i++)
{
len=getDistance(color16[i],colo);
if(len<distance) //切记这边不可取等号,看清题意是如有相等则输出第一个
{
distance=len;
num=i;
}
}
cout<<"("<<colo.R<<","<<colo.G<<","<<colo.B<<")"<<" maps to "<<"("<<color16[num].R<<","<<color16[num].G<<","<<color16[num].B<<")"<<endl;
}
return 0;
}