码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1046 Color Me Less解题报告

时间:2016-04-05 15:30:28      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

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)

  大水题本来没必要记的。可还是出了问题,说明自己更水!!
首先,自己理解题意太慢了,刷题太少,思维不够专注。其次,语言不熟,基础不扎实,把异或当平方用,肯定是MATLAB用的中毒太深。结果不报错,调试通过-1.#IND00才发现的。基础不扎实真的会浪费更多时间啊!
  AC代码
 1 #include <cstdio>
 2 #include <cmath>
 3 typedef struct Color
 4 {
 5     int R;
 6     int G;
 7     int B;
 8 }RGB;
 9 int main(int argc, char const *argv[])
10 {
11     RGB T[16];
12     for (int i = 0; i < 16; i++){
13         scanf("%d%d%d", &T[i].R, &T[i].G, &T[i].B);
14     }
15     int r, g, b;
16     
17     while (1){
18         scanf("%d%d%d", &r, &g, &b);
19         if (-1 == r || -1 == g || -1 == b)
20             break;
21         int mark = 0;
22         double D = 1e7 + 1.0;
23         double temp = 0;
24         for (int i = 0; i < 16; i++){
25             temp = sqrt((double)((T[i].R - r)*(T[i].R - r) + (T[i].G - g)*(T[i].G - g) + (T[i].B - b)*(T[i].B - b)));
26             if (temp < D){
27                 D = temp;
28                 mark = i;
29             }
30         }
31         printf("(%d,%d,%d) maps to (%d,%d,%d)\n", r, g, b, T[mark].R, T[mark].G, T[mark].B);
32     }
33     return 0;
34 }

 

 

POJ 1046 Color Me Less解题报告

标签:

原文地址:http://www.cnblogs.com/zhaoyu1995/p/5354883.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!