POJ1009解题报告

题目:

Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 12340 Accepted: 2694

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

--------------------------------------------------------------------------华丽的分割线-------------------------------------------------------------------------------------------------------------------------------------------

很明显,数据量超大的情况下,考虑暴力枚举是没有希望的,非常容易TLE或者MLE。

我们只考虑变化的数据,一个数据一旦变化,很容易就可以推出它会影响自己周围8个方向的节点,那么这些节点就需要进行重算。

需要注意的两个特殊点:

1.我们称之为1号点,即位于图最右方且它的下方有两行的点,这个点一旦发生变化,不光会影响周围6个点,而且会影响它下方二行的第一个点,需要对这些点进行重算

2.终点,我们也需要对终点的周围进行重算

只要重算该重算的点,那么没有被重算到的点自然就与它的前一个点的值相同。只需要输出即可。

import java.io.*;
import java.util.*;
class Point{
	
	private int firstVal;
	private int secondVal;
	
	public Point(int firstVal,int secondVal)
	{
		this.firstVal=firstVal;
		this.secondVal=secondVal;
	}
	
	public int getFirstVal() {
		return firstVal;
	}
	public void setFirstVal(int firstVal) {
		this.firstVal = firstVal;
	}
	public int getSecondVal() {
		return secondVal;
	}
	public void setSecondVal(int secondVal) {
		this.secondVal = secondVal;
	}
}
public class Main {

	//代表读入的pair
	private ArrayList<Point> pair=null;
	//代表结果图
	private ArrayList<Point> vMap=null;
	//代表图的宽度
	private int iWidth;
	//代表读入的值
	private int iParam;
	//代表读入的数量
	private int iCount;
	//代表总数目
	private int iNum;
	//代表当前位置
	private int iPos;
	
	public Main()
	{
		this.pair=new ArrayList<Point>();
		this.vMap=new ArrayList<Point>();
		init();
	}
	//进行初始化
	public void init()
	{
		this.pair.clear();
		this.vMap.clear();
		this.iPos=1;
		this.iCount=0;
		this.iParam=0;
		this.iWidth=0;
		this.iNum=0;
	}
	
	public void acm1009()
	{
		Scanner scan=new Scanner(System.in);
		iNum=0;
		while(true){
			
			iWidth=scan.nextInt();
			if(iWidth==0)
			{
				break;
			}
			iParam=scan.nextInt();
			iCount=scan.nextInt();
			while(iParam!=0||iCount!=0)
			{
				this.pair.add(new Point(iParam,iCount));
				iNum+=iCount;
				iParam=scan.nextInt();
				iCount=scan.nextInt();
			}
			calPixel();
			
			
			//对于vMap按照iPos从小到大进行排序
			sort();
			//输出结果
			System.out.println(iWidth);
			int iCur=0;
			
			int i;
			
			for(i=0;i<this.vMap.size();i++)
			{
				if(this.vMap.get(i).getSecondVal()==this.vMap.get(iCur).getSecondVal())
				{
					continue;
				}
				System.out.println(this.vMap.get(iCur).getSecondVal()+" "+(this.vMap.get(i).getFirstVal()-this.vMap.get(iCur).getFirstVal()));
				
				iCur=i;
			}
			System.out.println(this.vMap.get(iCur).getSecondVal()+" "+(iNum-this.vMap.get(iCur).getFirstVal()+1));
			System.out.println(iParam+" "+iCount);
			//清空,并重新开始进行计算
			init();
		}
		System.out.println(iWidth);
	}
	//冒泡排序
	public void sort()
	{
		for(int i=0;i<this.vMap.size()-1;i++)
		{
			for(int j=0;j<this.vMap.size()-i-1;j++)
			{
				if(this.vMap.get(j).getFirstVal()>this.vMap.get(j+1).getFirstVal())
				{
					int temp=this.vMap.get(j).getFirstVal();
					int temp1=this.vMap.get(j).getSecondVal();
					
					this.vMap.get(j).setFirstVal(this.vMap.get(j+1).getFirstVal());
					this.vMap.get(j).setSecondVal(this.vMap.get(j+1).getSecondVal());
					this.vMap.get(j+1).setFirstVal(temp);
					this.vMap.get(j+1).setSecondVal(temp1);
				}
			}
		}
	}
	//计算像素
	public void calPixel()
	{
		//初始时,位置为1
		iPos=1;
		
		//遍历pair,当有一个新的pair时,就证明有值改变了
		//我们只计算改变的值影响的周围的节点
		//还需要判断终点,终点即iPos到了最后一个位置时,这时循环i=this.pair.size(),且iPos不用增加
		for(int i=0;i<=this.pair.size();i++)
		{
			//计算出iPos位置所对应的节点所处的行和列
			//行和列均从0开始
			int row=(iPos-1)/iWidth;
			int col=(iPos-1)%iWidth;
			
			// 此处处理1号点问题
			if (col == iWidth - 1)
			{
				if ((row + 2)*iWidth < iNum)
				{
					Point tmpPix=new Point(iPos + iWidth+1, calNode(iPos + iWidth + 1, row + 2, 0));
					vMap.add(tmpPix);
				}
			}

			
			//计算被改变的节点所影响的节点 
			for(int m=row-1;m<=row+1;m++)
			{
				for(int n=col-1;n<=col+1;n++)
				{
					int myPos=m*iWidth+n+1;
					
					if(m>=0&&n>=0&&n<iWidth&&myPos<=iNum)
					{
						Point myPoint=new Point(myPos,calNode(myPos,m,n));
						this.vMap.add(myPoint);
					}
				}
			}
			
			if(i!=this.pair.size())
			{
				iPos+=this.pair.get(i).getSecondVal();
			}
		}
	}
	//计算节点中所应该填的值的方法,形式参数为iPos、row、col
	//计算节点
	//此方法也是准确无误的
	public int calNode(int iPos,int row,int col)
	{
		//初始化参数值为0
		int result=0;
		
		//扫描iPos节点的八个方向
		for(int i=row-1;i<=row+1;i++)
		{
			for(int j=col-1;j<=col+1;j++)
			{
				int temp=iWidth*i+j+1;
				if(temp==iPos)
				{
					continue;
				}
				if(i>=0&&j>=0&&j<iWidth&&temp<=iNum)
				{
					if(Math.abs(getValue(temp)-getValue(iPos))>result)
					{
						result=Math.abs(getValue(temp)-getValue(iPos));
					}
				}
			}
		}
		
		return result;
	}//calNode
	
	//获得值
	//举个例子,比如图为1 1 1,那么取iPos=3点的值
	//开始时iN=0,i=0,iN+=3;后iN变为了3,i++,i变为了1,这时iN<3不符合条件,退出循环
	//返回值为this.pair.get(0).getFirstVal()返回的值为1
	//证明此取值的方法准确无误!
	public int getValue(int iPos)
	{
		int i=0;
		int iN=0;
		for(i=0;iN<iPos;i++)
		{
			iN+=this.pair.get(i).getSecondVal();
		}
		return this.pair.get(i-1).getFirstVal();
	}
	public static void main(String[] args) throws Exception
	{
		Main mainf=new Main();
		mainf.acm1009();
	}
}


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值