题目链接:
https://pintia.cn/problem-sets/994805260223102976/problems/994805265579229184
我的错误代码(求订正,):
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
const int maxn=1001;
long long dectec[maxn][maxn];
int main(){
int M,N,TOL;
scanf("%d%d%d",&M,&N,&TOL);
for(int i=0;i<N;i++)
{
for(int j=0;j<M;j++)
{
scanf("%lld",&dectec[i][j]);
}
}
int x=0,y=0;//记录 一点红的点
int cnt=0;//记录次数
for(int i=1;i<N-1;i++){
for(int j=1;j<M-1;j++){
if(((dectec[i][j]-dectec[i-1][j-1])>=TOL)
&&((dectec[i][j]-dectec[i-1][j])>=TOL)
&&((dectec[i][j]-dectec[i-1][j+1])>=TOL)
&&((dectec[i][j]-dectec[i][j-1])>=TOL)
&&((dectec[i][j]-dectec[i][j+1])>=TOL)
&&((dectec[i][j]-dectec[i+1][j-1])>=TOL)
&&((dectec[i][j]-dectec[i+1][j])>=TOL)
&&((dectec[i][j]-dectec[i+1][j+1])>=TOL)
){
cnt++;
x=i;
y=j;
}
}
}
if(cnt==1){
printf("(%d, %d): %lld\n",x,y,dectec[x][y]);
}
else if(cnt>1){
printf("Not Unique\n");
}
else if(cnt==0){
printf("Not Exist\n");
}
return 0;
}
AC代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <cstring>
using namespace std;
const int maxn=1001;
map<int,int> vis;
int s[maxn][maxn];
int n,m,tol;
int dir[8][2]={1, 0, //正下方
-1, 0, //正上方
0, 1, //正右方
0, -1, //正左方
1, 1, //右下角
1, -1, //左下角
-1, 1, //右上角
-1 -1 }; // 左上角
bool check(int x,int y){
for(int i=0;i<8;i++){
int xx=x+dir[i][0];
int yy=y+dir[i][1];
if(xx>=0&&xx<n&&yy<m&&yy>=0&&abs(s[xx][yy]-s[x][y])<=tol)
return false;
}
return true;
}
int main(){
scanf("%d%d%d",&m,&n,&tol);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&s[i][j]);
vis[s[i][j]]++;
}
}
//cnt只记录出现过一次的数字的个数
//x,y记录坐标
int cnt=0;
int x,y;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(vis[s[i][j]]==1&&check(i,j)){
cnt++;
x=i;
y=j;
}
}
}
if(cnt==1){
printf("(%d, %d): %d\n",y+1,x+1,s[x][y]);
}
else if(cnt>1){
printf("Not Unique\n");
}
else{
printf("Not Exist\n");
}
return 0;
}