1068 万绿丛中一点红(20 分)
对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
输入格式:
输入第一行给出三个正整数,分别是 M 和 N(
≤1000
≤
1000
),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过 TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [0,
224
2
24
) 内。所有同行数字间用空格或 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
解析
注意题目的要求(我也是看别人的blog才发现的)
要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
①像素点是独一无二的
②该像素点与周围8个相邻的像素颜色超过阈值。
所以用数组保存所有像素。用map来查询该像素点是否是唯一。
用增量数组来表示周围8个方向。这是老套路了,一定要会。
const int MOVE[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
用judge函数判断每个点是否是要求的像素点。
这里我提前把Not Unique,Not Exist放在string了,防止我打错了o( ̄▽ ̄)o
const std::string out1="Not Unique";
const std::string out2="Not Exist";
#include<bits/stdc++.h>
using std::cin;
using std::cout;
using std::pair;
const std::string out1="Not Unique";
const std::string out2="Not Exist";
int array[1001][1001];
const int MOVE[8][2]={{-1,-1},{-1,0},{-1,1},{0,1},{1,1},{1,0},{1,-1},{0,-1}};
bool judge(int i,int j,int N,int M,int TOL){
for(int k=0;k<8;k++){
int x=i+MOVE[k][0];
int y=j+MOVE[k][1];
if(x>=0 && y>=0 && x<N && y<M)
if(abs(array[i][j]-array[x][y])<=TOL)
return false;
}
return true;
}
int main()
{
int M,N,TOL;
cin>>M>>N>>TOL;
std::map<int,int> pp;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++){
cin>>array[i][j];
pp[array[i][j]]++;
}
bool flag=false;
pair<int,int> point;
for(int i=0;i<N;i++)
for(int j=0;j<M;j++)
if(pp[array[i][j]]==1 &&judge(i,j,N,M,TOL)){
if(flag == false){
point.first=i;
point.second=j;
flag=true;
}
else{
cout<<out1;
return 0;
}
}
if(flag == false)
cout<<out2;
else
printf("(%d, %d): %d",point.second+1,point.first+1,array[point.first][point.second]);
}