poj 1009(注意数组的大小)

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm> 
using namespace std;
#define maxn 1005
int inPair[maxn][2],width,total,cup;
typedef struct outPair{
    int pos;
    int value;
}Pix;
Pix pix[maxn*100];
int getValue(int pos){
    int i = -1,j = 0;
    while(i<pos){
        i += inPair[j++][1];
    }
    return inPair[j-1][0];
}
int getCode(int pos){
    int i,j,row,col,tmp,code = 0;
    row = pos/width;
    col = pos%width;
    for(i=row-1;i<=row+1;i++){
        for(j=col-1;j<=col+1;j++){
            cup = i*width+j;
            if(i<0||j<0||j>=width||cup>=total)    //!!!!!!!!!
                continue;
            tmp = abs(getValue(pos)-getValue(cup));
            if(tmp>code)
                code = tmp;
        }
    }
    return code;
}
int cmp(const void*a,const void*b){
    Pix*x = (Pix*)a;
    Pix*y = (Pix*)b;
    return x->pos-y->pos;
}
int main(){
    int a,b,k,i,j,p,pos,row,col,count;
    while(scanf("%d",&width)==1&&width){
        k = 0;
        total = 0;
        memset(inPair,0,sizeof(inPair)); 
        while(scanf("%d%d",&a,&b)==2&&b){
            inPair[k][0] = a;
            inPair[k][1] = b;
            k++;
            total += b;
        }
        pos = 0;
        count = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            col = pos%width;
            for(i=row-1;i<=row+1;i++){
                for(j=col-1;j<=col+1;j++){
                    cup = i*width+j;
                    if(i<0||cup>=total||j<0||j>=width)
                        continue;
                    pix[count].pos = cup;
                    pix[count++].value = getCode(cup);
                }
            }
            pos += inPair[p][1];
        }
        pos = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            for(i=row-2;i<=row+2;i++){
                cup = i*width;
                if(i<0||cup>=total)
                    continue;
                pix[count].pos = cup;
                pix[count++].value = getCode(cup);
            }
            pos += inPair[p][1];
        }
        pos = 0;
        for(p=0;p<k;p++){
            row = pos/width;
            for(i=row-2;i<=row+2;i++){
                cup = (i+1)*width-1;
                if(i<0||cup>=total)
                    continue;
                pix[count].pos = cup;
                pix[count++].value = getCode(cup);
            }
            pos += inPair[p][1];
        }
        qsort(pix,count,sizeof(Pix),cmp);
//        for(i=0;i<count;i++){
//            printf("%d %d %d\n",i,pix[i].value,pix[i].pos);
//        }
        printf("%d\n",width);
        Pix tmp = pix[0];
        for(i=0;i<count;i++){
            if(tmp.value!=pix[i].value){
                printf("%d %d\n",tmp.value,pix[i].pos-tmp.pos);
                tmp = pix[i];
            }
        }
        printf("%d %d\n",tmp.value,total-tmp.pos);
        printf("0 0\n");
    }
    printf("0\n");
    return 0;
}

 

posted @ 2021-11-10 14:08  智人心  阅读(51)  评论(0)    收藏  举报