Color Me Less
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 31681 | Accepted: 15416 |
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个RGB的点,然后接着询问N个RGB点,求解询问的RGB点和16个中哪一个最近,公式已给,枚举就好,不过有个地方要小心,如果用pow的同学,别忘了它是有重载的,所以要明确你用的是哪个,有double,float和long double三个功能,最简单不会错的方法就是定义变量的时候都给它定义成double好了。睡觉去了。。
AC代码:
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
double a[17][4]; //16个,3位
const double inf = 0xfffffff - 0.1;
int main()
{
int i;
int rank;
double d;
double minx;
double e,f,g;
//freopen("1.txt","r",stdin);
while(cin>>a[0][0]>>a[0][1]>>a[0][2])
{
for(i=1;i<16;i++)
{
cin>>a[i][0]>>a[i][1]>>a[i][2];
}
while(cin>>e>>f>>g && e != -1 && f != -1 && g != -1)
{
minx = inf;
for(i=0;i<16;i++)
{
d = sqrt(pow(e-a[i][0],2)+ pow(f-a[i][1],2) + pow(g-a[i][2],2));
//cout<<d<<endl;
if(d < minx)
{
minx = d;
rank = i;
//cout<<rank<<endl;
}
}
cout<<"("<<e<<","<<f<<","<<g<<")"<<" "<<"maps to ("<<a[rank][0]<<","<<a[rank][1]<<","<<a[rank][2]<<")"<<endl;
}
}
return 0;
}