Color Me Less
Time Limit: 1000MS
|
|
Memory Limit: 10000K
|
Total Submissions: 18730
|
|
Accepted: 8886
|
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
Greater New York 2001
先读懂题意:
很简单,就是给定一个三维点以及一个由16个三维点组成的点集,求这个给定三维点与点集中哪个三维坐标点的距离最小。
基本思路:
就是模拟题啦,照着题意做就行。
代码如下:
#include <iostream>
#include <cmath>
int R[16], G[16], B[16];
void mapping(int r, int g ,int b)
{
long diff;
int flag;
int min = 255*3 + 1;
for(int i=0; i<16; i++)
{
//diff = abs(r-R[i])+abs(g-G[i])+abs(b-B[i]); WA??
//这里很诡异,我简化成求绝对值的和,提交后居然是WA,实在搞不懂
//非得要我求平方和之后再开方??
diff = sqrt((double)(r-R[i])*(r-R[i]) + (double)(g-G[i])*(g-G[i]) + (double)(b-B[i])*(b-B[i]));
if(diff < min)
{
min = diff;
flag = i;
}
}
std::cout<<"("<<r<<","<<g<<","<<b<<") maps to "
<<"("<<R[flag]<<","<<G[flag]<<","<<B[flag]<<")"<<std::endl;
}
int main()
{
int r, g, b;
for(int i=0; i<16; i++)
{
std::cin>>R[i]>>G[i]>>B[i];
}
std::cin>>r>>g>>b;
while(r!=-1 || g!=-1 || b!=-1)
{
mapping(r, g, b);
std::cin>>r>>g>>b;
}
system("pause");
return 0;
}
分享到:
相关推荐
【ACM竞赛与北大POJ解题报告】 在编程竞赛领域,ACM(国际大学生程序设计竞赛,简称ACM/ICPC)是一项极具影响力的比赛,它挑战参赛者的算法设计、问题理解和快速编码能力。北京大学(Peking University)的在线判题...
### ACM新手刷题攻略之POJ ... - 推荐题目:[poj1068](https://vjudge.net/problem/POJ-1068)、[poj2632](https://vjudge.net/problem/POJ-2632)、[poj1573](https://vjudge.net/problem/POJ-1573)、[poj2993]...
【标题】"POJ-2151.rar_poj"是一个与编程竞赛相关的压缩文件,主要涉及的问题是“检查问题的难度”,并且是为动态规划的实战练习而设计的。这个题目来自于著名的在线编程竞赛平台POJ(Programming Online Judge)。 ...
POJ(Problem Online Judge)是一个流行的在线编程练习平台,尤其适合初学者进行“百练”来巩固基础和提高解题能力。以下是一些从题目列表中抽取的知识点,涵盖多个编程领域: 1. **基础算法**: - **鸡兔同笼**...
【标题】"poj-1028-Web-Navigation.zip_poj" 指向的是一个编程竞赛问题,POJ(Programming Online Judge)平台上的第1028题,名为“Web Navigation”。该问题通常涉及到算法设计和实现,可能是为了考察参赛者的编程...
标题中的“POJ-1170.rar_poj”指的是编程竞赛网站POJ(Programming Online Judge)上的一个问题,编号为1170。POJ是一个在线的编程练习平台,主要面向学习和提升算法与编程技能的用户。这个问题的解决方案可能包含在...
【标题】"POJ-1009"是北大在线编程竞赛平台上的一个题目,它涉及到计算机科学中的算法和编程技巧。POJ(Problem Set of Peking University)是中国北京大学主办的一个在线编程竞赛平台,旨在提高学生的算法设计和...
- (poj1860, poj3259, poj1062, poj2253, poj1125, poj2240):介绍迪杰斯特拉算法(Dijkstra)、贝尔曼-福特算法(Bellman-Ford)、弗洛伊德算法(Floyd)等。 - 使用堆优化的迪杰斯特拉算法。 3. **最小生成树...
《POJ问题求解策略深度解析》 在编程竞赛的世界里,POJ(Programming Online Judge)是一个备受瞩目的在线评测系统,它为参赛者提供了大量的编程题目,旨在锻炼和提升编程解决问题的能力。本文将深入探讨POJ问题的...
### POJ 分类题目知识点详解 #### 一、基本算法 **1. 枚举** - **定义**:枚举是一种通过尝试所有可能情况来解决问题的方法。 - **示例题目**: - poj1753 - poj2965 - **应用场景**:适用于问题规模较小或解决...
- POJ 1860、POJ 3259:利用Dijkstra算法解决最短路径问题。 - POJ 1062、POJ 2253:涉及Bellman-Ford算法的应用场景。 - POJ 1125、POJ 2240:Floyd算法的典型实例。 ##### 最小生成树 - **算法**: - Prim...
青蛙的约会,这是一个经典的计算机算法问题,源自编程竞赛平台POJ(Programming Online Judge)上的第1061题。此问题通常被归类为图论中的最短路径问题,涉及到了深度优先搜索(DFS)或广度优先搜索(BFS)等算法。...
【标题】"POJ2002-Squares"是一个经典的计算机编程题目,源自北京大学的在线判题系统(POJ,即PKU Online Judge)。这个题目主要涉及到算法设计和实现,尤其是数学和动态规划方面的知识。 【描述】"解题报告+AC代码...
《深入探索LeetCode OJ与My-POJ调试实践》 在编程的世界里,LeetCode OJ(在线判题系统)和My-POJ(个人POJ)是程序员提升技能、锻炼算法的重要平台。这两个工具提供了丰富的编程挑战,帮助开发者在解决实际问题中...
标题 "POJ 1751 求最小生成树Prim算法(JAVA)" 提到的是一个编程挑战,涉及图论中的经典算法——Prim算法。在计算机科学中,Prim算法是用于寻找加权无向图的最小生成树的一种有效方法。最小生成树是一棵树形结构,...
- POJ 1753 - POJ 2965 #### 2. 排序算法 - **描述**:排序算法包括快速排序、归并排序等。 - **应用场景**:数据排序、查找问题等。 - **相关题目**: - POJ 1328 - POJ 2109 - POJ 2586 #### 3. 枚举算法 -...
根据提供的文件信息,我们可以分析出该段代码是用于解决POJ平台上的2314题的一种解决方案,主要涉及到了变量管理、表达式处理等方面。下面将详细解释代码中的关键概念和实现逻辑。 ### 关键概念解析 #### Variable...
twilight-poj-solutionPOJ () solution by twilight想当年要找一题的分析, 解答实在太难了现在都是开源的时代了, 干脆把Archive放到GitHub上, 供后来人参考.POJ ID: 部分题解: 转载请注明出处~
- 示例题目:poj1191, poj1054, poj3280, poj2029, poj2948, poj1925, poj3034 2. **四边形不等式** - 示例题目:POJ3254, poj2411, poj1185 3. **记忆化搜索** - 示例题目:poj2057, poj1947, poj2486, poj...
标题“POJ3253-POJ3253-Fence Repair【STL优先队列】”指的是一个在线编程竞赛题目,源自北京大学的在线判题系统POJ(Problem Online Judge)。该题目要求参赛者使用C++编程语言解决特定的问题,并且在解决方案中...