(测试点3超时了,不知道该用什么方法去降低复杂度)
1.题目如下:
图像过滤是把图像中不重要的像素都染成背景色,使得重要部分被凸显出来。现给定一幅黑白图像,要求你将灰度值位于某指定区间内的所有像素颜色都用一种指定的颜色替换。
输入格式:
输入在第一行给出一幅图像的分辨率,即两个正整数 M 和 N(0<M,N≤500),另外是待过滤的灰度值区间端点 A 和
B(0≤A<B≤255)、以及指定的替换灰度值。随后 M 行,每行给出 N 个像素点的灰度值,其间以空格分隔。所有灰度值都在 [0,
255] 区间内。
输出格式:
输出按要求过滤后的图像。即输出 M 行,每行 N 个像素灰度值,每个灰度值占 3 位(例如黑色要显示为
000),其间以一个空格分隔。行首尾不得有多余空格。
输入样例:
3 5 100 150 0
3 189 254 101 119
150 233 151 99 100
88 123 149 0 255
输出样例:
003 189 254 000 000
000 233 151 099 000
088 000 000 000 255
代码长度限制 16 KB 时间限制 400 ms 内存限制 64 MB
2.代码如下:
package pat_1066;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class testPractice1066 {
public static void main(String[] args) {
try {
BufferedReader bf1=new BufferedReader(new InputStreamReader(System.in));
String str1=bf1.readLine();
int N=Integer.parseInt(str1.split(" ")[0]);
int factorNumber=Integer.parseInt(str1.split(" ")[1]);
int factorLow=Integer.parseInt(str1.split(" ")[2]);
int factorHigh=Integer.parseInt(str1.split(" ")[3]);
int correctNum=Integer.parseInt(str1.split(" ")[4]);
for(int i=0;i<N;i++) {
String temp=bf1.readLine();
ArrayList<String> factorLine=new ArrayList<String>(Arrays.asList(temp.split(" ")));
for(int j=0;j<factorNumber;j++) {
if(Integer.parseInt(factorLine.get(j))<=factorHigh && Integer.parseInt(factorLine.get(j))>=factorLow) {
if(j!=factorNumber-1) {
System.out.printf("%03d ",correctNum);
}
else {
System.out.printf("%03d",correctNum);
}
}
else {
if(j!=factorNumber-1) {
System.out.printf("%03d ",Integer.parseInt(factorLine.get(j)));
}
else {
System.out.printf("%03d",Integer.parseInt(factorLine.get(j)));
}
}
}
System.out.printf("\n");
}
}catch(Exception ex) {
ex.printStackTrace();
}
}
}