POJ1009_Edge Detection_跳跃式编码

来源:互联网 发布:淘宝店进货渠道 编辑:程序博客网 时间:2024/05/22 12:59

Edge Detection
Time Limit: 1000MS Memory Limit: 10000KTotal 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 (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

715 4100 1525 2175 225 5175 225 50 01035 500000000200 5000000000 03255 110 1255 210 1255 210 1255 10 00

Sample Output

785 50 285 575 10150 275 30 2150 20 40 0100 499999990165 200 4999999900 03245 90 00

大致题意:

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


大体思路:

为防止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 1010struct 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 0
原创粉丝点击