ZOJ 1067

原来我想,可不可以通过对目标集的16个color做一些预处理,可以缩小一下搜索空间

 

可是一时没想到,就先试试把16个都扫描一遍,谁知AP之后竟是0ms。网上搜了一下,也没发现有人用缩小搜索空间的方法。我再想想吧。

 

代码帖出来

 

#include<iostream> #include<limits> using namespace std; class Color{ public: int R,G,B; int squ_dist( const Color & c) { return ( R - c.R )*( R - c.R ) + ( G - c.G)*( G - c.G) + ( B - c.B)*( B - c.B); } friend ostream & operator<<( ostream & os, const Color & c); }; ostream & operator<<( ostream & os, const Color & c) { os << "(" << c.R <<","<<c.G <<","<<c.B<<")"; return os; } int main( void) { Color target[16]; for( int i=0; i<16; i++) { cin>> target[i].R >>target[i].G >>target[i].B; } Color tobemapped; while( cin >> tobemapped.R && tobemapped.R >= 0 ) { cin >> tobemapped.G >> tobemapped.B; int d, m; d = numeric_limits<int>::max(); for( int i=0; i<16; i++) if( d > tobemapped.squ_dist( target[i] )) { d = tobemapped.squ_dist( target[i] ); m = i; } cout<<tobemapped <<" maps to " <<target[m] <<endl; } return 0; }

你可能感兴趣的:(ZOJ 1067)