1.链接地址

https://vjudge.net/problem/POJ-1046

2.问题描述

 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 

输入样例

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

输出样例

(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)

3.解题思路

枚举比较即可

4.算法实现源代码

#include<iostream>
#include<math.h>
using namespace std;
 
struct RGB
{
    double r;
    double g;
    double b;
};
 
int main()
{
    RGB a[16];
    for(int i=0;i<16;i++)
    {
        cin>>a[i].r>>a[i].g>>a[i].b;
    }
    RGB b[100];
    RGB c[100];
    double e1,g1,b1,num,l;
    int cnt=0;
    while(cin>>e1>>g1>>b1&&(e1!=-1&&g1!=-1&&b1!=-1))
    {
        num=1000;
        b[cnt].r=e1;
        b[cnt].g=g1;
        b[cnt].b=b1;
        for(int i=0;i<16;i++)
        {
            l=sqrt(pow((a[i].r-e1),2)+pow((a[i].g-g1),2)+pow((a[i].b-b1),2));
            if(num>l)
            {
                num=l;
                c[cnt].r=a[i].r;
                c[cnt].g=a[i].g;
                c[cnt].b=a[i].b;
            }
        }
        cnt++;
    }
    for(int i=0;i<cnt;i++)
    {
        cout<<"("<<b[i].r<<","<<b[i].g<<","<<b[i].b<<") maps to ("<<c[i].r<<","<<c[i].g<<","<<c[i].b<<")"<<endl;
    }
    return 0;
 }