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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

zoj 1067 Color Me Less

Color Me Less Time Limit: 2 Seconds      Memory Limit: 65536 KB Problem A color reduction i...

zoj 1067 Color Me Less

正确代码: #include #include #include using namespace std; int main() { int tar[16][3]; for(int i=0;...

ZOJ--1067:Color Me Less

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

ZOJ 1067 Color Me Less

Color Me Less Time Limit: 2 Seconds      Memory Limit: 65536 KB Problem A color reduction i...

POJ1046——Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33605   ...

POJ 1046 Color Me Less

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31449   Accepted: 1...

集训9.5----Color Me Less

题目描述 A color reduction is a mapping from a set of discrete colors to a smaller one.The soluti...

poj 1046(Color Me Less)

Color Me Less Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23677   ...

AOJ-AHU-OJ-186 Color Me Less

Color Me Less Time Limit: 1000 ms   Case Time Limit: 1000 ms   Memory Limit: 64 MB Descr...

Color Me Less POJ - 1046

A color reduction is a mapping from a set of discrete colors to a smaller one. The solution to this ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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