栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

poj 1009 Edge Detection

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

poj 1009 Edge Detection

#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 pre;   //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++].pre=GetCode(tpos+1);  //对发生变化的像素点的附近8个点编码}pos+=InMapPair[p][1];  //跳跃,确定下一个像素发生变化的点的位置}qsort(OutMap,k,sizeof(Pix),cmp);  //对OutMap根据顺序位置Pix temp=OutMap[0];for(int i=0;i<k;i++){if(temp.pre==OutMap[i].pre)continue;cout<<temp.pre<<' '<<OutMap[i].pos-temp.pos<<endl;temp=OutMap[i];}cout<<temp.pre<<' '<<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;}int GetValue(int pos){int i=0,p=0;while(p<pos)p+=InMapPair[i++][1];return InMapPair[i-1][0];}int GetCode(int pos){int pre=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 tpre=GetValue(tpos+1);if(MaxAbs<abs(tpre-pre))   //注意取绝对值MaxAbs=abs(tpre-pre);}return MaxAbs;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/380049.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号