PAT Basic 1068. 万绿丛中一点红

PAT Basic 1068. 万绿丛中一点红

1. 题目描述:

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

2. 输入格式:

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

3. 输出格式:

在一行中按照 (x, y): color 的格式输出所求像素点的位置以及颜色值,其中位置 x 和 y 分别是该像素在图像矩阵中的列、行编号(从 1 开始编号)。如果这样的点不唯一,则输出 Not Unique;如果这样的点不存在,则输出 Not Exist

4. 输入样例:

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
4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0
3 3 5
1 2 3
3 4 5
5 6 7

5. 输出样例:

(5, 3): 16711680
Not Unique
Not Exist

6. 性能要求:

Code Size Limit
16 KB
Java (javac)
Time Limit
600 ms
Memory Limit
64 MB
Python (python2)
Time Limit
1400 ms
Memory Limit
64 MB
Python (python3)
Time Limit
1400 ms
Memory Limit
64 MB
Other Compilers
Time Limit
400 ms
Memory Limit
64 MB

思路:

准除草题,在整个矩阵中独一无二并且满足TOL要求的点即为目标点,遍历矩阵统计目标点个数,最后分情况进行输出即可。

但还是有几个地方需要注意,首先是二维数组的定义,int **pMatrix = (int **)malloc(sizeof(int[col]) * row);是不正确的方式,因为malloc返回指向分配内存空间的地址,而这里分配的空间又是按照int类型分的,所以不能将其转换为一个二级指针,通过malloc定义二维数组的三种方式可以自行Google。还有这里判断唯一性我一开始是想维护一个int型数组uniqueCount[]进行计数的,但是这里颜色值在 \([0,2^{24})\) 内,分配的数组大小较大时,会一直报Segmentation Error,后面还是定义了子函数isUnique()对每个点进行遍历判断。对TOL要求的判断代码写的也是比较冗余的。。。

比较简洁的代码可以参考大佬题解:PAT 1068. 万绿丛中一点红(20)-乙级_柳婼的博客-CSDN博客 ,定义map容器即可按照我一开始的想法判断唯一性,只能说需要抓紧学习下C++。。。

My Code:

#include <stdio.h>
#include <stdlib.h> // malloc header, abs header

int judgeTol(void *Matrix, int row, int col, int posX, int posY, int tol);
int isUnique(void *Matrix, int row, int col, int posX, int posY);

int main(void)
{
    int col=0, row=0, tol=0;
    int i=0, j=0; // iterator
    // can't use this way, the array size is too big
    //int uniqueCount[1000000000] = {0}; // to count number's appear times
    //int uniqueCount[10] = {0};
    int countRes = 0;
    int resI=0, resJ=0;
    
    scanf("%d%d%d", &col, &row, &tol);
    int (*pMatrix) [col] = (int (*)[col])malloc(sizeof(int[col]) * row); // define a two dimensional matrix
    //int **pMatrix = (int **)malloc(sizeof(int[col]) * row); // this way is wrong, will cause segmentation fault!!!
    
    for(i=0; i<row; ++i)
    {
        for(j=0; j<col; ++j)
        {
            scanf("%d", &pMatrix[i][j]);
            //printf("%d\n", uniqueCount[pMatrix[i][j]]);
            //++uniqueCount[pMatrix[i][j]];
            //printf("%d, count: %d\n", pMatrix[i][j], uniqueCount[pMatrix[i][j]]);
        }
    }
    
    for(i=0; i<row; ++i)
    {
        for(j=0; j<col; ++j)
        {
            //if(uniqueCount[pMatrix[i][j]] == 1) && judgeTol(pMatrix, row, col, i, j, tol)) // find red point
            //if(judgeTol(pMatrix, row, col, i, j, tol)) // find red point
            //if((uniqueCount[pMatrix[i][j]] == 1) && judgeTol(pMatrix, row, col, i, j, tol)) // find red point
            if(isUnique(pMatrix, row, col, i, j) && judgeTol(pMatrix, row, col, i, j, tol))
            {
                ++countRes;
                resI = i;
                resJ = j;
            }
        }
    }
    
    if(!countRes) // countRes == 0
    {
        printf("Not Exist\n");
    }
    else if(countRes == 1)
    {
        printf("(%d, %d): %d\n", resJ+1, resI+1, pMatrix[resI][resJ]);
    }
    else // countRes > 1
    {
        printf("Not Unique\n");
    }
    
    
//     for(i=0; i<row; ++i) // test the input is correct
//     {
//         for(j=0; j<col; ++j)
//         {
//             printf("%d ", pMatrix[i][j]);
//         }
//         printf("\n");
//     }
    
    free(pMatrix);
    return 0;
}


int judgeTol(void *Matrix, int row, int col, int posX, int posY, int tol)
{
    int (*pMatrix) [col] = (int (*)[col])Matrix;
    int i=0, j=0; // iterator
    
    i = posX - 1;
    if(i<0) i=0;
    j = posY - 1;
    if(j<0) j=0;
    for(; j<=posY+1 && j<col; ++j) // last row
    {
        if((abs(pMatrix[i][j] - pMatrix[posX][posY]) <= tol) && (i!=posX || j!=posY))
        {
            return 0;
        }
    }
    
    i = posX; // current row
    j = posY - 1;
    if(j<0) j=0;
    if((abs(pMatrix[i][j] - pMatrix[posX][posY]) <= tol) && (i!=posX || j!=posY)) return 0;
    
    i=posX;
    j = posY + 1;
    if(j>=col) j= col-1;
    if((abs(pMatrix[i][j] - pMatrix[posX][posY]) <= tol) && (i!=posX || j!=posY)) return 0;
    
    i = posX + 1;
    if(i>=row) i=row-1;
    j = posY - 1;
    if(j<0) j=0;
    for(; j<=posY+1 && j<col; ++j) // next row
    {
        if((abs(pMatrix[i][j] - pMatrix[posX][posY]) <= tol) && (i!=posX || j!=posY))
        {
            return 0;
        }
    }
    
    return 1;
    
//     for(i=0; i<row; ++i) // test the input is correct
//     {
//         for(j=0; j<col; ++j)
//         {
//             printf("%d ", pMatrix[i][j]);
//         }
//         printf("\n");
//     }
//     return 1;
    
}

int isUnique(void *Matrix, int row, int col, int posX, int posY)
{
    int (*pMatrix) [col] = (int (*)[col])Matrix;
    int i=0, j=0; // iterator
    
    for(i=0; i<row; ++i)
    {
        for(j=0; j<col; ++j)
        {
            if((pMatrix[i][j] == pMatrix[posX][posY]) && (i!=posX || j!=posY)) return 0;
        }
    }
    
    return 1;
}
posted @ 2023-04-04 20:01  十豆加日月  阅读(14)  评论(0编辑  收藏  举报