//我不会用结构体,就又开了一个数组。
目前,TLE,再改改就能AC了,但是不想改了。。
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int MAXsize = 1000; //图片的上限
int width;
int pixelcount = 0;
int pixel[MAXsize][2] = { 0 };
int laterpixel[8*MAXsize][2];//记录变换后的图像像素
int compare(const void *,const void *);
int pixelcal(int);
int Tpixel(int);
#define test
int getP(int pos,int pair)
{
int tmp=0;
for(int i=0;i<pair;i++)
{
tmp+=pixel[i][1];
if(pos<=tmp)
return pixel[i][0];
}
}
int main() {
//width为图像的宽度,pixel为每个节点的宽度,line是pixel的数量
int totalpair=0; //记录pair数量,一个像素Opixel和像素的数量line就是一个pair
#ifndef test
while (cin >> width && width)
#else
width=7;
#endif
{
int Opixel, line; //O_pixel指的是原来未经过转换的像素
int count = 0; //count计数设置
//image中像素的个数
#ifndef test
while (cin >> Opixel >> line && line){
pixel[count][0] = Opixel; //第一列记录像素
pixel[count++][1] = line; //第二列记录每像素的数量
pixelcount += line; //像素总数,也就是格子数
totalpair=count;
}
#else
pixel[0][0]=15;
pixel[0][1]=4;
pixel[1][0]=100;
pixel[1][1]=15;
pixel[2][0]=25;
pixel[2][1]=2;
pixel[3][0]=175;
pixel[3][1]=2;
pixel[4][0]=25;
pixel[4][1]=5;
pixel[5][0]=175;
pixel[5][1]=2;
pixel[6][0]=25;
pixel[6][1]=5;
pixelcount=35;
totalpair=7;
#endif
int t=0;
while (true)
{
if(t==pixelcount)
break;
cout<<getP(t,totalpair)<<"\t";
if((t+1)%7==0)
cout<<endl;
t++;
}
cout<<width<<endl;
#ifdef test
for(int pos=0;pos<pixelcount;pos++)
{
int i=getP(pos,totalpair);
int max=0;
int tmpp=0;
if(pos-width>=0)
{
tmpp=abs(i-getP(pos-width,totalpair));
if(max<tmpp)
max=tmpp;
}
if(pos+width<pixelcount)
{
tmpp=abs(i-getP(pos+width,totalpair));
if(max<tmpp)
max=tmpp;
}
if(pos%totalpair!=0)
{
tmpp=abs(i-getP(pos-1,totalpair));
if(max<tmpp)
max=tmpp;
if(pos-1-width>=0)
{
tmpp=abs(i-getP(pos-1-width,totalpair));
if(max<tmpp)
max=tmpp;
}
if(pos-1+width<pixelcount)
{
tmpp=abs(i-getP(pos-1+width,totalpair));
if(max<tmpp)
max=tmpp;
}
}
if((pos+1)%totalpair!=0)
{
tmpp=abs(i-getP(pos+1,totalpair));
if(max<tmpp)
max=tmpp;
if(pos+1-width>=0)
{
tmpp=abs(i-getP(pos+1-width,totalpair));
if(max<tmpp)
max=tmpp;
}
if(pos+1+width<pixelcount)
{
tmpp=abs(i-getP(pos+1+width,totalpair));
if(max<tmpp)
max=tmpp;
}
}
cout<<max<<"\t";
if((pos+1)%7==0)
cout<<endl;
}
#endif
int pos=1;
int pair;
int k=0;
int laterpos;
for(pair=0;pair<=totalpair;pair++){
int row=(pos-1)/width;
int column=(pos-1)%width;
for(int i=column-1;i<=column+1;row++){
for(int j=row-1;j<=row+1;j++){
laterpos =i*width+j; //得到将要编码的中心点(x,y)
if(i<0 || j<0 || j>=width || laterpos>=pixelcount)
continue;
laterpixel[k][0]=Tpixel(++laterpos);
laterpixel[k++][1]=laterpos+1;
}
}
}
pos+=pixel[pair][1];
qsort(laterpixel,k,sizeof(int),compare);
int temp=laterpixel[0][0];
int temp2=0;
for(int i=0;i<k;i++){
if(temp==laterpixel[i][0])
continue;
cout<<temp<<laterpixel[0][1]-temp2<<endl;
temp=laterpixel[i][0];
temp2=laterpixel[i][1];
}
cout<<temp<<totalpair-temp2+1<<endl;
cout<<"0 0"<<endl;
}
return 0;
}
int compare(const void *a,const void *b)
{
return *((int*)a) > *((int *)b) ? 1 : -1;
}
int pixelcal(int pos){ //计算每个像素点压缩后的像素
return pixel[pos][0];
}
int Tpixel(int pos){ //位置为pos的点转换后的像素编码
int row,column;
int maxvalue=0,encode=0;
int pixelvalue=pixelcal(pos);
row=(pos-1)/width,column=(pos-1)%width;
for(int i=row-1;i<row+1;i++){
for(int j=column-1;j<column+1;j++){
int truepos=i*width+j;
if(i<0 || j<0 || j>=width || truepos>=pixelcount || truepos==pos-1) //tpos==pos-1 为中心的像素点,即当前待编码的点
continue;
int nextpixel=pixelcal(truepos);
encode=abs(pixelvalue-nextpixel);
maxvalue=(maxvalue<encode) ? encode : maxvalue;
}
}
return maxvalue;
}
参考:http://blog.csdn.net/lyy289065406/article/details/6648671