PAT B -1068 little green leaves red (20 points)

Click on the link full solution summary PAT B -AC

Title:
For a computer, but the color is a 24-bit pixel value corresponding to the point. Now given a resolution of an M × N Videos, you find the required green leaves little red, the pixel that is a unique color, the color of the color point and its surrounding eight pixels adjacent difference sufficiently large.

Input format:
input of the first line gives the three positive integers, respectively, and M is N (≤ 1000), i.e., the resolution of the image; and TOL, a pixel adjacent points required color difference threshold value, the color difference exceeds TOL the point was only consideration. Then N lines of M pixels are given color values of the range [0,2 24 inside). Separated by a space or TAB between all digital counterparts.

Output format:
Following the row (x, y): colorof pixels in the output format required location and a color value, wherein the location xand yare the pixel columns of the image matrix, the row number (numbered from 1). If such a point is not unique, then the output Not Unique; if such sites do not exist, then the output Not Exist.

Sample Input 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

Output Sample 1:

(5, 3): 16711680

Sample Input 2:

4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0

Output Sample 2:

Not Unique

Sample Input 3:

3 3 5
1 2 3
3 4 5
5 6 7

Sample Output 3:

Not Exist

My code:

#include<iostream>
#include<cstdio>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#include<sstream>
using namespace std;
//有的时候题目是一起做的,所以会有不需要的头文件

int main()
{
    set<int>color_single,color_not,color_res;
    int M,N,TOL;
    cin>>M>>N>>TOL;
    int src[N][M];
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            cin>>src[i][j];
            if(color_not.find(src[i][j])!=color_not.end()){}
            else if(color_single.find(src[i][j])!=color_single.end())
            {
                color_single.erase(src[i][j]);
                color_not.insert(src[i][j]);
            }
            else
                color_single.insert(src[i][j]);
        }
    }

    int x=0,y=0;
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<M;j++)
        {
            bool flag1=(i-1<0||j-1<0)?true:abs(src[i][j]-src[i-1][j-1])>TOL;
            bool flag2=(i-1<0)?true:abs(src[i][j]-src[i-1][j])>TOL;
            bool flag3=(i-1<0||j+1>=M)?true:abs(src[i][j]-src[i-1][j+1])>TOL;
            bool flag4=(j-1<0)?true:abs(src[i][j]-src[i][j-1])>TOL;
            bool flag5=(j+1>=M)?true:abs(src[i][j]-src[i][j+1])>TOL;
            bool flag6=(i+1>=N||j-1<0)?true:abs(src[i][j]-src[i+1][j-1])>TOL;
            bool flag7=(i+1>=N)?true:abs(src[i][j]-src[i+1][j])>TOL;
            bool flag8=(i+1>=N||j+1>=M)?true:abs(src[i][j]-src[i+1][j+1])>TOL;

            if(flag1&&flag2&&flag3&&flag4&&flag5&&flag6&&flag7&&flag8&&
               color_single.find(src[i][j])!=color_single.end())
            {
                color_res.insert(src[i][j]);
                x=j+1;
                y=i+1;
            }
        }
    }

    if(color_res.size()==0)
        cout<<"Not Exist"<<endl;//case 2
    else if(color_res.size()>1)
        cout<<"Not Unique"<<endl;//case 1
    else
        printf("(%d, %d): %d\n",x,y,*color_res.begin());

    return 0;
}

The code is written is feces!
I did not pay attention to a topic that is unique, resulting in added after many set to exclude duplicate number
After two case has not passed, later found to be misled by the title, we must take into account the number of all eight surrounding pixel value and therefore is not considered on the boundary, in fact, the pixels on the boundary is likely to enter candidates! So finally we added eight flag to make a judgment. Fortunately the AC!
This question is free, then I'll rewrite it

Published 82 original articles · won praise 1 · views 1667

Guess you like

Origin blog.csdn.net/qq_34451909/article/details/104923141