POJ1009-Edge Detection

全解题报告索引目录 -> 【北大ACM – POJ试题分类

转载请注明出处:http://exp-blog.com

-------------------------------------------------------------------------

 

 

大致题意:

某种卫星使用一种叫做“run length encoding”的方式来储存大尺寸图片,

有一种简单的 edge detection 算法 是将 图像中的每一个点的值与他周围的八个点相减,然后记录下绝对值最大的,上面的右图是左图经过这种算法转换之后的结果。

现在你的任务就是实现这个算法,输入的图片是以 run length encoding 的形式表示的,同时也要求转换后的图片也以 run length encoding 的形式表示。

 

解题思路:

非常令人纠结的模拟题,

由于图片宽度可能为10^9,因此不能开数组,会MLE

又因为像素点很多,不能直接暴力,会TLE

 

突破点在于Input的pair,pair上限只有1000,数据量是最少的,因此只能利用这点去解题。

要利用pair,就必须懂得“跳跃式编码”,就是说只在像素发生变化的位置进行编码,而像素没有变化的位置则其编码值与其左边的像素一致。

 

我只说解题方法,不给证明了。

先给所有像素点pix顺序标号pos,从1开始,以这个标号pos作为该像素点pix的索引

利用pos去模拟pix在二维图片的坐标row=(pos-1)/width,col=(pos-1)%width

这样就无需定义二维数组,仅仅虚构了一个二维数组,就解决了空间溢出MLE的问题

 

接下来在 像素发生变化的位置(下面称为“边界”)的地方 编码

边界位置其实就是每对pair的个数决定的,对边界位置及其周遭共9个像素点编码,把编码结果及对应的索引pos都存放在OutMap,编码方法就是题目给出的算法

最后把OutMap中的编码值根据其索引值进行升序排序,依次读取OutMap中的编码值,当编码值code发生变化时,则用 变化后的编码索引 减去 变化前的编码索引,就是code在OutMap中出现的次数。

 

 

Source修正:

Mid-Central USA 2000

http://mcpc.cigas.net/archives/2000/browse.html

 

//Memory Time 
//332K   32MS 

#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;

const int size=1000;  //每幅图片的pair上限
int width;  //Map的宽
int total=0;  //像素点总个数

typedef class OutMapPix
{
	public:
		int pos;    //OutMap中每个像素点的顺序位置,pos从1开始
		int code;   //OutMap中每个像素点对应InMap的编码
}Pix;

int InMapPair[size][2];  //InMapPair[][0]为像素值,InMapPair[][1]为InMapPair[][0]连续出现的个数
Pix OutMap[size*8];    //每个pix都依赖其周围的8个点编码

int cmp(const void* a,const void* b);  //快排比较规则
int GetValue(int pos);  //返回第pos个像素点的像素值
int GetCode(int pos);   //返回第pos个像素点的编码

int main(int k)
{
	while(cin>>width && width)
	{
		int pairv,pairt;
		k=total=0;
		while(cin>>pairv>>pairt && pairt)
		{
			InMapPair[k][0]=pairv;
			InMapPair[k++][1]=pairt;
			total+=pairt;
		}
		int PairNum=k;  //pair的个数

		cout<<width<<endl;

		int pos=1;  //当前处理的像素点的位置
		k=0; //OutMap[]指针
		for(int p=0;p<=PairNum;p++)
		{
			int row=(pos-1)/width;  //得到pos在二维图对应的坐标
			int col=(pos-1)%width;

			for(int i=row-1;i<=row+1;i++)        //枚举(row,col)周围及其自身共9个点(x,y)
				for(int j=col-1;j<=col+1;j++)
				{
					int tpos=i*width+j;  //得到(x,y)的顺序位置

					if(i<0 || j<0 || j>=width || tpos>=total)
						continue;

					OutMap[k].pos=tpos+1;
					OutMap[k++].code=GetCode(tpos+1);  //对发生变化的像素点的附近8个点编码
				}

			pos+=InMapPair[p][1];  //跳跃,确定下一个像素发生变化的点的位置
		}

		qsort(OutMap,k,sizeof(Pix),cmp);  //对OutMap根据顺序位置

		/*OutPut*/

		Pix temp=OutMap[0];
		for(int i=0;i<k;i++)
		{
			if(temp.code==OutMap[i].code)
				continue;
			cout<<temp.code<<' '<<OutMap[i].pos-temp.pos<<endl;
			temp=OutMap[i];
		}
		cout<<temp.code<<' '<<total-temp.pos+1<<endl;
		cout<<"0 0"<<endl;

	}
	cout<<0<<endl;

	return 0;
}


