(测试点3、5答案不对;测试点4内存超出限制了。。。。。。)
1.题目如下:
对于计算机而言,颜色不过是像素点对应的一个 24 位的数值。现给定一幅分辨率为 M×N
的画,要求你找出万绿丛中的一点红,即有独一无二颜色的那个像素点,并且该点的颜色与其周围 8 个相邻像素的颜色差充分大。
输入格式:
输入第一行给出三个正整数,分别是 M 和 N(≤ 1000),即图像的分辨率;以及 TOL,是所求像素点与相邻点的颜色差阈值,色差超过
TOL 的点才被考虑。随后 N 行,每行给出 M 个像素的颜色值,范围在 [0,2 24 ) 内。所有同行数字间用空格或 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
代码长度限制 16 KB
Java (javac)
时间限制 600 ms 内存限制 64 MB
Python (python2)
时间限制1400 ms 内存限制 64 MB
Python (python3)
时间限制 1400 ms 内存限制 64 MB
其他编译器
时间限制400 ms 内存限制 64 MB
2.代码如下:
package pat_1067;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class testPractice1068 {
public static void main(String[] args) {
try {
BufferedReader bf1=new BufferedReader(new InputStreamReader(System.in));
String str1=bf1.readLine();
int row=Integer.parseInt(str1.split(" ")[1]);
int list=Integer.parseInt(str1.split(" ")[0]);
int threshold=Integer.parseInt(str1.split(" ")[2]);
String[][] nums=new String[row][list];
int [][] isRed=new int[row][list];
for(int i=0;i<row;i++) {
String temp=bf1.readLine();
for(int j=0;j<list;j++) {
nums[i][j]=temp.split("\\s+")[j];
}
}
int[][] round= {{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
int same=-1;
int number=0;
int rowNumber=0;
int listNumber=0;
for(int i=1;i<row-1;i++) {
for(int j=1;j<list-1;j++) {
isRed[i][j]=1;
for(int k=0;k<8;k++) {
if(i+round[k][0]>=0 &&i+round[k][0]<row&&j+round[k][1]<list&&j+round[k][1]>=0 && isRed[i][j]!=0) {
int temp1=Integer.parseInt(nums[i][j]);
int temp2=Integer.parseInt(nums[i+round[k][0]][j+round[k][1]]);
if((temp1-temp2)<=threshold) {
isRed[i][j]=0;
//isRed[i+round[k][0]][j+round[k][1]]=0;
}
}
}
if(isRed[i][j]==1) {
number=Integer.parseInt(nums[i][j]);
rowNumber=i+1;
listNumber=j+1;
same++;
}
}
}
if(same==-1) {
System.out.println("Not Exist");
}
if(same==0) {
System.out.printf("(%d, %d): %d",listNumber,rowNumber,number);
}
if(same>=1) {
System.out.println("Not Unique");
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}