#include"Functions.hpp"
#include"Good/Relativity.hpp"
#include"Good/Good.hpp"
#include"floor.hpp"
#include"quantum.hpp"

unsigned long long iter=0;
void inline change(Movement*const m1,Vertices const e){
  ++iter;
  auto*const v=vertices+e;
  auto*const m0=v->selected;
  auto*const c0=m0-e;
  auto*const c1=m1-e;
  auto*const h0=m0->height.l;
  auto*const h1=m1->height.l;
  v->selected=zeromovement,zeromovement=m0;

  ~*m0;

  Change(m0->height,m1->height)

  v->selected=m1,!*m1;

  Delta const dH=h0-h1;
  score-=dH,*v-=dH;
}
//////////////////////////////////////////////////
Movement**index=new Movement*[nVertices*(nColors-1)*(hDegree*4+1)];
Movement***tanq=new Movement**[hDegree*4+1];
Movement***tans=tanq+hDegree*3;
signed mins;

#define Tabu score+5

unsigned inline count(Movement*const m1,Vertices const e){
  auto*const v=vertices+e;
  auto*const m0=v->selected;
  auto*const c0=m0-e;
  auto*const c1=m1-e;
  auto*const h0=m0->height.l;
  auto*const h1=m1->height.l;
  Delta r=0;
  loopE{
    setV
    setM(m0,c0)
    setM(m1,c1)
    setH(h0,m0)
    setH(h1,m1)
    setH(h,s)
    if(m1==s&&h1->o==1)r+=1;
    if(m1==s&&h1->o==1&&h1+1==h0)qr[qrn++]=e;
    if(m0==s)r-=1;
    else if(h0->u==0)qr[qrn++]=e;
  }if(qrn)r-=quantum();
  return score+r;
}

void inline indice(){
  Delta R=0;
  for(auto i=0u;i<hDegree*4+1;++i)
    tanq[i]=index+nVertices*(nColors-1)*i;
  for(Vertices e=0;e<nVertices;++e){
    auto const*const v=vertices+e;
    auto const*const s=v->selected;
    auto const*const h0=s->height.l;
    auto const*const c0=s-e;
    for(Colors c=0;c<nColors;++c){
      auto*const m=&movements[c][e];
      if(m!=s){
        auto const*const c1=m-e;
        auto const*const h1=m->height.l;
        Delta r=0;
        qrn=0;
        loopE{
          setV
          setM(m0,c0)
          setM(m1,c1)
          setH(h0,m0)
          setH(h1,m1)
          setH(h,s)

          /*
          if(m1==s&&h1->o==1&&h1+1!=h0)r+=1;
          /**/
          //*
          if(m1==s&&h1->o==1)r+=1;
          if(m1==s&&h1->o==1&&h1+1==h0)qr[qrn++]=e;
          /**/
          /*
          if(m0==s&&h0->u==0)r-=3;else//h0<=h
          if(m0==s&&h0->u!=0)r-=1;else//h0<=h
          if(m0!=s&&h0->u==0)r-=1,qr[qrn++]=e;//h0<=h
          /**/
          //*
          if(m0==s){
            if(h0->u==0)r-=2;
            else r-=1;
            //r-=1;
          }else if(h0->u==0)qr[qrn++]=e;
          /**/
          /*
          if(m0==s)r-=1;
          else if(h0<=h)qr[qrn++]=e;
          if(h0->u==0)qr[qrn++]=e;
          /**/
          /*
          //if(m0==s&&h0->u==0)r-=4;else
          //if(m0==s&&h0->u!=0)r-=1;else
          //if(m0!=s&&h0->u==0)r-=2;
          if(m0==s&&h0->u==0)r-=3;else
          if(m0!=s&&h0->u==0)r-=1,qr[qrn++]=e;else
          if(m0==s&&h0->u!=0)r-=1;
          if(h0<=h)r-=1;
          /**/
          /*
          if(m0==s)r-=1;
          if(h0->u==0)r-=1;
          if(h0<=h)qr[qrn++]=e;
          /**/
        }
        if(qrn)r-=quantum();
        *(tans[r]++)=m;
        if(r<R)R=r;
      }
    }
  }
  mins=hDegree*3+R;
}

//score,best,newscore
unsigned lock_mov[nVertices*nColors];
unsigned lock_res[nVertices*nColors];
unsigned lock_cnt[nVertices*nColors];
unsigned lock_scr[nVertices*nColors];
unsigned lock_bst[nVertices*nColors];
unsigned lock_idx[nVertices*nColors];
unsigned lock_new[nVertices*nColors];
//

