这道题注意题目中的“独一无二”,就是必须这个点只能出现一次。与其8个周围的点并不是指只能在内圈进行扫描,在
最外圈也必须扫描,8个周围的点意思表达不明确。最后没法通过一个超时的例子,需要把cin换成scanf,因为cin效率比
scanf效率低很多。这就是这道题的几个坑点。下面代码:
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int tol, n, m;
vector<int> v[1000];
int dir[8][2] = { { -1, -1 },{ -1, 0 },{ -1, 1 },{ 0, 1 },{ 1, 1 },{ 1, 0 },{ 1, -1 },{ 0, -1 } };
bool judge(int i, int j) {
for (int k = 0; k < 8;k++) {
int tx = j + dir[k][0];
int ty = i + dir[k][1];
if (tx >= 0 && tx < n && ty >= 0 && ty < m && v[i][j] - v[ty][tx] >= 0 - tol && v[i][j] - v[ty][tx] <= tol) {
return false;
}
}
return true;
}
int main() {
int count = 0, x = 0, y = 0, c, data = 0;
cin >> n >> m >> tol;
std::ios::sync_with_stdio(false);//此处是让cin效率提高,变得和scanf一样快
map<int, int> mapp;
for (int i = 0; i < m;i++) {
for (int j = 0; j < n; j++) {
cin >> c;
std::ios::sync_with_stdio(false);
v[i].push_back(c);
mapp[v[i][j]]++;
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mapp[v[i][j]] == 1 && judge(i,j)==true) {
y = i + 1;
x = j + 1;
data = v[i][j];
count++;
}
if (count == 2) break;
}
if (count == 2) break;
}
if (count == 2) {
cout << "Not Unique";
}
else if (count == 0) {
cout << "Not Exist";
}
else if (count == 1) {
cout << "(" << x << ", " << y << ")" << ": " << data;
}
system("pause");
return 0;
}