天天看点

PAT 乙级 1068 万绿丛中一点红 (20分)

1068 万绿丛中一点红 (20分)

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

#include <iostream>
#include <map>
#include <vector>
using namespace std;
typedef struct{
    int row;
    int col;
    long long color;
}ds;
int main(){
    long long m,n,tol;
    cin>>m>>n>>tol;
    long long store[1005][1005] = {0};
    map<long long,int> cnt_map;
    vector<ds> ans;
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            cin>>store[i][j];
            cnt_map[store[i][j]] = cnt_map[store[i][j]] + 1;
        }
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) {
            if(cnt_map[store[i][j]] > 1)
                continue;
            //左上
            if(i-1 >= 1 && j-1 >= 1 &&abs(store[i][j] - store[i-1][j-1]) <= tol)
                continue;
            //上
            if(i-1 >= 1 && abs(store[i][j] - store[i-1][j]) <= tol)
                continue;
            //右上
            if(i-1 >= 1 && j+1 <= m && abs(store[i][j] - store[i-1][j+1]) <= tol)
                continue;
            //右
            if(j+1 <= m && abs(store[i][j] - store[i][j+1]) <= tol)
                continue;
            //右下
            if(j+1 <= m && i+1 <= n && abs(store[i][j] - store[i+1][j+1]) <= tol)
                continue;
            //下
            if(i+1 <= n && abs(store[i][j] - store[i+1][j]) <= tol)
                continue;
            //左下
            if(j+1 <= n && i-1 <= 0 && abs(store[i][j] - store[i-1][j+1]) <= tol)
                continue;
            //左
            if(i-1 <= 0 && abs(store[i][j] - store[i-1][j]) <= tol)
                continue;
            ds mid;
            mid.row = i;
            mid.col = j;
            mid.color = store[i][j];
            ans.push_back(mid);
        }
    }
    if(ans.size() == 1){
        ds mid = ans[0];
        cout<<"("<<mid.col<<", "<<mid.row<<"): "<<mid.color;
    } else if(ans.size() == 0){
        cout<<"Not Exist";
    } else{
        cout<<"Not Unique";
    }
    return 0;
}