题源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的就可以不求颜色差了(懒得改代码了)