解題報告_18.5.22_POJ_1009_0

轉載自:https://blog.csdn.net/lyy289065406/article/details/6648671

#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;  
}  

發佈了36 篇原創文章 · 獲贊 3 · 訪問量 6322
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章