void inline warp(){
  unsigned n=0;
  Vertices tabus[nEdges]={};
  Vertices tabuq[nVertices]={};
  Vertices tabup=-1;
  Movement*tabum[nVertices]={};
  Score best=score;
  unsigned QRN=1;
A:
  for(auto*i=hIndex.lowest;i<&hIndex.zeropoint;++i){
    auto const*const j=i->z;
    if(*i==j)continue;
    hIndex.lowest=i;
    change(*(*j)->z,(*j)->vertex);
    goto A;
  }
  if(score<best)best=score;
  if(score==0)goto Z;
B:
  indice();
  for(auto i=mins;i<hDegree*4+1;++i){
    auto q=index+nVertices*(nColors-1)*i;
    auto s=tanq[i]-q;
    {
      Movement*p;
      unsigned N;
      for(auto j=0u;j<s;++j)
        N=rand()%s,p=q[j],q[j]=q[N],q[N]=p;
    }
    for(;q<tanq[i];++q){
      auto const m=*q;
      auto const e=m->e;
      //*
      auto const*const v=vertices+e;
      auto      *const S=v->selected;
      auto const*const h0=S->height.l;
      auto const*const h1=m->height.l;
      auto c=(m-e-movements[0])/nVertices;
      bool cont=0;
      /*
      for(unsigned j=0;j<Tabu;++j){
        if(tabum[j]==m){
          cont=1;
          break;
        }
      }if(cont)continue;
      if(e==tabup)continue;
      /**/
      //*
      for(unsigned j=0;j<Tabu;++j){
        if(tabus[j]==e){
          //if(count(m,e)<best)goto QRN;
          /*
          if((signed)(score-hDegree*3+i)<best){
            cout<<(signed)(score-hDegree*3+i)<<','<<best<<endl;
            goto QRN;
          }
          /**/
          cont=1;
          break;
        }
      }if(cont)continue;
      tabus[n]=e,n=(n+1)%(Tabu);
QRN:
      //if(e==tabup)continue;
      /**/
      /*
      if(
      //lock_bst[e*nColors+c]==best&&
      //lock_scr[e*nColors+c]==score&&
      //lock_new[e*nColors+c]==h1-h0&&
      lock_idx[e*nColors+c]==i//&&
      //lock_res[e*nColors+c]==s
      //QRN-lock_mov[e*nColors+c]==lock_res[e*nColors+c]
      //lock_mov[e*nColors+c]!=25
      //&&rand()%(lock_mov[e*nColors+c]+1)!=0
      ){
        ++lock_mov[e*nColors+c];
        continue;
      }
      lock_mov[e*nColors+c]=0;
      lock_bst[e*nColors+c]=best;
      lock_scr[e*nColors+c]=score;
      lock_new[e*nColors+c]=h1-h0;
      lock_idx[e*nColors+c]=i;
      lock_res[e*nColors+c]=s;
      //lock_res[e*nColors+c]=QRN-lock_mov[e*nColors+c];
      //lock_mov[e*nColors+c]=QRN;
      //QRN++;
      /**/
      //tabum[n]=m,n=(n+1)%(Tabu);
      tabup=e;
      //if(!Floor(m,e))continue;
      //if(!flur(m,e))continue;
      //if(!floor(m,e))continue;
#ifdef PrintPath
      cout<<e<<endl;
#endif
      change(m,e);
      goto C;
    }
  }
C:
  if(score<best)best=score;
  if(score==0)goto Z;
D:
#ifdef PrintScore
  cout<<score<<','<<best<<','<<iter<<endl;
#endif
  //for(auto*i=&hIndex.zeropoint-1;i>=hIndex.lowest;--i){
  for(auto*i=hIndex.lowest;i<&hIndex.zeropoint;++i){
    for(auto const*j=i->z;*i!=j;j=j->z){
      auto const e=(*j)->vertex;
      auto const m=(*j)->z;
      //if(Floor(*m,e))
      //if(floor(*m,e))
      if(e!=tabup)
      //if(Floor(*m,e))
      //if(flur(*m,e))
      {
#ifdef PrintPath
        cout<<e<<endl;
#endif
        change(*m,e);
        goto D;
      }
    }
  }
E:
  if(score<best)best=score;
  if(score!=0)goto B;
Z:
  cerr<<iter<<','<<score<<endl;
  return;
}
