POJ1009_Edge Detection_跳跃式编码

Edge Detection
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 21451 Accepted: 5038

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

大致题意:

给出如图所示左图,对于图中的每一个格,求出它与上下左右邻居差的最大值。求完后形成了右图。


大体思路:

为防止MLE,要用一维数组而不是二维数组。行号和列号从1开始,对于第i个数,行数为 (i-1)/w+1 ,列数为 (i-1)%w+1 。

为了防止TLE,要用聚聚所说的跳跃式编程 ,就是说只有数据发生变化时才重新计算,否则当前方格保持前一个方格的值。观察数据可以发现(实际我是观察聚聚的题解才发现的),结果图数据发生变化的方格都出现在输入数据变化的格子周围的八个格子。所以只需对输入数据发生变化的周围8个格子进行计算,其他格子保持就可以了。


解题过程:

对于这个题,讲真,我的本事是做不了的。认认真真地看了网上聚聚的题解,终于勉强写了出来。


#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
#define _long 1010
struct out {
	int val,loc;
	}Out[9*_long];
int cmp (const void*x,const void*y)
{
	return ((out*)x)->loc>((out*)y)->loc?1:-1;
}
int width,Inv[_long],Inn[_long],tot;
int getnum(int i,int j)
{
	int k=i*width+j,t,sum=0;
	for(t=0;sum-1<k;t++)
		sum+=Inn[t];
	return Inv[t-1];
}
int fun (int row,int col)
{
	int p=getnum(row,col),i,j,m,Max=0;
	for(i=row-1;i<=row+1;i++)
	for(j=col-1;j<=col+1;j++){
		if(i<0||j<0||i>=tot/width||j>=width||i==row&&j==col) continue;
		m=abs(getnum(i,j)-p);
		Max=Max>m?Max:m;
	}
	return Max;
}
int main()
{
	//freopen("1009.txt","r",stdin);
	//freopen("out.txt","w",stdout);
	int pairs,pos,i,j,cow,col,ans,posn;
	while(cin>>width&&width!=0){
			cout<<width<<endl;
			tot=0,pairs=0,pos=1,ans=0,posn=0;
		while(cin>>Inv[pairs]>>Inn[pairs]&&Inn[pairs]!=0)tot+=Inn[pairs++];
		while(pos<=tot){
			cow=(pos-1)/width,col=(pos-1)%width;
			for(i=cow-1;i<=cow+1;i++)
			for(j=col-1;j<=col+1;j++){
				if(i<0||j<0||i>=tot/width||j>=width) continue;
				Out[ans].val=fun(i,j),Out[ans++].loc=i*width+j;
			}
			pos+=Inn[posn++];
		}
		Out[ans].val=fun(tot/width-1,0),Out[ans++].loc=tot-width;
	qsort(Out,ans,sizeof(Out[0]),cmp);
	for(i=0;i<ans;){
		cout<<Out[i].val<<" ";
		for(j=i;Out[j].val==Out[i].val&&j<ans;j++);
		if(j==ans) cout<<tot-Out[i].loc<<endl;
		else cout<<Out[j].loc-Out[i].loc<<endl;
		i=j;
	}
	cout<<"0 0"<<endl;
	}
	cout<<"0";
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值