ZOJ 1067Color Me Less

原创 2011年05月27日 23:34:00

Time Limit: 1 Second      Memory Limit: 32768 KB


Problem

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.


Example

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

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)

 

 没什么好讲的,根据公式求最小值

这道题目给我最大的提示是:要合理用结构啊。

两种Code,

Code 1:

#include<stdio.h>
struct color{
 int R,G,B;
}map[16];
int main()
{
 int d;
 for(int i=0;i<16;i++)
  scanf("%d%d%d",&map[i].R,&map[i].G,&map[i].B);
 color c;
 while(scanf("%d%d%d",&c.R,&c.G,&c.B)&&c.R>=0)
 {
  int index=0;
  int min=65535;
  for(int i=0;i<16;++i)
  {
   d=(c.R-map[i].R)*(c.R-map[i].R)+(c.G-map[i].G)*(c.G-map[i].G)+(c.B-map[i].B)*(c.B-map[i].B);
   if(min>d)
   {
    min=d;
    index=i;
   }
  }
  printf("(%d,%d,%d) maps to (%d,%d,%d)/n",c.R,c.G,c.B,map[index].R,map[index].G,map[index].B);
 }
 return 0;
}

 

Code 2:

#include<stdio.h>//用和来比i可以不

int main()
{
 int a[16][3];
 int b[3],sign,i,j;
 int D,min;
 for(i=0;i<16;i++)
  for(j=0;j<3;j++)
   scanf("%d",&a[i][j]);
  while(scanf("%d%d%d",&b[0],&b[1],&b[2])&&b[0]!=-1&&b[1]!=-1&&b[2]!=-1)
  {
    
    min=200000;
    sign=0;
    for(i=0;i<16;i++)
    {
     D=(b[0]-a[i][0])*(b[0]-a[i][0])+(b[1]-a[i][1])*(b[1]-a[i][1])+(b[2]-a[i][2])*(b[2]-a[i][2]);
     if(D<min)//D<=min//如果等于的话,是输出前一个匹配的还是后一个呢、、这里被纠结了N久,
     {
      min=D;
      sign=i;
     }
    }
    printf("(%d,%d,%d) maps to (%d,%d,%d)/n",b[0],b[1],b[2],a[sign][0],a[sign][1],a[sign][2]);
  }
  return 0;
}

POJ,ZOJ题目分类(多篇整合版,分类很细致,全面)

水题: 3299,2159,2739,1083,2262,1503,3006,2255,3094 初级: 一、基本算法:        (1)枚举 (1753,2965)  ...
  • dg357442101
  • dg357442101
  • 2016年03月13日 14:24
  • 3470

浙大ZOJ 1009 Enigma问题解决及别人的解决方案

教训:在这题上浪费太多次机会了,因为以下几个原因: 1. 没考虑到m(m为轮子字母表的规模)为1的情况,从而导致出现“Floating Point Error”。通过将“if(i!=0&&i%(m-...
  • xiaogugood
  • xiaogugood
  • 2014年01月06日 17:16
  • 1645

2个经典 zoj2314 无源汇有上下界最大流 并输出可行流 ZOJ3229 有源汇上下界最大流

ZOJ Problem Set - 2314 Reactor Cooling Time Limit: 5 Seconds      Memory Limit: 32768 KB      Spe...
  • hnust_xiehonghao
  • hnust_xiehonghao
  • 2013年08月24日 11:36
  • 1663

zoj 1067 Color Me Less

正确代码: #include #include #include using namespace std; int main() { int tar[16][3]; for(int i=0;...
  • u010772673
  • u010772673
  • 2014年12月25日 11:35
  • 391

ZOJ--1067:Color Me Less

思路: 这题是让 先输入十六组数 然后给后面的每组数 从这十六组数中找通过那个式子值最小的 一组数据 ; 比较简单 不解释 java: import java.util.Scanner; p...
  • yf224
  • yf224
  • 2017年06月14日 22:11
  • 114

ZOJ 1067 Color Me Less

Color Me Less Time Limit: 2 Seconds      Memory Limit: 65536 KB Problem A color reduction i...
  • wpfengqi
  • wpfengqi
  • 2012年08月23日 14:12
  • 220

POJ1046——Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33605   ...
  • LDUtyk
  • LDUtyk
  • 2016年11月15日 15:46
  • 198

POJ 1046 Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31449   Accepted: 1...
  • u013068502
  • u013068502
  • 2015年04月10日 20:55
  • 386

集训9.5----Color Me Less

题目描述 A color reduction is a mapping from a set of discrete colors to a smaller one.The soluti...
  • IPOmonkey
  • IPOmonkey
  • 2016年09月06日 18:37
  • 192

POJ1046,Color Me Less,心情不爽就去切水

Color Me Less Description A color reduction is a mapping from a set of discrete colors to a sm...
  • Stand_over_sun
  • Stand_over_sun
  • 2012年11月19日 21:24
  • 407
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ZOJ 1067Color Me Less
举报原因:
原因补充:

(最多只允许输入30个字)