POJ 1009 Edge Detection 解题报告 JAVA


Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 14821 Accepted: 3333

Description

IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compressed image, finds the edges in the image, as described below, and outputs another compressed image of the detected edges. 
A simple edge detection algorithm sets an output pixel's value to be the maximum absolute value of the differences between it and all its surrounding pixels in the input image. Consider the input image below: 

The upper left pixel in the output image is the maximum of the values |15-15|,|15-100|, and |15-100|, which is 85. The pixel in the 4th row, 2nd column is computed as the maximum of |175-100|, |175-100|, |175-100|, |175-175|, |175-25|, |175-175|,|175-175|, and |175-25|, which is 150. 
Images contain 2 to 1,000,000,000 (10 9) pixels. All images are encoded using run length encoding (RLE). This is a sequence of pairs, containing pixel value (0-255) and run length (1-10 9). Input images have at most 1,000 of these pairs. Successive pairs have different pixel values. All lines in an image contain the same number of pixels. 

Input

Input consists of information for one or more images. Each image starts with the width, in pixels, of each image line. This is followed by the RLE pairs, one pair per line. A line with 0 0 indicates the end of the data for that image. An image width of 0 indicates there are no more images to process. The first image in the example input encodes the 5x7 input image above. 

Output

Output is a series of edge-detected images, in the same format as the input images, except that there may be more than 1,000 RLE pairs. 

Sample Input

7
15 4
100 15
25 2
175 2
25 5
175 2
25 5
0 0
10
35 500000000
200 500000000
0 0
3
255 1
10 1
255 2
10 1
255 2
10 1
255 1
0 0
0

Sample Output

7
85 5
0 2
85 5
75 10
150 2
75 3
0 2
150 2
0 4
0 0
10
0 499999990
165 20
0 499999990
0 0
3
245 9
0 0
0

poj的前几道题都比较简单,1009算是比较难的一道题。这道题的难点在于:1、跳跃编程(把输入数组转化成表示图像的二维数组)。2、效率优化。看见到这道题时比较没头绪,后来上网看了看一些大牛的解题报告,渐渐地有了思路。接下来我们分三步,慢慢的来解这道题。

1、暴力求解

我做任何算法题都有一个习惯,如果一开始没能想到优越解的时候我会试着用暴力循环的方式把这道题给解出来,然后再试图优化。暴力求解的过程中我们把难点一解决了。就是把通过输入数组得到某个点的值。例如:


我们想获得左边为(2,3)那个像素点的值(结果是100),我们可以计算改点离(0,0)点共相距10个,那么通过输入数据的第二列便可获得100的结果。

代码如下:

package poj1009;

import java.util.Scanner;


public class Main1 {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int m,n,cp,cpl,index,mn;//m图像像素的行数,n列数,cp输出结果的编码,cpl对应cp的出现次数,mn总共有像素点的数
		int[][] dt ;//用来装二维数组
		while(true){
			n = cin.nextInt();
			if(n==0){
				break;
			}
			System.out.println(n);
			index = 0;
			dt = new int[2][10000];
			while(true){
				dt[0][index] = cin.nextInt();
				dt[1][index] = cin.nextInt();
				if(dt[0][index]==0 && dt[1][index]==0){
					break;
				}
				index++;
			}
			mn=0;
			//把输入数组的第二列相加就能得到所有像素点的数量
			for(int i=0; i<10000;i++){
				if(dt[i][i]==0){
					break;
				}
				mn+=dt[1][i];
			}
			//获得行数
			m = mn/n;
			cp = getM(dt,0,0,m,n);//getM()方法用来获得某一像素点的编码值,名字取得不好
			cpl=0;
			
			//两层for循环,暴力求每一个(i,j)点的结果,从而得到整道题的结果
			for(int i=0; i<m; m++){
				for(int j=0; j<n; j++){
					if(getM(dt,i,j,m,n)==cp){
						cpl++;
					}else{
						System.out.println(cp + " " + cpl);
						cp = getM(dt,i,j,m,n);
						cpl=1;
					}
				}
			}
			System.out.println(cp + " " + cpl);
			System.out.println("0 0");
		}
		System.out.println("0");
		System.exit(0);
	}

	
	
	//取(x,y)点的编码结果
	public static int getM(int[][] dt, int x, int y, int m, int n) {
		int max = 0;
		for(int i=-1; i<2; i++){
			for(int j=-1; j<2; j++){
				if(i==0 && j==0){
					continue;
				}else if(x==0 && i==-1){
					continue;
				}else if(x==(m-1) && i==1){
					continue;
				}else if(y==0 && j==-1){
					continue;
				}else if(y==n-1 && j==1){
					continue;
				}else {
					if(max<Math.abs(dt[0][getPositionOfPoint(dt,x,y,n)]-dt[0][getPositionOfPoint(dt,x+i,y+j,n)])){
						max = Math.abs(dt[0][getPositionOfPoint(dt,x,y,n)]-dt[0][getPositionOfPoint(dt,x+i,y+j,n)]);
					}
				}
			}
		}
		
		return max;
	}

	//取(x,y)点映射到输入数组的位置,即dt数组中的位置,返回dt的二维下标
	private static int getPositionOfPoint(int[][] dt, int x, int y, int n) {
		int t = x*n + y + 1;
		int total = 0;
		int result = 0;
		if(total<t){
			total+=dt[0][result];
			result++;
		}
		return result-1;
	}

}


