PAT菜鸡进化史_乙级_1068
对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
输入格式:
输入第一行给出三个正整数,分别是 M 和 N(≤ 1000),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过 TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [0,224) 内。所有同行数字间用空格或 TAB 分开。
输出格式:
在一行中按照 (x, y): color
的格式输出所求像素点的位置以及颜色值,其中位置 x 和 y 分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。如果这样的点不唯一,则输出 Not Unique
;如果这样的点不存在,则输出 Not Exist
。
输入样例1:
8 6 200
0 0 0 0 0 0 0 0
65280 65280 65280 16711479 65280 65280 65280 65280
16711479 65280 65280 65280 16711680 65280 65280 65280
65280 65280 65280 65280 65280 65280 165280 165280
65280 65280 16777015 65280 65280 165280 65480 165280
16777215 16777215 16777215 16777215 16777215 16777215 16777215 16777215
输出样例1:
(5, 3): 16711680
输入样例2:
4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0
输出样例2:
Not Unique
输入样例3:
3 3 5
1 2 3
3 4 5
5 6 7
输出样例3:
Not Exist
思路:
这题逻辑是不难的,主要还是题目读清楚
是要求“只出现一次”+“差距足够大”,别漏了条件
Code:
#include <iostream>
#include <vector>
#include <cmath>
#include <map>
int main(){
using namespace std;
// input the color
int M, N, tol;
cin >> M >> N >> tol;
vector<vector<int>> color(N, vector<int>(M));
map<int, int> exis_num;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
cin >> color[i][j];
exis_num[color[i][j]]++;
}
}
// find the point we want
int want = 0, x, y;
for (int i = 0; i < N; i++){
for (int j = 0; j < M; j++){
if (exis_num.find(color[i][j])->second > 1)
continue;
if (i - 1 >= 0 && j - 1 >= 0 && abs(color[i - 1][j - 1] - color[i][j]) <= tol)
continue;
if (i - 1 >= 0 && abs(color[i - 1][j] - color[i][j]) <= tol)
continue;
if (i - 1 >= 0 && j + 1 < M && abs(color[i - 1][j + 1] - color[i][j]) <= tol)
continue;
if (j - 1 >= 0 && abs(color[i][j - 1] - color[i][j]) <= tol)
continue;
if (j + 1 < M && abs(color[i][j + 1] - color[i][j]) <= tol)
continue;
if (i + 1 < N && j - 1 >= 0 && abs(color[i + 1][j - 1] - color[i][j]) <= tol)
continue;
if (i + 1 < N && abs(color[i + 1][j] - color[i][j]) <= tol)
continue;
if (i + 1 < N && j + 1 < M && abs(color[i + 1][j + 1] - color[i][j]) <= tol)
continue;
want++;
x = j + 1;
y = i + 1;
}
}
if (want > 1)
cout << "Not Unique" << endl;
else if (!want)
cout << "Not Exist" << endl;
else
printf("(%d, %d): %d\n", x, y, color[y - 1][x - 1]);
return 0;
}