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 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
import java.util.ArrayList;
import java.util.Scanner;

public class Poj1009 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		while(true){
			//读取图片列值,如果为0,则退出程序
			int column = sc.nextInt();

			if(column == 0){
				break;
			}

			ArrayList<Integer> arr1 = new ArrayList<Integer>();

			while(sc.hasNext()){
				//获取像素值
				int pixel = sc.nextInt();
				//获取重复次数
				int len = sc.nextInt();

				if(pixel == 0 && len == 0){
					break;
				}

				for(int i=0; i<len; i++){
					arr1.add(pixel);
				}
			}

			//图像像素值总长度
			int size = arr1.size();
			//二维表行数
			int row = size/column;

			//新建输入图片数组
			int[][] inputImage = new int[row][column];
			//新建输出图片数组
			int[][] outputImage = new int[row][column];

			//初始化输入图片数组
			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){
					inputImage[i][j] = arr1.get(i*column + j);
				}
			}

			//存储一个某像素与周边8个像素之间的绝对值
			int[] nums = new int[8];

			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){

					int max = 0;

					//寻找周边像素值,如果没有则为-1
					int num1 = (i-1)<0||(j-1)<0 ? -1 : inputImage[i-1][j-1];
					int num2 = (i-1)<0 ? -1 : inputImage[i-1][j];
					int num3 = (i-1)<0||(j+1)>=column ? -1 : inputImage[i-1][j+1];
					int num4 = (j+1)>=column ? -1 : inputImage[i][j+1];
					int num5 = (i+1)>=row || (j+1)>=column ? -1 : inputImage[i+1][j+1];
					int num6 = (i+1)>=row ? -1 : inputImage[i+1][j];
					int num7 = (i+1)>=row || (j-1)<0 ? -1 : inputImage[i+1][j-1];
					int num8 = (j-1)<0 ? -1 : inputImage[i][j-1];

					//计算与周边像素值的绝对值,如果没有则为-1
					nums[0] = num1==-1?num1:Math.abs(inputImage[i][j] - num1);
					nums[1] = num2==-1?num2:Math.abs(inputImage[i][j] - num2);;
					nums[2] = num3==-1?num3:Math.abs(inputImage[i][j] - num3);;
					nums[3] = num4==-1?num4:Math.abs(inputImage[i][j] - num4);;
					nums[4] = num5==-1?num5:Math.abs(inputImage[i][j] - num5);;
					nums[5] = num6==-1?num6:Math.abs(inputImage[i][j] - num6);;
					nums[6] = num7==-1?num7:Math.abs(inputImage[i][j] - num7);;
					nums[7] = num8==-1?num8:Math.abs(inputImage[i][j] - num8);;

					for(int k=0; k<nums.length; k++){
						if(nums[k] != -1){
							//该值不为-1,说明周边该像素存在
							if(max < nums[k]){
								max = nums[k];
							}
						}
					}
					//已经找出绝对值中的最大值

					outputImage[i][j] = max;
				}
			}

			System.out.println(column);

			int count = 0;
			int oldValue = -1;

			//通过输出二位数组输出一对一对的值
			for(int i=0; i<row; i++){
				for(int j=0; j<column; j++){
					if(oldValue == -1){
						//说明这是第一个像素,直接赋值,并计数为1
						oldValue = outputImage[i][j];
						count = 1;
					}else if(oldValue == outputImage[i][j]){
						//说明和上一个数重复,计数加1
						count++;
					}else{
						//说明不和上一个数重复,计数置为1,并打印上一个数的值和次数
						System.out.println(oldValue+" " + count);
						oldValue = outputImage[i][j];
						count = 1;
					}
				}
			}

			System.out.println(oldValue+" " + count);
			System.out.println("0 0");
		}

		System.out.println("0");
	}

}

时间: 2024-10-15 03:11:26

POJ 1009的相关文章

POJ 1009 Edge Detection

Edge Detection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22153   Accepted: 5201 Description IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compre

next_permutation用法和一个不成功的案例~(&gt;_&lt;)

几种常见用法: #include<iostream>#include<string.h>#include<algorithm>using namespace std;int main(){ char d[100]; cin>>d; sort(d,d+strlen(d)); char *first=d; char *last=d+strlen(d); do{  cout<<d<<endl; }while(next_permutation

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

POJ Frogs&#39; Neighborhood havel-hakimi定理 (简单题)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8263   Accepted: 3504   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..

POJ 1651 Multiplication Puzzle (区间dp)

题目大意:对n个数组成的序列取数,规定最两边不能取,每次取一个a[i],得到 a[l] * a[i] * a[r] 的分数(a[l]是a[i]左边的数,a[r]是a[i]右边的数),并把这个数从序列中移走,求n-2次取数后的得分和的最小值 分析:正着确定状态不好做,不如反着来,设dp[l][r]为向区间[l, r]中填满数所得到分数和的最小值,考虑最近一次填数的位置,不难得出: dp[l][r] = fmin(dp[l][m] + dp[m][r] + a[l] * a[m] * a[r]) (

poj 3090 Visible Lattice Points 法雷级数||打表

由于图像关于对角线对称,所以我们只看下三角区域.将x轴看做分母,被圈的点看成分子 依次是{1/2},{1/3,1/2},{1/4,3/4},{1/5,2/5,3/5,4/5} 写成前缀和的形式就是 {1/2},{1/2,1/3,2/3},{1/2,1/3,1/3,1/4,3/4},{1/2,1/3,1/3,1/4,3/4,1/5,2/5,3/5,4/5} 发现,这就是一个法雷级数,即第k项增加的数就是phi[k].最后的答案*2+(0,1)+(1,0),(1,1)三个点就好了 #include