POJ1046 Color Me Less

题目来源:http://poj.org/problem?id=1046

题目大意:

  在RGB颜色空间中,用下面的公式来度量两个颜色值的距离。

  现给出16个RGB表示的颜色,和一些用于测试的颜色,求被测试的颜色与16个颜色中哪个最相近,有多个距离相等时取输入排在最前的一个颜色。

输入:前16行为给定的颜色,后面的每一行为一个测试用例,每个颜色都是由代表R、G、B的三个整数组成,值在0到255之间。以-1 -1 -1作为输入结束的标志。

输出:对于每个测试的颜色,输出他们的对应关系。格式见sample.


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)

水题,暴力解决。

 1 //////////////////////////////////////////////////////////////////////////
 2 //        POJ1046 Color Me Less
 3 //        Memory: 268K        Time: 0MS
 4 //        Language: C++        Result: Accepted
 5 //////////////////////////////////////////////////////////////////////////
 6 
 7 #include <iostream>
 8 #include <cmath>
 9 
10 using namespace std;
11 
12 struct Color {
13     int r, g, b;
14 };
15 
16 Color color[16];
17 
18 double distance(int i, int &r, int &g, int &b) {
19     return sqrt((float)(color[i].r - r) * (color[i].r - r) 
20               + (color[i].g - g) * (color[i].g - g)
21               + (color[i].b - b) * (color[i].b - b));
22 }
23 
24 int main(void) {
25     for (int i = 0; i < 16; ++i) cin >> color[i].r >> color[i].g >> color[i].b;
26     int id = 0;
27     while (true) {
28         int r, g, b;
29         cin >> r >> g >> b;
30         if (r == -1) break;
31         double min_d = distance(0, r, g, b);
32         int min_id = 0;
33         for (int i = 1; i < 16; ++i) {
34             double d = distance(i, r, g, b);
35             if (d < min_d) {
36                 min_id = i;
37                 min_d = d;
38             }
39         }
40         cout << "(" << r << "," << g << "," << b << ")" << " maps to " 
41              << "(" << color[min_id].r << "," << color[min_id].g << "," 
42              << color[min_id].b << ")" << endl;
43     }
44     system("pause");
45     return 0;
46 }
View Code
posted @ 2013-08-08 20:15  小菜刷题史  阅读(283)  评论(0编辑  收藏  举报