Color Me Less
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30146   Accepted: 14634

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.

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)

Source

解题思路:

一种颜色用三元组(r,g,b)表示。两个颜色的距离为

给出16个已知的颜色值,求这里面距离给定的颜色值距离最短的颜色值。

枚举,比較距离就能够了。

代码:

#include <iostream>
#include <cmath>
const int inf=0x7fffffff;
using namespace std; struct RGB
{
double r,g,b;
int match;//用来记录距离最短的颜色是第几个
}rgb[100]; int main()
{
int k=1;
while(cin>>rgb[k].r>>rgb[k].g>>rgb[k].b&&rgb[k].r!=-1&&rgb[k].g!=-1&&rgb[k].b!=-1)
{
k++;
}
double dis;
for(int i=17;i<=k-1;i++)
{
dis=inf;
for(int j=16;j>=1;j--)
{
double temp=sqrt((rgb[i].r-rgb[j].r)*(rgb[i].r-rgb[j].r)+(rgb[i].g-rgb[j].g)*(rgb[i].g-rgb[j].g)+(rgb[i].b-rgb[j].b)*(rgb[i].b-rgb[j].b));
if(dis>=temp)
{
dis=temp;//最短距离
rgb[i].match=j;//第i中颜色距离第j种颜色距离最短
}
}
}
for(int i=17;i<=k-1;i++)
{
cout<<"("<<rgb[i].r<<","<<rgb[i].g<<","<<rgb[i].b<<") maps to ("<<rgb[rgb[i].match].r<<","<<rgb[rgb[i].match].g<<","<<rgb[rgb[i].match].b<<")"<<endl; }
return 0;
}

[ACM] POJ 1046 Color Me Less的更多相关文章

  1. POJ 1046 Color Me Less 最详细的解题报告

    题目来源:POJ 1046 Color Me Less 题目大意:每一个颜色由R.G.B三部分组成,D=Math.sqrt(Math.pow((left.red - right.red), 2)+ M ...

  2. poj 1046 ——Color Me Less

    提交地址:http://poj.org/problem?id=1046 Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7630   Accepted: 2507 Description ...

  4. poj 1046 Color Me Less

    Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33007   Accepted: 16050 D ...

  5. POJ 1046 Color Me Less(浅水)

    一.Description A color reduction is a mapping from a set of discrete colors to a smaller one. The sol ...

  6. 北大ACM - POJ试题分类(转自EXP)

    北大ACM - POJ试题分类 -- By EXP 2017-12-03 转载请注明出处: by EXP http://exp-blog.com/2018/06/28/pid-38/ 相关推荐文: 旧 ...

  7. ACM: POJ 1401 Factorial-数论专题-水题

    POJ 1401 Factorial Time Limit:1500MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  8. 组合数学 - 波利亚定理 --- poj : 2154 Color

    Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7873   Accepted: 2565 Description ...

  9. [ACM] POJ 3687 Labeling Balls (拓扑排序,反向生成端)

    Labeling Balls Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10161   Accepted: 2810 D ...

随机推荐

  1. Webhook

    Webhook就是用户通过自定义回调函数的方式来改变Web应用的一种行为,这些回调函数可以由不是该Web应用官方的第三方用户或者开发人员来维护,修改.通过Webhook,你可以自定义一些行为通知到指定 ...

  2. cocos2dx 3.0正式版 在mac上新建项目

    触碰科技确定2.x版本号不会再更新了,会一直维护3.x的版本号.于是赶紧看看3.0的,简单浏览一下.类的使用方法和原来的几乎相同,仅仅是 表达的写法变了下,. . . . 以后肯定有非常多变化,速度熟 ...

  3. NumPy基础入门学习

    对于习惯使用了MATLAB的用户而言,学习NumPy这个python工具包付出的成本应该是不大的. NumPy的基本的object是多维数组,是一个有同样类型的数字等构成的一张表格,能够通过元组进行索 ...

  4. 机器学习完整过程案例分布解析,python代码解析

    所谓学习问题,是指观察由n个样本组成的集合,并依据这些数据来预測未知数据的性质. 学习任务(一个二分类问题): 区分一个普通的互联网检索Query是否具有某个垂直领域的意图.如果如今有一个O2O领域的 ...

  5. HDU - 2254 奥运 (求等比数列和)

    Description 北京迎来了第一个奥运会,我们的欢呼声响彻中国大地,所以今年的奥运金牌 day day up! 比尔盖兹坐上鸟巢里,手里摇着小纸扇,看的不亦乐乎,被俺们健儿的顽强拼搏的精神深深的 ...

  6. WIN10 10招

    还有不到两个月的时间,7 月 29 日 Windows 10 就将正式公布,在此之前已经有不少的用户已经使用上了 Windows 10 的预览版.对于那些苦等 Windows 10 的用户来说,幸福非 ...

  7. js--11对象的创建方式

    <html> <head> <title>Object</title> </head> <body> <script ty ...

  8. vim 基础学习之普通模式

    .操作 = 操作符 + 动作 aaa bbb例如,d是删除命令,b是移动到距离光标最近的字符串开头当我们执行db的时候,就会删除光标(不包括光标位置)到最近字串开头之间的字符dj则会删除光标所在行以及 ...

  9. 详解Android插件化开发-资源访问

    动态加载技术(也叫插件化技术),当项目越来越庞大的时候,我们通过插件化开发不仅可以减轻应用的内存和CPU占用,还可以实现热插拔,即在不发布新版本的情况下更新某些模块.     通常我们把安卓资源文件制 ...

  10. 三个角度解构云计算,商业驱动or技术驱动?

    从云计算的使用者到云服务的输出者,大多互联网公司在过去一年完成了角色的转换,也让云计算的未来更加扑朔迷离.不过,抛却进入时间这个评判因素,单从技术和商业化的角度来解构云计算的话,对于云计算的格局以及未 ...