POJ 1009 Edge Detection(模拟)

来源:互联网 发布:usb摄像头和网络摄像头 编辑:程序博客网 时间:2024/06/05 19:29

Edge Detection
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 17586 Accepted: 4020

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

Hint

A brute force solution that attempts to compute an output value for every individual pixel will likely fail due to space or time constraints. 

AC代码:

#include <cstdio>#include <cmath>#include <algorithm>using namespace std;int w,tot,k,b;struct node{int pos;    int ans;  }out[10010],in[1001];int cmp(node x,node y){return x.pos<y.pos;}int gg(int x){int p,i;p=0;i=0;while(p<x)p+=in[i++].ans;return in[i-1].pos;}int so(int x){int n,ans=0,i,j,q,t,h,l;n=gg(x);h=(x-1)/w;l=(x-1)%w;for (i=h-1;i<=h+1;i++){for (j=l-1;j<=l+1;j++){q=i*w+j;if (i<0||j<0||j>=w||q>=tot||q+1==x)continue;t=gg(q+1);if (ans<abs(t-n))ans=abs(t-n);}}return ans;}void getans(){int i,j,q,we,n,h,l;q=1;k=0;for(n=0;n<=b;n++){h=(q-1)/w;l=(q-1)%w;for(i=h-1;i<=h+1;i++){for (j=l-1;j<=l+1;j++){we=i*w+j;if (i<0 || j<0 || j>=w || we>=tot)continue;out[k].pos=we+1;out[k++].ans=so(we+1);}}q+=in[n].ans;}}void putans(){int i,j;sort(out,out+k,cmp);node t;t=out[0];for(i=0;i<k;i++){if (out[i].ans==t.ans)continue;printf("%d %d\n",t.ans,out[i].pos-t.pos);t=out[i];}printf("%d %d\n",t.ans,tot-t.pos+1);printf("0 0\n");}int main(){while(scanf("%d",&w),w){int num,len,i;b=tot=0;while(scanf("%d%d",&num,&len),len){in[b].pos=num;in[b++].ans=len;tot+=len;}printf("%d\n",w);getans();putans();}printf("0\n");return 0;}


0 0
原创粉丝点击