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
 
思路:既然要与周围八个元素比较,考虑到边界问题,需要在最外面加一圈0,而初始化矩阵为0,再下标从1输入矩阵可以很好的解决这个问题,另外就是这里用map标记做的话会简单一点......
 
 1 //1068 万花丛中一点红 
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<map>
 6 using namespace std;
 7 #define max 16777216
 8 int main()
 9 {
10     int M,N,tol;
11     cin>>M>>N>>tol;
12     int color[1001][1001]={0};
13     map<int,int>mp;//自动初始化为0 
14     for(int i=1;i<=N;i++)
15     {
16         for(int j=1;j<=M;j++)
17         {
18              cin>>color[i][j];
19              mp[color[i][j]]++;//先标记 
20         }
21     }
22     int flag=0;
23     int markN[10],markM[10],count=0;
24     for(int i=1;i<=N;i++)
25     {
26         for(int j=1;j<=M;j++)
27         {
28             if(abs(color[i][j]-color[i-1][j-1])>tol&&abs(color[i][j]-color[i-1][j])>tol&&abs(color[i][j]-color[i-1][j+1])>tol&&abs(color[i][j]-color[i][j+1])>tol
29             &&abs(color[i][j]-color[i+1][j+1])>tol&&abs(color[i][j]-color[i+1][j])>tol&&abs(color[i][j]-color[i+1][j-1])>tol&&abs(color[i][j]-color[i][j-1])>tol)
30             {
31                 if(mp[color[i][j]]==1)
32                 {
33                     flag++;
34                     markN[count]=i;
35                     markM[count]=j;
36                     count++;
37                 }
38             }
39         }
40     }
41     if(flag==0)
42         cout<<"Not Exist"<<endl;
43     else if(flag==1)
44         cout<<"("<<markM[0]<<", "<<markN[0]<<"): "<<color[markN[0]][markM[0]]<<endl;
45     else if(flag>1)
46         cout<<"Not Unique"<<endl;
47     return 0;
48 }
大佬见笑,,
原文地址:https://www.cnblogs.com/xwl3109377858/p/10485995.html