PAT乙级|1068 万绿丛中一点红(测试点3)

 题源https://pintia.cn/problem-sets/994805260223102976/exam/problems/994805265579229184

 

1. 题意解释

(1)颜色差=像素点差值的绝对值

(2)边界点也需考虑,如左上角的点(1, 1)点需与(2, 1) (1, 2) (2, 2)比较

(3)像素值在所有像素值中只出现一次,且满足颜色差充分大的像素值才是符合条件的像素值!(测试点3)

(4)满足3的像素值如果有多个,则输出“Not Unique”(题中输入样例2)

2. 测试点3的数据(猜测)

样例1:(满足颜色差足够大的点后出现)

4 5 3

0 5 5 5

0 0 0 0

0 0 0 0

5 0 0 0

0 0 0 0

样例2:  (满足颜色差足够大的点先出现)

4 5 3

0 0 5 0

0 0 0 0

0 0 0 0

0 0 0 0

0 5 5 0

3. AC代码

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <vector>
#include <iomanip>
#include <map>
#define MAX_SIZE 100000
using namespace std;

struct pos {
    int cnt = 0, i, j, flag = 1;
};

int main() {
//    freopen("/Users/susu/Library/Developer/Xcode/DerivedData/data-fpaniiksgwkelwahnecbdtczbbwf/Build/Products/Debug/data.in", "r", stdin);
//    freopen("test1.out", "w", stdout);
    int m, n, tol;
    cin >> m >> n >> tol;
    int mp[1000][1000];
    map<int, pos> point;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++) {
            cin >> mp[i][j];
            pos temp;
            temp.cnt = 0; // 该像素值出现次数初始化
            temp.i = i;
            temp.j = j;
            point.insert(make_pair(mp[i][j], temp));
        }
    // 求出满足颜色差充分大的点
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < m; j++) {
            point[mp[i][j]].cnt ++; // 统计该像素值出现次数(也可在初始化时统计,cnt>1的就可以不求颜色差了)
            int flag = 0; // flag==0表示该点与其周围像素点的颜色差充分大
            for(int p = max(i - 1, 0); p <= min(i + 1, n - 1); p++) { // max、min防止越界
                if(flag) break;
                for(int q = max(j - 1, 0); q <= min(j + 1, m - 1); q++) {
                    if(p == i && q == j) continue;
                    if(abs(mp[i][j] - mp[p][q]) <= tol) {
                        flag = 1;
                        break;
                    }
                }
            }
            point[mp[i][j]].flag = flag;
        }
    }
    // 统计满足条件的点的数量
    int cnt = 0, posi = 0, posj = 0;
    for(map<int, pos>::iterator it = point.begin(); it != point.end(); it++) {
        if(it->second.flag == 0 && it->second.cnt == 1) {
            cnt ++;
            posi = it->second.i;
            posj = it->second.j;
        }
    }
    if(cnt == 1)
        printf("(%d, %d): %d", posj+1, posi+1, mp[posi][posj]);
    else if(cnt > 1)
        cout << "Not Unique";
    else cout << "Not Exist";
}

4. 优化

代码36行:统计该像素值出现次数:也可在初始化时统计,cnt>1的就可以不求颜色差了(懒得改代码了)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值