• B1068 万绿丛中一点红


    对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N 的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。

    输入格式:

    输入第一行给出三个正整数,分别是 M 和 N(≤ 1000),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过 TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [ 内。所有同行数字间用空格或 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

    最终AC的代码如下:
    #include <iostream>
    #include <cmath>
    #include <vector>
    #include <map>
    using namespace std;
    
    struct Point{
        int x;
        int y;
        int color;
        Point(){
            x = 0;
            y = 0;
            color = 0;
        }
    };
    
    int main(){
        int i, j, m, n, tol;
        vector<Point> ans;
        map<int, int> mp;
        Point temp;
        scanf("%d %d %d", &m, &n, &tol);
        int a[m+1][n+1]={0};
        bool hashTable[m+1][n+1]={false};
        for(j=0; j<n; j++){
            for(i=0; i<m; i++){
                scanf("%d", &a[i][j]);
                mp[a[i][j]]++;
                if(i>0){
                    if(abs(a[i][j]-a[i-1][j])<=tol){
                        hashTable[i][j] = true;
                        hashTable[i-1][j] = true;
                    }
                }
            }
        }
        for(i=0; i<m; i++){
            for(j=0; j<n; j++){
                if(hashTable[i][j] || mp[a[i][j]]>1){
                    continue;
                }
                if(j>0){
                    if(abs(a[i][j]-a[i][j-1])>tol){
                        if(i>0){
                            if(abs(a[i][j]-a[i-1][j-1])<=tol){
                                hashTable[i][j] = true;
                                hashTable[i-1][j-1] = true;
                                continue;
                            }
                        }
                        if(i<m-1){
                            if(abs(a[i][j]-a[i+1][j-1])<=tol){
                                hashTable[i][j] = true;
                                hashTable[i+1][j-1] = true;
                                continue;
                            }
                        }
                    }else{
                        hashTable[i][j] = true;
                        hashTable[i][j-1] = true;
                        continue;
                    }
                }
                if(j<n-1){
                    if(abs(a[i][j]-a[i][j+1])>tol){
                        if(i>0){
                            if(abs(a[i][j]-a[i-1][j+1])<=tol){
                                hashTable[i][j] = true;
                                hashTable[i-1][j+1] = true;
                                continue;
                            }
                        }
                        if(i<m-1){
                            if(abs(a[i][j]-a[i+1][j+1])<=tol){
                                hashTable[i][j] = true;
                                hashTable[i+1][j+1] = true;
                                continue;
                            }
                        }
                    }else{
                        hashTable[i][j] = true;
                        hashTable[i][j+1] = true;
                        continue;
                    }
                }
                temp.x = i + 1;
                temp.y = j + 1;
                temp.color = a[i][j];
                ans.push_back(temp);
            }
        }
        if(ans.size()==1){
            printf("(%d, %d): %d
    ", ans[0].x, ans[0].y, ans[0].color);
        }else if(ans.size()==0){
            printf("Not Exist
    ");
        }else{
            printf("Not Unique
    ");
        }
        return 0;
    }

    我觉得自己的语文水平真的很有限呐!!!从一开始就是测试点3一直通过不了,然后卡了很久。最后看博客分析,才忽然明白了下面题干的意思:

    要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点

    这就是说,如果该点出现了两次及以上,那么就已经不满足“独一无二”的条件了。

  • 相关阅读:
    ArrayList源码剖析
    Java集合框架
    Java数据结构和算法(十五)——无权无向图
    Java数据结构和算法(十四)——堆
    Java数据结构和算法(十三)——哈希表
    Java数据结构和算法(十二)——2-3-4树
    Java数据结构和算法(十一)——红黑树
    Java数据结构和算法(十)——二叉树
    Java数据结构和算法(九)——高级排序
    [刷题] Leetcode算法 (2020-2-27)
  • 原文地址:https://www.cnblogs.com/heyour/p/12240290.html
Copyright © 2020-2023  润新知