写在前面
- 二维数组存储像素点
- map存储是否唯一值
- 周围8个像素点
- 未确定边上像素点是否计入在内,
- 扩大数组2行,2列,边上像素点(不满足8个邻居)也参与计算
- 20分钟a题
测试用例
input:
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:
(5, 3): 16711680
input:
4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0
output:
Not Unique
input:
3 3 5
1 2 3
3 4 5
5 6 7
output:
Not Exist
ac代码
#include
#include
using namespace std;
int main()
{
int col,row,tol;
scanf("%d %d %d\n", &col, &row, &tol);
int a[row+2][col+2] = {
0};
map<int, int> pixels;
for(int i=1; i<=row; i++)
for(int j=1; j<=col; j++)
{
scanf("%d", &a[i][j]);
pixels[a[i][j]] += 1;
}
int x, y, cnt=0;
bool flag = false;
for(int i=1; i<=row; i++)
{
for(int j=1; j<=col; j++)
{
if((abs(a[i][j]-a[i-1][j-1])>tol)&&
(abs(a[i][j]-a[i-1][j])>tol)&&
(abs(a[i][j]-a[i-1][j+1])>tol)&&
(abs(a[i][j]-a[i][j+1])>tol)&&
(abs(a[i][j]-a[i+1][j+1])>tol)&&
(abs(a[i][j]-a[i+1][j])>tol)&&
(abs(a[i][j]-a[i+1][j-1])>tol)&&
(abs(a[i][j]-a[i][j-1])>tol)&&
(pixels[a[i][j]]==1))
{
x = i;
y = j;
cnt++;
if(cnt>1)
{
flag = true;
break;
}
}
}
if(flag) break;
}
if(cnt==1) printf("(%d, %d): %d", y, x, a[x][y]);
if(cnt>1) printf("Not Unique");
if(cnt<=0) printf("Not Exist");
return 0;
}
参考代码
#include
#include
#include
using namespace std;
int m, n, tol;
vector<vector<int>> v;
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 = i + dir[k][0];
int ty = j + dir[k][1];
if (tx >= 0 && tx < n && ty >= 0 && ty < m && v[i][j] - v[tx][ty] >= 0 - tol && v[i][j] - v[tx][ty] <= tol) return false;
}
return true;
}
int main() {
int cnt = 0, x = 0, y = 0;
scanf("%d%d%d", &m, &n, &tol);
v.resize(n, vector<int>(m));
map<int, int> mapp;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &v[i][j]);
mapp[v[i][j]]++;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mapp[v[i][j]] == 1 && judge(i, j) == true) {
cnt++;
x = i + 1;
y = j + 1;
}
}
}
if (cnt == 1)
printf("(%d, %d): %d", y, x, v[x-1][y-1]);
else if (cnt == 0)
printf("Not Exist");
else
printf("Not Unique");
return 0;
}