Python ZOJ1067 Color Me Less

题目

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

The input file 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. 

题目重要信息:

输入的前16组数据是“目标集合”,也就是RGB1(R1, G1, B1)

接下来的输入直到出现"-1 -1 -1" 的结束标志前,都是RGB2(R2, G2, B2)

我们要做的,则是找出每一个RGB2对应的最近的RGB1

两个RGB颜色之间的“距离”公式如图

输入输出注意事项:

前16是“目标集合”,后面的输入才是RGB2

“-1 -1 -1”是输入的集合标志

输入一个RGB2应该跟着输出RGB2和与它最近的颜色RGB1

样例

输入

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

输出

 (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.怎么储存一个RGB颜色

从题目里我们知道,我们需要把这些RGB颜色储存起来,而在后面每一个RGB2都要计算与目标集合里的每一个RGB1的距离进行比较,要经常访问RGB(R, G, B)所以一个较为有效的储存方式显得尤为重要。

我们在题目中主要访问的是RGB中的变量R、变量G和变量B,所以何不为RGB创建一个类?

class RGB:
    def __init__(self, r1, g1, b1):
        self.r = r1
        self.g = g1
        self.b = b1

步骤2:为RGB距离计算公式创建一个函数,方便后面的多次计算

def dis(rgb1, rgb2): 
    d = ((rgb2.r - rgb1.r) ** 2 + (rgb2.g - rgb1.g) ** 2 + (rgb2.b - rgb1.b) ** 2) ** (1 / 2)
    return d
'''很明显我们的函数需要两个RGB颜色,也就是说,传入的是两个RGB类的实例对象'''

到这里为止我们已经把解题所需要的准备做好了,是时候处理输入和输出了

步骤3:储存RGB1

list_1 = []  # RGB1

for time in range(16):
    r_1, g_1, b_1 = map(int, input("").split(" "))
    x = RGB(r_1, g_1, b_1)  # 创建实例对象
    list_1.append(x)

步骤4:计算

while True:
    r_2, g_2, b_2 = map(int, input("").split(" "))
    if r_2 == g_2 == b_2 == -1:
        break
    x_2 = RGB(r_2, g_2, b_2)  # RGB2的实例对象
    list_2 = []
    for j in list_1:
        dtc = dis(x_2, j)
        list_2.append(dtc)  # 把一个RGB2与所有RGB1的距离储存到列表内
    dis_min = min(list_2)  #找到列表内的最小值
    rgb_1 = list_1[list_2.index(dis_min)]
    '''找到最小值的下标,用同样的下标去访问list_1里的RGB1
    这个RGB1就是离RGB2最近的颜色'''
    print("({},{},{}) maps to ({},{},{})".format(x_2.r, x_2.g, x_2.b, rgb_1.r, rgb_1.g, rgb_1.b))

最终答案:

class RGB:
    def __init__(self, r1, g1, b1):
        self.r = r1
        self.g = g1
        self.b = b1


def dis(rgb1, rgb2):
    d = ((rgb2.r - rgb1.r) ** 2 + (rgb2.g - rgb1.g) ** 2 + (rgb2.b - rgb1.b) ** 2) ** (1 / 2)
    return d


list_1 = []  # RGB1

for time in range(16):
    r_1, g_1, b_1 = map(int, input("").split(" "))
    x = RGB(r_1, g_1, b_1)
    list_1.append(x)


while True:
    r_2, g_2, b_2 = map(int, input("").split(" "))
    if r_2 == g_2 == b_2 == -1:
        break
    x_2 = RGB(r_2, g_2, b_2)
    list_2 = []
    for j in list_1:
        dtc = dis(x_2, j)
        list_2.append(dtc)
    dis_min = min(list_2)
    rgb_1 = list_1[list_2.index(dis_min)]
    print("({},{},{}) maps to ({},{},{})".format(x_2.r, x_2.g, x_2.b, rgb_1.r, rgb_1.g, rgb_1.b))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桜小路嵐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值