2、行内优化

通过上面的代码我们至少能够把这个问题给解出来,但它显然不能AC,因为题目的数据量太大。那么我们试着做一些优化。看下面这种情况


假如我们已经获得(2,0)点的编码结果,而且从(2,0)点到(2,2)这几点的周围9点的数值完全一样,那我们就不必去循环计算这些点,这样我们就可以把每一行里像这样的点的计算时间节省。具体代码如下:

package poj1009;

import java.util.Scanner;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;

public class Main2 {
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		int m, n, cp, cpl, index, mn;//m图像像素的行数,n列数,cp输出结果的编码,cpl对应cp的出现次数,mn总共有像素点的数
		int[][] dt;//用来装二维数组
		while (true) {
			n = cin.nextInt();
			if (n == 0) {
				break;
			}
			System.out.println(n);
			index = 0;
			dt = new int[2][10000];
			while (true) {
				dt[0][index] = cin.nextInt();
				dt[1][index] = cin.nextInt();
				if (dt[0][index] == 0 && dt[1][index] == 0) {
					break;
				}
				index++;
			}
			mn = 0;
			for (int i = 0; i < 10000; i++) {
				if (dt[1][i] == 0) {
					break;
				}
				mn += dt[1][i];
			}
			m = mn / n;
			cp = getM(dt, 0, 0, m, n);
			cpl = 0;
			int flag = 0;
			for (int i = 0; i < m; i++) {
				for (int j = 0; j < n; j++) {
					if (getM(dt, i, j, m, n) == cp) {
						cpl++;
					} else {
						System.out.println(cp + " " + cpl);
						cp = getM(dt, i, j, m, n);
						cpl = 1;
					}

					if(getMinSameCnt(dt, i, j, m, n)>=3 && j+getMinSameCnt(dt, i, j, m, n)-1<n){
						if(getM(dt, i, j+1, m, n) == cp){
							cpl += getMinSameCnt(dt, i, j, m, n) - 1;
						}else {
							System.out.println(cp + " " + cpl);
							cp = getM(dt,i,j+1,m,n);
							cpl = getMinSameCnt(dt, i, j, m, n) -1;
						}
						j += getMinSameCnt(dt, i, j, m, n) - 1;
					}
				}
			}
			System.out.println(cp + " " + cpl);
			System.out.println("0 0");
		}
		System.out.println("0");
		System.exit(0);
	}

	// 取(x,y)点的编码结果
	public static int getM(int[][] dt, int x, int y, int m, int n) {
		int max = 0;
		for (int i = -1; i < 2; i++) {
			for (int j = -1; j < 2; j++) {
				if (i == 0 && j == 0) {
					continue;
				} else if (x == 0 && i == -1) {
					continue;
				} else if (x == (m - 1) && i == 1) {
					continue;
				} else if (y == 0 && j == -1) {
					continue;
				} else if (y == n - 1 && j == 1) {
					continue;
				} else {
					int a = dt[0][getPositionOfPoint(dt, x, y, n)];
					int b = dt[0][getPositionOfPoint(dt, x + i, y + j, n)];
					int temp = a - b;
					if (max < Math.abs(dt[0][getPositionOfPoint(dt, x, y, n)]
							- dt[0][getPositionOfPoint(dt, x + i, y + j, n)])) {
						max = Math
								.abs(dt[0][getPositionOfPoint(dt, x, y, n)]
										- dt[0][getPositionOfPoint(dt, x + i, y
												+ j, n)]);
					}
				}
			}
		}

		return max;
	}

	// 取(x,y)点映射到输入数组的位置,即dt数组中的位置,返回dt的二维下标
	private static int getPositionOfPoint(int[][] dt, int x, int y, int n) {
		int t = x * n + y + 1;
		int total = 0;
		int result = 0;
		while (total < t) {
			total += dt[1][result];
			result++;
		}
		return result - 1;
	}

	//或者x行在(x,y)点后值与(x,y)相等的点数
	private static int getTheSameVlaueCnt(int[][] dt, int x, int y, int n) {
		int t = x * n + y + 1;
		int total = 0;
		int temp = 0;
		while (t > total) {
			total += dt[1][temp];
			temp++;
		}
		return total - t;
	}
	
	
	//返回x行 x-1行 x+1行 三行里在各自行后面重复出现元素最小数量
	private static int getMinSameCnt(int[][] dt, int x, int y, int m, int n) {
		if (m == 1) {
			return getTheSameVlaueCnt(dt, x, y, n);
		} else if (m == 2 || x == 0) {
			return getMin(getTheSameVlaueCnt(dt, 0, y, n),
					getTheSameVlaueCnt(dt, 1, y, n));
		} else if (x == m - 1) {
			return getMin(getTheSameVlaueCnt(dt, m - 1, y, n),
					getTheSameVlaueCnt(dt, x - 2, y, n));
		} else {
			return getMin(
					getMin(getTheSameVlaueCnt(dt, x, y, n),
							getTheSameVlaueCnt(dt, x - 1, y, n)),
					getMin(getTheSameVlaueCnt(dt, x, y, n),
							getTheSameVlaueCnt(dt, x + 1, y, n)));
		}
	}

	private static int getMin(int a, int b) {
		if (a < b) {
			return a;
		}
		return b;
	}

}

