1068 万绿丛中一点红 (20 分)
题目链接
算法分析
题目本身不难,但是非常难get到出题人的全部意图,要注意以下几点:
(1)周围八个点(×)-----边界的点也要考虑在内,如何判断?(看代码的judge函数)
(2)差值-----差的绝对值
(3)唯一存在------颜色不重复
测试点
我这个题卡在测试点三,一开始是因为没有考虑只能出现一次的情况,后来用map修改后还是不行,因此又调了半天,最终发现是在“差值”上面出了问题,应该判断二者的差的绝对值。
代码实现
#include<bits/stdc++.h>
using namespace std;
#define N 1005
int num[N][N];
int x, y;
int t;
int m, n, tol;
int judge(int x, int y);
map<int, int>on;
int main(){
scanf("%d%d%d", &m, &n, &tol);
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j){
scanf("%d", &num[i][j]);
on[num[i][j]] ++;
}
for(int i = 1; i <= n; ++ i)
for(int j = 1; j <= m; ++ j){
bool flag = judge(i, j);
if(flag && on[num[i][j]] == 1){
++ t;
x = i, y = j;
}
}
if(t == 0) puts("Not Exist");
else if(t == 1) printf("(%d, %d): %d", y, x, num[x][y]);
else puts("Not Unique");
return 0;
}
int judge(int x, int y){
int is = 1;
int i_low, i_upp, j_low, j_upp;//需要遍历的图的行列边界值
if(x == 1) i_low = x, i_upp = x + 1;
else if(x == n) i_low = x - 1, i_upp = x;
else i_low = x - 1, i_upp = x + 1;
if(y == 1) j_low = y, j_upp = y + 1;
else if(y == n) j_low = y - 1, j_upp = y;
else j_low = y - 1, j_upp = y + 1;
for(int i = i_low; i <= i_upp; ++ i)
for(int j = j_low; j <= j_upp; ++ j){
if(i == x && j == y) continue;
if(abs(num[x][y] - num[i][j]) <= tol){
is = 0;
break;
}
}
return is;
}