ZOJ_1067_Color Me Less

来源:互联网 发布:js中新建json对象 编辑:程序博客网 时间:2024/05/18 23:29
Color Me Less

Time Limit: 2 Seconds      Memory Limit: 65536 KB

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

The input file 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.


Example

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

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)


Source: Greater New York 2001


      鉴于迫切提高英语水平,针对以后的题目,将给出全文翻译,虽然这大大增加了做题需要的时间,但是对自己的提高也有帮助,特此说明,与君共勉!不完善之处请多多指摘。


翻译:

                                                                       程序设计(这题目太难翻,直译总感觉不对,这样翻译取自有道词典。。)

     (这一段引用自他人,特此说明)

      缩色就是将一组不连续的颜色绘制成一个更少颜色的图,解决办法是你需要在24位标准颜色空间里完成这个绘制。输入的值由固定的16RGB颜色值,以及与规定颜色最接近的任何颜色的集合。为了利于我们使用,一个RGB颜色定义为一定的三个数的组(RGB),其中三个数都是0到255之间的整数。两个颜色之间的距离定义为两个三维点之间的欧几里得距离。这就是说,给两个颜色(R1,G1,B1)和 (R2,G2,B2),他们的距离由方程式给出。 
     输入
     输入是一组RGB颜色,每个颜色占据一行,由3个0-255的整型来指明并且3个整型由单个空格来划分开。首先输入的16组颜色是余下的将要绘制的颜色的目标颜色组。输入由包含3个-1值的一行来终止。
     输出
    对于每个要被绘制的颜色,输出他们的颜色以及离他们最近的颜色。




题意和思路:不得不说。。ZOJ把简单题用英语复杂化的水平还是不错的。。一定要加上个缩色的背景。。题目的意思就是先给出16个颜色,再给出5个颜色,从16个颜色当中找到离这5个颜色最近的颜色,打印出来就可以了。


易错点:这个错误我觉得我找出来是运气好。。要不然没这么轻易看出来,之前测试全部正确但是wrong answer其实有个小细节要注意,就是对于有多个目标点和输入的点的距离是相同的时候,要取在前面的目标点


Sample Program Here

#include<iostream>#include<math.h>using namespace std;struct RGB{   float r;   float g;   float b;};void calculate(RGB input[],int len1,RGB target[],int len2){   float distance=0,temp;   RGB nearest[100]; //记录每个输出对应的最近的点      for(int i=0;i<len1;i++){   //计算一次距离公式,初始化distance   distance=pow(input[i].r-target[0].r,2)+pow(input[i].g-target[0].g,2)+pow(input[i].b-target[0].b,2);     nearest[i].r=target[0].r;  nearest[i].g=target[0].g;  nearest[i].b=target[0].b;   for(int j=1;j<len2;j++){      temp=pow(input[i].r-target[j].r,2)+pow(input[i].g-target[j].g,2)+pow(input[i].b-target[j].b,2);  if(temp<distance){  distance=temp;      nearest[i].r=target[j].r;  nearest[i].g=target[j].g;  nearest[i].b=target[j].b;  }   }   //离第2个颜色距离最近的点得到       cout<<'('<<input[i].r<<','<<input[i].g<<','<<input[i].b<<") maps to "   <<'('<<nearest[i].r<<','<<nearest[i].g<<','<<nearest[i].b<<')'<<endl;   }}int main(){  RGB target[16];  RGB in[100];  int i=0;  //初始化目标颜色组  for(i=0;i<16;i++){     cin>>target[i].r>>target[i].g>>target[i].b;  }  i=0;  while(1){     cin>>in[i].r>>in[i].g>>in[i].b; if(in[i].r==-1&&in[i].g==-1&&in[i].b==-1){    calculate(in,i,target,16);  //结束则进行计算break; } i++;  }  return 0;}