您的位置:首页 > 编程语言 > C语言/C++

pat乙级真题 1068. 万绿丛中一点红(20)

2017-12-02 01:23 295 查看

题目链接

                  
点击打开链接

注意事项

这道题目逻辑上不难,但坑很多。1 特殊数所对应的数字只能出现一次。2输出特殊数注意列在前,行在后。3 寻找一个数的周围八个数字时,要注意不能溢出。

代码

#include <stdlib.h>
#include <stdio.h>

int flag[2<<24] ={0}, //标志每个数字出现的次数
_a[1000][1000];
bool isSpecial(int m,int n,int M,int N,int _tot){
if(flag[_a[m]
] !=1) return false; //该数字出现多次就不是特殊数
if(m-1 >=0 && n-1 >=0 && abs(_a[m-1][n-1] - _a[m]
)<=_tot) return false;
if(m-1 >=0 && abs(_a[m-1]
- _a[m]
)<=_tot) return false;
if(m-1 >=0 && n+1 < M && abs(_a[m-1][n+1] - _a[m]
)<=_tot) return false;
if(m >=0 && n+1 < M && abs(_a[m][n+1] - _a[m]
)<=_tot) return false;
if(m+1 < N && n+1 < M && abs(_a[m+1][n+1] - _a[m]
)<=_tot) return false;
if(m+1 < N && abs(_a[m+1]
- _a[m]
)<=_tot) return false;
if(m+1 <N && n-1 >=0 && abs(_a[m+1][n-1] - _a[m]
)<=_tot) return false;
if( n-1 >=0 && abs(_a[m][n-1] - _a[m]
)<=_tot) return false;
return true;
}
int main(){
int M,N,tot,specal_point[2],count= 0;
scanf("%d %d %d",&M,&N,&tot);
for(int i =0;i<N;i++){
for(int j = 0;j<M;j++){
scanf("%d",&_a[i][j]);
flag[_a[i][j]]++;
}
}

for(int i = 0;i <N;i++){
for(int j = 0;j<M;j++){
if(isSpecial(i,j,M,N,tot)){
count++;
specal_point[1] = i+1;
specal_point[0] =j+1;
}
}
}
if(count == 0)
printf("Not Exist");
if(count>=2)
printf("Not Unique");
if(count == 1)
printf("(%d, %d): %d",specal_point[0],specal_point[1],_a[specal_point[1]-1][specal_point[0] -1]);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat 乙级 c语言