对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
输入格式:
输入第一行给出三个正整数,分别是 M 和 N(≤ 1000),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过 TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [0,224) 内。所有同行数字间用空格或 TAB 分开。
输出格式:
在一行中按照 (x, y): color
的格式输出所求像素点的位置以及颜色值,其中位置 x
和 y
分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。如果这样的点不唯一,则输出 Not Unique
;如果这样的点不存在,则输出 Not Exist
。
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
int main() {
int m, n;
long long color[1001][1001] = { 0 };
int tol;
cin >> m >> n >> tol;
vector<int> color_2;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
long long tmp;
cin >> tmp;
color[i][j] = tmp;
color_2.push_back(tmp);
}
}
vector<pair<vector<int>, long long>> result;
int next[8][2] = { {0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,-1},{-1,1} };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int tx, ty;
int cnt = 0;
int cmp = 8;
for (int k = 0; k < 8; k++) {
tx = j + next[k][0];
ty = i + next[k][1];
if (tx < 0 || tx >= m || ty < 0 || ty >= n) {
cmp--;
continue;
}
if (color[ty][tx] - color[i][j] < -tol || color[ty][tx] - color[i][j] > tol)cnt++;
}
if (cnt == cmp) {
if (count(color_2.begin(), color_2.end(), color[i][j]) == 1) {
vector<int> tmp;
tmp.push_back(i + 1);
tmp.push_back(j + 1);
result.push_back(make_pair(tmp, color[i][j]));
}
}
}
}
if (result.size() == 0) cout << "Not Exist" << endl;
else if (result.size() == 1) {
for (auto i : result) {
cout << "(" << i.first[1] << ", " << i.first[0] << "): " << i.second << endl;
}
}
else {
cout << "Not Unique" << endl;
}
return 0;
}