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

zoj 1701 Edge Detection

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

zoj 1701 Edge Detection

#include<iostream>#include<cmath>#include<algorithm>using namespace std;const int size=1000;  int width;  int total=0; typedef class OutMapPix{public:int pos;    int pre;   }Pix;int InMapPair[size][2];  Pix OutMap[size*8];    int cmp(const void* a,const void* b);  int GetValue(int pos);  int GetCode(int 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;  cout<<width<<endl;int pos=1;  k=0; for(int p=0;p<=PairNum;p++){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)continue;OutMap[k].pos=tpos+1;OutMap[k++].pre=GetCode(tpos+1);  }pos+=InMapPair[p][1];  }qsort(OutMap,k,sizeof(Pix),cmp);  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)  /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/374538.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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