POJ 1009 Edge Detection 解题报告 JAVA

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情


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 (109) 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-109). 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,在很多细节上还可以进一步优化。





EdgeDetectionUsingACOA:使用蚁群优化算法(Java)的图像边缘检测 使用ACOA进行边缘检测 使用蚁群优化算法(Java)的图像边缘检测 立即下载

相关推荐

poj1009 Edge Detection 可以直接AC的

poj1009 Edge Detection 可以直接AC的

POJ 1009 Edge Detection(模拟)

Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17586   Accepted: 4020 Description IONU Satellite Imaging, Inc. records and stores very larg

风尘_浪子 1070

Sobel-Edge-Detection

A soble edge function using basic convolutions. CImg library is used to display, read, and write image files.

北大oj1009——Edge Detection

Edge Detection | Time Limit: 1000MS | | Memory Limit: 10000K | Description IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program tha...

m0_37609239的博客 298

北大ACM 1009题—Edge Detection

Edge Detection Time Limit:1000MS Memory Limit:10000K Total Submissions:15307 Accepted:3445 Description IONU Satellite Imaging, Inc. records and stores very larg...

weixin_30258901的博客 257

POJ1009解题报告

题目: Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12340   Accepted: 2694 Description IONU Satellite Imaging, Inc. records and stores very lar

卧龙居 2684

ACM —— 1009 Edge Detection

解题代码: import java.util.Arrays; import java.util.HashMap; import java.util.Scanner; public class Main { static int mWidth, mHight; static int[][] pairs = new int[1000][2]; static HashMap

WYYZ5的专栏 1059

poj 1009 java_POJ 1009 Edge Detection 解题报告 JAVA

Edge DetectionTime Limit:1000MSMemory Limit:10000KTotal Submissions:14821Accepted:3333DescriptionIONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You ar...

weixin_33724759的博客 203

POJ 1009 Edge Detection(一)

花了一晚上的时间才弄出来的程序居然 OutOfMemery 了,悲催。 应该是想法错了,此题不应该全局遍历的,耗时且对大数据量来说又不实际。 在问题的讨论区有人提到了一种思路,当添加一个数时会影响八个数。那么使用添加做驱动不断的修正数值是否会好些呢,又或者有其他的规律我没看到。今天就先到这,明天继续,灭了这题。 下面是今晚的成果,可惜是 WA 的: import java.util....

翔之骓 158

POJ_1009_EdgeDetection分析

题目是对RLE压缩的图像进行边缘检测。 RLE是游程编码的意思(Run length edcoding),比如555555根据游程编码可以编码成5 6。在代码中可以用控制符来区分编码字节和重复次数。 题目大意如下: 输入一张或多张游程编码的压缩图片,输出该图片的边缘检测的图片。其中,输入时0表示没有下一张图片,0 0表示本张图片输入结束。   算法思路: 1)输入的为RLE压缩图像,需...

domore 264

POJ1009-Edge Detection

解题报告索引目录 -> 【北大ACM – POJ试题分类】 转载请注明出处:http://exp-blog.com ------------------------------------------------------------------------- 大致题意: 某种卫星使用一种叫做“run length encoding”的方式来储存大尺寸图片, 有一种...

ζёСяêτ - 小優YoU 1万+

POJ 1009 Edge Detection解题报告

解决该题的核心思想是:只计算包括变化点的9个点的值。设输入图像为(v0, r0), (v1, r1), ..., (vn, rn), 那么变化点为v0, v1, ..., vn。这些点的值计算出来后,后面的输出就好说。但这样的计算还是不够的,我只是找出了以下3种特殊情况,处理后就ACCEPTED了,但是我无法论证处理这几种特殊情况是结果正确的充分条件。希望有高手能论证吧。 特殊情况1和2,图中红

lzshlzsh的专栏 3512

边缘检测 (Edge-Detection) 论文、代码大汇总

边缘检测/边缘提取 论文、代码的大汇总。 Github上持续更新:https://github.com/MarkMoHR/Awesome-Edge-Detection-Papers 目录 基于深度学习的方法 一般的边缘检测 物体轮廓提取 语义边缘检测 (包含分类) 遮挡边缘检测 根据多帧进行边缘检测 传统方法 1. 基于深度学习的方法 1.1 一般的边缘检测 方法简称 论文 ...

MokHoYin的博客 3万+

[POJ][1009]Edge Detection

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 d

软件工程学森 2793

POJ 1009

Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18826   Accepted: 4359 Description IONU Satellite Imaging, Inc. records and stores very large

bear_huangzhen的专栏 1724

poj1009 -- Edge Detection

Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21730   Accepted: 5099 Description IONU Satellite Imaging, Inc. records and stores very larg

BestFSQ的博客 929

POJ1009:Edge Detection

问题描述 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 b

打怪升级 427
tracy_junzi
博客等级 码龄16年 2粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值