ZOJ_1067_Color Me Less
来源:互联网 发布:js中新建json对象 编辑:程序博客网 时间:2024/05/18 23:29
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
鉴于迫切提高英语水平,针对以后的题目,将给出全文翻译,虽然这大大增加了做题需要的时间,但是对自己的提高也有帮助,特此说明,与君共勉!不完善之处请多多指摘。
翻译:
程序设计(这题目太难翻,直译总感觉不对,这样翻译取自有道词典。。)
(这一段引用自他人,特此说明)
题意和思路:不得不说。。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;}
- ZOJ_1067_Color Me Less
- 1046 Color Me Less
- 1067 Color Me Less
- ZOJ1067 Color Me Less
- JOJ1090: Color Me Less
- 1046 Color Me Less
- ZOJ1067Color Me Less
- 1046 Color Me Less
- acm1067-Color Me Less
- 1067_Color Me Less
- poj_1046 Color Me Less
- poj1046 color me less
- AOJ186 Color Me Less
- POJ1046;Color me less
- poj1046--Color Me Less
- Color Me Less
- Color Me Less
- 1046 Color Me Less
- Android的版本(Version)和API-level的对应关系
- C++中头文件(.h)和源文件(.cpp)都应该写些什么
- 漫谈中国各省未来发展趋势(2011版)
- 10.26事件
- 转载过来的反调试方法
- ZOJ_1067_Color Me Less
- android 视频播放器
- Ant问题:warning: 'includeantruntime' was not set
- fedora 中文乱码 各种问题
- android全屏的三种办法
- 绕过WAF进行sql注入
- 倾诉孤独真是伤感日志:我对你的爱净重21克
- 使用AsyncSocket实现RTSP协议
- vim cscope E567: no cscope connections