poj 1009 Edge Detection 模拟

//poj 1009
//sep9
#include <iostream>
#include <algorithm>
using namespace std;
const int maxN=1024;

struct RLE
{
	int v,pos;	
}rle_in[maxN],rle_out[maxN*8];

int n,m,p,q,sum;

int cmp(RLE x,RLE y)
{
	return x.pos<y.pos;	
}

int get_pos(int i,int j)
{
	int tpos=i*m+j;
	int ans,l=0,r=p;
	while(l<r){
		int mid=(l+r)/2;
		if(rle_in[mid].pos<=tpos){
			ans=mid;
			l=mid+1;
		}else 
			r=mid;
	}
	return ans;
}

void update(int row,int col)
{
	if(row<0||row>=n||col<0||col>=m)
		return ;
	int maxx=0;
	int center=rle_in[get_pos(row,col)].v;	
	for(int i=row-1;i<=row+1;++i)
		for(int j=col-1;j<=col+1;++j)
			if(i>=0&&i<n&&j>=0&&j<m&&!(i==row&&j==col))
				maxx=max(maxx,abs(center-rle_in[get_pos(i,j)].v));
	rle_out[q].v=maxx;
	rle_out[q++].pos=row*m+col;		
	
}

int main()
{
	while(scanf("%d",&m)==1&&m){
		int a,b;
		sum=p=0;
		while(scanf("%d%d",&a,&b)==2){
			if(a==0&&b==0)
				break;
			rle_in[p].v=a;
			rle_in[p++].pos=sum;
			sum+=b;
		}
		n=sum/m;	
		q=0;
		rle_in[p].pos=sum;
		for(int k=0;k<=p;++k){
			int row=rle_in[k].pos/m;
			int col=rle_in[k].pos%m;
			for(int i=row-1;i<=row+1;++i)
				for(int j=col-1;j<=col+1;++j)
					update(i,j);
		}
		sort(rle_out,rle_out+q,cmp);
		int pre=0;
		RLE tmp=rle_out[0];
		printf("%d\n",m);
		for(int i=1;i<q;++i)
			if(rle_out[i].v!=tmp.v){
		 		printf("%d %d\n",tmp.v,rle_out[i].pos-tmp.pos);
				tmp=rle_out[i];
			}
		printf("%d %d\n",tmp.v,sum-tmp.pos);
		puts("0 0");
	}
	puts("0");
	return 0;	
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值