/*快排比较规则*/
int cmp(const void* a,const void* b)
{
	Pix* x=(Pix*)a;
	Pix* y=(Pix*)b;
	return x->pos - y->pos;
}

/*返回第pos个像素点的像素值*/
int GetValue(int pos)
{
	int i=0,p=0;
	while(p<pos)
		p+=InMapPair[i++][1];

	return InMapPair[i-1][0];
}

/*返回第pos个像素点的编码*/
int GetCode(int pos)
{
	int code=GetValue(pos);
	int MaxAbs=0;

	int row=(pos-1)/width;
	int col=(pos-1)%width;

	for(int i=row-1;i<=row+1;i++)
		for(int j=col-1;j<=col+1;j++)
		{
			int tpos=i*width+j;

			if(i<0 || j<0 || j>=width || tpos>=total || tpos==pos-1)  //tpos==pos-1为中心的像素点,即当前待编码的点
				continue;

			int tcode=GetValue(tpos+1);

			if(MaxAbs<abs(tcode-code))   //注意取绝对值
				MaxAbs=abs(tcode-code);
		}

	return MaxAbs;
}


 

 

 

Sample Input

3

255 1

10 1

255 2

10 1

255 2

10 1

255 1

0 0

10

35 500000000

200 500000000

0 0

7

15 4

100 15

25 2

175 2

25 5

175 2

25 5

0 0

8

4 8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

0 0

8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

0 0

8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

0 0

5

15 12

100 3

15 1

100 4

15 2

100 3

0 0

30

10 41

20 41

15 41

30 41

25 41

0 5

0 0

1

255 2

0 0

10

4 20

6 20

0 0

1

4 1000000000

0 0

1000000000

4 1000000000

0 0

30

10 41

20 41

15 41

30 41

25 41

0 5

0 0

8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

0 0

8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

4 8

0 0

8

4 8

1 1

2 1

4 1

8 1

6 1

4 1

2 1

0 1

5 8

0 0

5

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

30 11

20 11

10 11

20 11

0 0

0

 

Sample Output

3

245 9

0 0

10

0 499999990

165 20

0 499999990

0 0

7

85 5

0 2

85 5

75 10

150 2

75 3

0 2

150 2

0 4

0 0

8

3 2

4 3

2 1

4 2

3 1

2 1

4 2

2 3

4 1

0 0

8

1 1

2 1

4 2

2 4

1 1

2 1

4 2

2 4

0 0

8

1 1

2 1

4 2

2 4

0 0

5

0 6

85 12

0 2

85 3

0 2

0 0

30

0 10

10 62

5 20

15 62

5 20

25 6

5 15

0 9

25 6

0 0

1

0 2

0 0

10

0 10

2 20

0 10

0 0

1

0 1000000000

0 0

1000000000

0 1000000000

0 0

30

0 10

10 62

5 20

15 62

5 20

25 6

5 15

0 9

25 6

0 0

8

1 1

2 1

4 2

2 4

1 1

2 1

4 2

2 4

1 1

2 1

4 2

2 4

0 0

8

3 1

2 1

4 2

2 3

4 1

3 2

4 3

2 1

4 2

0 0

8

3 2

4 3

2 1

4 3

3 1

4 2

2 2

3 1

5 1

4 2

3 4

5 2

0 0

5

0 5

10 10990

0 5

0 0

0

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值