3、行间优化

上面一段代码在行内做了优化,但是像下面这样的场景我们还可以在行之间做优化

     1010
10101010101010
10101010101010
10101010101010
10101010101010

假设我们求得了红色格的值,根据题目要求第三行和第四行的编码值应该都为0,这样就又节省了两行的计算时间。具体代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedReader(new InputStreamReader(
				System.in)));
		int m, n, cp, cpl, index, mn;
		int[][] dt;
		while (true) {
			n = cin.nextInt();
			if (n == 0) {
				break;
			}
			System.out.println(n);
			index = 0;
			dt = new int[2][10000];
			while (true) {
				dt[0][index] = cin.nextInt();
				dt[1][index] = cin.nextInt();
				if (dt[0][index] == 0 && dt[1][index] == 0) {
					break;
				}
				index++;
			}
			mn = 0;
			for (int i = 0; i < 10000; i++) {
				if (dt[1][i] == 0) {
					break;
				}
				mn += dt[1][i];
			}
			m = mn / n;
			cp = getM(dt, 0, 0, m, n);
			cpl = 0;
			int flag = 0;
			for (int i = 0; i < m; i++) {
				if (dt[1][getPositionOfPoint(dt, i, 0, n)] - flag > 2 * n
						&& flag >= n) {
					int t = dt[1][getPositionOfPoint(dt, i, 0, n)] - flag
							- (dt[1][getPositionOfPoint(dt, i, 0, n)] - flag)
							% n - n;
					if(cp == 0){
						cpl += t;
					}else {
						System.out.println(cp + " " + cpl);
						cp = 0;
						cpl = t;
					}
					flag += t;
					i = i + t/n;
				}
				for (int j = 0; j < n; j++) {
					if (getM(dt, i, j, m, n) == cp) {
						cpl++;
					} else {
						System.out.println(cp + " " + cpl);
						cp = getM(dt, i, j, m, n);
						cpl = 1;
					}
					flag++;
					if (flag >= dt[1][getPositionOfPoint(dt, i, j, n)]) {
						flag = 0;
					}
					if (getMinSameCnt(dt, i, j, m, n) >= 3
							&& j + getMinSameCnt(dt, i, j, m, n) - 1 < n) {
						if (getM(dt, i, j+1, m, n) == cp) {
							cpl += getMinSameCnt(dt, i, j, m, n) - 1;
						} else {
							System.out.println(cp + " " + cpl);
							cp = getM(dt, i, j+1, m, n);
							cpl = getMinSameCnt(dt, i, j, m, n) - 1;
						}
						flag += getMinSameCnt(dt, i, j, m, n) - 1;
						j += getMinSameCnt(dt, i, j, m, n) - 1;
					}
				}
			}
			System.out.println(cp + " " + cpl);
			System.out.println("0 0");
		}
		System.out.println(0);
	}

	// 取(x,y)点的编码结果
	public static int getM(int[][] dt, int x, int y, int m, int n) {
		int max = 0;
		for (int i = -1; i < 2; i++) {
			for (int j = -1; j < 2; j++) {
				if (i == 0 && j == 0) {
					continue;
				} else if (x == 0 && i == -1) {
					continue;
				} else if (x == (m - 1) && i == 1) {
					continue;
				} else if (y == 0 && j == -1) {
					continue;
				} else if (y == n - 1 && j == 1) {
					continue;
				} else {
					int a = dt[0][getPositionOfPoint(dt, x, y, n)];
					int b = dt[0][getPositionOfPoint(dt, x + i, y + j, n)];
					int temp = a - b;
					if (max < Math.abs(dt[0][getPositionOfPoint(dt, x, y, n)]
							- dt[0][getPositionOfPoint(dt, x + i, y + j, n)])) {
						max = Math
								.abs(dt[0][getPositionOfPoint(dt, x, y, n)]
										- dt[0][getPositionOfPoint(dt, x + i, y
												+ j, n)]);
					}
				}
			}
		}

		return max;
	}
	

	// 取(x,y)点映射到输入数组的位置,即dt数组中的位置,返回dt的二维下标
	private static int getPositionOfPoint(int[][] dt, int x, int y, int n) {
		int t = x * n + y + 1;
		int total = 0;
		int result = 0;
		while (total < t) {
			total += dt[1][result];
			result++;
		}
		return result - 1;
		
	}

	private static int getTheSameVlaueCnt(int[][] dt, int x, int y, int n) {
		int t = x * n + y + 1;
		int total = 0;
		int temp = 0;
		while (t > total) {
			total += dt[1][temp];
			temp++;
		}
		return total - t;
	}

	// 返回x行 x-1行 x+1行 三行里在各自行后面重复出现元素最小数量
	/**
	 * 
	 */
	private static int getMinSameCnt(int[][] dt, int x, int y, int m, int n) {
		if (m == 1) {
			return getTheSameVlaueCnt(dt, x, y, n);
		} else if (m == 2 || x == 0) {
			return getMin(getTheSameVlaueCnt(dt, 0, y, n),
					getTheSameVlaueCnt(dt, 1, y, n));
		} else if (x == m - 1) {
			return getMin(getTheSameVlaueCnt(dt, m - 1, y, n),
					getTheSameVlaueCnt(dt, m - 2, y, n));
		} else {
			return getMin(
					getMin(getTheSameVlaueCnt(dt, x, y, n),
							getTheSameVlaueCnt(dt, x - 1, y, n)),
					getMin(getTheSameVlaueCnt(dt, x, y, n),
							getTheSameVlaueCnt(dt, x + 1, y, n)));
		}
	}

	private static int getMin(int a, int b) {
		if (a < b) {
			return a;
		}
		return b;
	}
	
	

}

总结:经过两次优化,最后的代码可以AC了。不过虽然能AC,在很多细节上还可以进一步优化。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值