ZOJ Problem Set - 1067
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=67
没想出什么好的算法,直接写代码了,没什么优化。
#include <iostream>
#include <climits>
using namespace std;
#include <math.h>
int main()
{
int tarSet[16][3];
int Color[3];
int loop = 0;
long dist, minDist, minIdx;
while(16 > loop) {
cin >> tarSet[loop][0]
>> tarSet[loop][1]
>> tarSet[loop][2];
loop++;
}
while(cin >> Color[0] >> Color[1] >> Color[2]) {
if(-1 == Color[0] && -1 == Color[1] && -1==Color[2])
break;
minDist = INT_MAX;
minIdx = -1;
for(loop = 0;loop < 16;loop++) {
dist = (tarSet[loop][0] - Color[0]) * (tarSet[loop][0] - Color[0])
+ (tarSet[loop][1] - Color[1]) * (tarSet[loop][1] - Color[1])
+ (tarSet[loop][2] - Color[2]) * (tarSet[loop][2] - Color[2]);
if(dist < minDist) {
minDist = dist;
minIdx = loop;
}
}
cout << "(" << Color[0] << ","
<< Color[1] << ","
<< Color[2] << ") maps to ("
<< tarSet[minIdx][0] << ","
<< tarSet[minIdx][1] << ","
<< tarSet[minIdx][2] << ")"
<< endl;
}
return 0;
}