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. U-BOOT启动流程分析--start_armboot函数(二)

    第二阶段的功能: 初始化本阶段所需的硬件设备(主要设置系统时钟.初始化串口.Flash.网卡.USB) 检测系统内存映射(memory map) 将内核映像和根文件系统映象从Flash上读到RAM空间 ...

  2. Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-qvc66dfs/supervisor/

    # 安装supervisor 出错 pip3 install supervisor # 解决 sudo pip3 install supervisor

  3. 洛谷 P1964 【mc生存】卖东西

    P1964 [mc生存]卖东西 题目背景 服务器好好玩 题目描述 lcy0x1去服务器的系统商店卖东西. 一个人的背包有21格. 一开始他的背包里有m件不同的物品(不能卖). 他要卖n种物品,每种物品 ...

  4. 使用python创建cocos2d-x项目

    已准备条件: 已安装vs2012,已下载cocos2d-x sdk 2.2.3包. 旧版本号使用包里面的模板创建项目,如今新的包,使用python  来创建 1.下载安装  python  https ...

  5. Python 查找Twitter中特定话题中最流行的10个转发Tweet

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-4 @author: guaguastd @name: fi ...

  6. hdu1533Going Home KM算法

    //给一个n*m的图, //m表示人,h表示房子 //问全部人走回家的最小步数 //每一个人仅仅能进一间房 //非常明显的最大带权匹配 //每一个人到每每间房的距离即为权值 //因为是求最小,仅仅要改 ...

  7. java使用默认线程池踩过的坑(二)

    云智慧(北京)科技有限公司 陈鑫 是的.一个线程不可以启动两次.那么它是怎么推断的呢? public synchronized void start() { /** * A zero status v ...

  8. 9.9递归和动态规划(六)——打印n对括号的所有有效组合(即左右括号正确配对)

    /**  * 功能:打印n对括号的所有有效组合(即左右括号正确配对). */ 两种方法: 方法一: /** * 思路:在括号的最前面或者原有的每对括号中面插入一对括号. 至于其它任何位置.比方字符串的 ...

  9. python django的单元测试

    # TestCase 基类的使用 django.test.TestCase # 测试依赖于数据库访问,创建或查询模型 unittest.TestCase #没有与数据库交互 避免刷新数据库的成本

  10. Myeclipse集成Maven(图文说明)

    myeclipse 上安装 Maven3 环境准备: JDK 1.6 Maven 3.2.5 myeclipse 2013 安装 Maven 之前要求先确定你的 JDK 已经安装配置完毕.Maven是 ...