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

1068. 万绿丛中一点红(20) PAT

2017-06-16 10:28 369 查看


1068. 万绿丛中一点红(20)

时间限制

500 ms

内存限制

65536 kB

代码长度限制

8000 B

判题程序

Standard

作者

CHEN, Yue

对于计算机而言,颜色不过是像素点对应的一个24位的数值。现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大。

输入格式:

输入第一行给出三个正整数,分别是M和N(<= 1000),即图像的分辨率;以及TOL,是所求像素点与相邻点的颜色差阈值,色差超过TOL的点才被考虑。随后N行,每行给出M个像素的颜色值,范围在[0, 224)内。所有同行数字间用空格或TAB分开。

输出格式:

在一行中按照“(x, y): color”的格式输出所求像素点的位置以及颜色值,其中位置x和y分别是该像素在图像矩阵中的列、行编号(从1开始编号)。如果这样的点不唯一,则输出“Not Unique”;如果这样的点不存在,则输出“Not Exist”。
输入样例1:
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

输出样例1:
(5, 3): 16711680

输入样例2:
4 5 2
0 0 0 0
0 0 3 0
0 0 0 0
0 5 0 0
0 0 0 0

输出样例2:
Not Unique

输入样例3:
3 3 5
1 2 3
3 4 5
5 6 7

输出样例3:
Not Exist


#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int a[1005][1005];
//测试点3 abs
//测试点5 独一无二
int main(){
int m,n,tol;
cin>>m>>n>>tol;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int count=0;
int x,y,ans;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){

//int isok1=0,isok2=0;
int isok=0;
int valid=0;
//cout<<i<<","<<j<<":";
for(int k=-1;k<=1;k++){
if(i+k<1||i+k>n){
continue;
}
for(int q=-1;q<=1;q++){
if(j+q<1||j+q>m){
continue;
}
valid++;
//cout<<"("<<i+k<<","<<j+q<<")"<<"  ";
//					if(a[i+k][j+q]-a[i][j]>tol){
//						//cout<<i+k<<" "<<j+q<<endl;
//						//cout<<a[i][j]<<"-"<<a[i+k][j+q]<<"="<<a[i+k][j+q]-a[i][j]<<endl;
//						isok1++;
//					}
//
//					if(a[i][j]-a[i+k][j+q]>tol){
//						//cout<<i+k<<" "<<j
bea0
+q<<endl;
//						//cout<<a[i][j]<<"-"<<a[i+k][j+q]<<"="<<a[i+k][j+q]-a[i][j]<<endl;
//						isok2++;
//					}
//
if(abs(a[i+k][j+q]-a[i][j])>tol){
isok++;
}
}
}
//cout<<valid<<endl;
//			if(isok1==valid-1){
//				cout<<valid<<endl;
//				count++;
//				x=j;
//				y=i;
//				ans=a[i][j];
//				cout<<ans<<endl;
//			}
//			if(isok2==valid-1){
//				cout<<"~~"<<valid<<endl;
//				count++;
//				x=j;
//				y=i;
//				ans=a[i][j];
//				cout<<ans<<endl;
//			}

if(isok==valid-1){
//判断是否唯一
int tag=0;
for(int row=1;row<=n;row++){
for(int col=1;col<=m;col++){
if(a[row][col]==a[i][j]){
tag++;
}
}
if(tag==2)break;
}
if(tag==1){
count++;
x=j;
y=i;
ans=a[i][j];
//cout<<ans<<endl;
}
}
if(count>1){
break;
}
//	cout<<endl;
}
if(count>1){
break;
}
}
//cout<<count<<endl;
if(count==0){
cout<<"Not Exist"<<endl;
}else if(count==1){
cout<<"("<<x<<", "<<y<<"): "<<ans<<endl;
}else if(count>1){
cout<<"Not Unique"<<endl;
}

return 0;
}
以上是面目全非思路版。

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
int a[1005][1005];
//测试点3 abs
//测试点5 独一无二
int main(){
int m,n,tol;
cin>>m>>n>>tol;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
}
}
int count=0;
int x,y,ans;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
int isok=0;
int valid=0;

for(int k=-1;k<=1;k++){
if(i+k<1||i+k>n){
continue;
}
for(int q=-1;q<=1;q++){
if(j+q<1||j+q>m){
continue;
}
valid++;
if(abs(a[i+k][j+q]-a[i][j])>tol){
isok++;
}
}
}
if(isok==valid-1){
//判断是否唯一
int tag=0;
for(int row=1;row<=n;row++){
for(int col=1;col<=m;col++){
if(a[row][col]==a[i][j]){
tag++;
}
}
if(tag==2)break;
}
if(tag==1){
count++;
x=j;
y=i;
ans=a[i][j];
}
}
if(count>1){
break;
}

}
if(count>1){
break;
}
}

if(count==0){
cout<<"Not Exist"<<endl;
}else if(count==1){
cout<<"("<<x<<", "<<y<<"): "<<ans<<endl;
}else if(count>1){
cout<<"Not Unique"<<endl;
}

return 0;
}

这道题,分明就是一道阅读题!

对于计算机而言,颜色不过是像素点对应的一个24位的数值。现给定一幅分辨率为MxN的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围8个相邻像素的颜色差充分大

条件1:这个颜色是独一无二的。

条件2:这个点的颜色必须与周围8个相邻像素的颜色充分大。

条件3:如果这个颜色的在矩形边界,也必须考察它与周围的颜色的插值足够大。

条件4:以上所说的差值,就是个abs()。(我开始以为,这个颜色必须是九宫格里最大的,或者是九宫格里最小的)

网上搜到的题解,菊苣们用了map来保存颜色在图片中的个数。很好的想法。为什么我没想到呢。

因为我当时根本就没看到独一无二这个条件。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT 考研机试 C++