POJ 1009 Edge Detection

Edge Detection
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 14193   Accepted: 3177

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

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.
 
======================================================================
该题要考虑的细节比较多.看到题目的第一眼就觉得有好几个要考虑的地方.第一行和最后一行,第一列和最后一列.针对跨行的数据做优化.还有就是初始化值的问题.
写的时候发现其实第一行和最后一行以及中间行可以统一处理.但是针对行的优化是必须的.
针对某一行考虑的时候,我们只用考虑其上一行对以及下一行所涉及的pair.因此把他们找出来,把起始列标做一个排序,就能得到按照顺序排列的一组下标(这些下标对应的点,加上其左边,右边的点当做变化点,需要计算其值).每计算一个点,就能得到其后所有点(直到下一个变化点)的值.如果仅仅涉及一个pair,那这些值就必然为0了.而且可以计算该pair跨行的数量,把这些行跳过去.
中间还做了个小优化.取某个点的值的时候由于我们的操作的下标跨度不会很大.因此每次取值我都会记录上次取值的pair下标,这一次取的时候,值就可以在上次搜索的pair附近得到.
总的来说时间效率还是不错.评测的时间消耗是0MS.
#include <stdio.h>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

void solve(int w);
int main()
{
	while(true)
	{
		int w;
		scanf("%d",&w);
		if(!w) break;
		solve(w);
	};
	printf("0\n");
	return 0;
}

int pair_n[1000+10],pair_v[1000+10],pair_s[1000+10],index = 0;

int get(int pos)
{
	while(pos < pair_s[index] || pos >= pair_s[index]+pair_n[index])
	{
		if(pos < pair_s[index]) --index;
		else ++index;
	}

	return pair_v[index];
}

int get_offset(int pos,int off,int w,int h)
{
	int row = pos/w, col = pos%w;
	switch(off)
	{
		case 0: if(row<1 || col<1) return -1000; else return get(pos-w-1);
		case 1: if(row<1) return -1000; else return get(pos-w);
		case 2: if(row<1 || col>=w-1) return  -1000; else return get(pos-w+1);
		case 3: if(col<1) return -1000; else return get(pos-1);
		case 4: if(col>=w-1) return -1000; else return get(pos+1);
		case 5: if(row>=h-1 || col<1) return -1000; else return get(pos+w-1);
		case 6: if(row>=h-1) return -1000; else return get(pos+w);
		case 7: if(row>=h-1 || col>=w-1) return -1000; else return get(pos+w+1);
		default: return -1000;
	}
	return -1000;
}

int get_max(int pos,int w,int h)
{
	int v = get(pos), m = -100;
	for(int i=0;i<8;++i){
		int _v = max(get_offset(pos,i,w,h),v);
		m = fmax(m,fabs(_v-v));
	}
	return m;
}

void solve(int w)
{
	index = 0;
	printf("%d\n",w);
	//读入数据
	int n,sum=0,h;
	for(n=0;;++n)
	{
		int len,value;
		scanf("%d%d",&value,&len);
		if(len==0 && value==0) break;
		pair_n[n] = len;
		pair_v[n] = value;
		pair_s[n] = sum;
		sum += len;
	}
	pair_s[n] = sum;

	h = sum/w;
	//求解数据
	int i1 = 0; int i2 = 0;
	int old_v = -1; int old_n = 0;

	for(int r = 0;r<h;++r)//行
	{
		while(pair_s[i1]+pair_n[i1]<(r-1)*w) i1 += 1;
		while(pair_s[i2]+pair_n[i2]<(r+2)*w && pair_s[i2]+pair_n[i2] < sum) i2 += 1;
		if(i1 == i2)//跨行
		{
			//值必为0
			if(old_v != 0){//和之前的值相异
				if(old_n != 0) printf("%d %d\n",old_v,old_n);
				old_n = 0; old_v = 0;
			}
			int d = max(((pair_s[i1]+pair_n[i1])-r*w)/w-2,0);
			r += d;
			old_n += d*w+w;
		}else{
			vector<int> v;
			for(int i=i1+1;i<=i2;++i) v.push_back(pair_s[i]%w);
			v.push_back(0);v.push_back(w-1);
			sort(v.begin(),v.end());
			vector<int>::iterator end = unique(v.begin(),v.end());
			vector<int>::iterator begin = v.begin();

			int last = -1;

			for(vector<int>::iterator it=begin;it!=end;++it)
			{
				int i = *it; int p = r*w + (*it);
				if(i!=0 && i-1>last){
					int v = get_max(p-1,w,h);//检测左边的点
					if(v == old_v) old_n += (i-1)-last;
					else
					{
						if(old_n != 0)
						{
							old_n += i-2-last;
							printf("%d %d\n",old_v,old_n);
						}
						old_v = v;
						old_n = 1;
					}
					last = i-1;
				}

				if(i>last)
				{
					int v = get_max(p,w,h);//检测该点
					if(v == old_v) old_n += i-last;
					else
					{
						if(old_n != 0)
						{
							old_n += i-1-last;
							printf("%d %d\n",old_v,old_n);
						}
						old_n = 1;
						old_v = v;
					}
					last = i;
				}

				if(i!=w-1 && i+1>last){
					int v = get_max(p+1,w,h);//检测右边的点
					if(v == old_v) old_n += i+1-last;
					else
					{
						if(old_n != 0)
						{
							old_n += i-last;
							printf("%d %d\n",old_v,old_n);
						}
						old_n = 1;
						old_v = v;
					}
				}
				last = i+1;
			}
		}
	}
	if(old_n != 0) printf("%d %d\n",old_v,old_n);
	printf("0 0\n");
}

  

posted @ 2012-12-15 22:17  lssnail  阅读(510)  评论(0编辑  收藏  举报