Problem
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
1 #include <iostream> 2 #include <string> 3 #include <algorithm> 4 #include <map> 5 #include <vector> 6 #include <cstdio> 7 #include <cmath> 8 #include <cstring> 9 using namespace std; 10 11 int main() 12 { 13 int r[16], b[16], g[16]; 14 for(int i = 0; i < 16; i++) 15 { 16 scanf("%d %d %d", r + i, g + i, b + i); 17 } 18 int x, y, z; 19 while(scanf("%d %d %d", &x, &y, &z)) 20 { 21 if(x == -1 && y == -1 && z == -1) break; 22 double minD = (x - r[0]) * (x - r[0]) + (y - g[0])* (y - g[0]) + (z - b[0]) * (z - b[0]); 23 int id = 0; 24 for(int i = 1; i < 16; i++) 25 { 26 double D = (x - r[i]) * (x - r[i]) + (y - g[i]) * (y - g[i]) + (z - b[i]) * (z - b[i]); 27 if(minD > D) 28 { 29 minD = D; 30 id = i; 31 } 32 } 33 printf("(%d,%d,%d) maps to (%d,%d,%d) ", x, y, z, r[id], g[id], b[id]); 34 // cout << id << endl; 35 } 36 return 0; 37 }