PAT乙级真题 1068 万绿丛中一点红 C++实现

题目

对于计算机而言,颜色不过是像素点对应的一个 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

思路

分两步:

  1. 该像素点唯一
  2. 其与周围色差大于阈值

判断唯一

map<int, bool> duplicate记录每个点是否重复:把像素值作为key,每读一个像素,判断该key是否存在,若已存在就把value记为false,否则记为true。

柳神直接使用map<int, int>对每个数字计数,读到某个值时直接:
mapp[v[i][j]]++;

在StackOverflow上看到以下解释:

As soon as you access the map with the [] operator, if the key doesn’t exist it gets added. The default initializer of the int type gets invoked - so it will get a value of 0.

对map使用[]索引值,若key不存在则会将该key添加,并调用value的默认值作为其值。int的默认值即0。所以可以使用map直接计数。

判断色差

暴力法,遍历每个点,与周围8个点比较。

为了方便考虑边缘的点,在矩阵四周包裹了一圈不影响结果判断的数字,-2^24 = -16777216。

判断与周围点像素差的绝对值是否全都大于阈值即可。

代码

#include <iostream>
#include <vector>
#include <map>
#include <cmath>
using namespace std;

int main(){ 
    //提高cin效率
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int m, n, tol;
    cin >> m >> n >> tol;

    map<int, bool> duplicate;
    //矩阵四周填充-2^24,以便判断边缘点
    vector<vector<int> > a(n+2, vector<int>(m+2, -16777216));
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){
            cin >> a[i][j];
            if (duplicate.find(a[i][j])==duplicate.end()){
                duplicate[a[i][j]] = false;
            }
            else{
                duplicate[a[i][j]] = true; //标记此点存在了
            }
        }
    }

    int x = 0;
    int y = 0;
    int color = 0;
    int count = 0;
    bool notUnique = false;
    for (int i=1; i<=n; i++){
        for (int j=1; j<=m; j++){
            int temp = a[i][j];
            if (duplicate[temp]){ //该点不唯一
                continue; 
            }
            if (abs(temp-a[i-1][j-1])>tol &&
                 abs(temp-a[i-1][j])>tol &&   
                 abs(temp-a[i-1][j+1])>tol && 
                 abs(temp-a[i][j-1])>tol && 
                 abs(temp-a[i][j+1])>tol && 
                 abs(temp-a[i+1][j-1])>tol && 
                 abs(temp-a[i+1][j])>tol && 
                 abs(temp-a[i+1][j+1])>tol){
                if (count==1){
                    notUnique = true;
                    break;
                }
                x = i;
                y = j;
                color = temp;
                count++;
            }
        }
        if (notUnique){
            break;
        }
    }
    if (notUnique){
        cout << "Not Unique" << endl;
    }
    else if(count==0){
        cout << "Not Exist" << endl;
    }
    else{
        cout << "(" << y << ", " << x << "): " << color << endl;
    }
    return 0;
}

发布了105 篇原创文章 · 获赞 7 · 访问量 1727

猜你喜欢

转载自blog.csdn.net/zhang35/article/details/103935548