#define T k=++k%4
#define R p=cx+u[(k+1)%4],q=cy+v[(k+1)%4]
#define C 0<=p&&p<w&&0<=q&&q<h&&!a[q*w+p]
#define D p=cx+u[k],q=cy+v[k]
#define G cx=p,cy=q,a[q*w+p]=++i


int* buildSnailReverse(int* a, int w, int h)
{
        memset(a,0,w*h*4);
        int xs=w>h?abs(h-w):0,ys=h>w?abs(h-w):0,p,q,i=1,k=w>=h?0:1,u[4]={1,0,-1,0},v[4]={0,1,0,-1},xi=1,yi=1;
      int cx=w/2,cy=(h-1)/2;
        if(w>h)
                cx=cy=(h-1)/2,xs++;
        if(w<h)
                cx=cy=w/2;

        a[cy*w+cx]=i;
        while(yi++<=ys||xi++<=xs)
                D,G;

        while(i<h*w){
                R;
                if(C)
                        T,G,D;
                else{
                        D;
                        if(C)
                                G;
                        else
                                T,D;
                }
        }
        return a;
}