Dirección del problema:/JudgeOnline/problem?id=1077
BFS:
#include
usando el espacio de nombres std;
int fac[10]={1,1};
bool tflag[9];
struct bbit{
unsigned int val:4;
};
struct bbbit
{
unsigned int val:2;
};
estructura Nodo
{
bbit s[9],pos;
int paso;
ruta bbbit[21],etiqueta;
int hashval()
{
int ret=0,i,j,tmp;
memset(tflag,false,sizeof(tflag));
for(i=0;i<8;i++)
{
tmp=0;
for(j=0;j
if(!tflag[j])
tmp++;
ret+=tmp*fac[8-i];
tflag[s[i].val]=true;
}
return ret;
}
bool up()
{
if(pos.val<=2) devolver falso;
s[pos.val].val^=s[pos.val-3].val;
s[pos.val-3].val^= s[pos.val].val;
s[pos.val].val^=s[pos.val-3].val;
ruta[paso].val =0;
pos.val-=3;
devuelve verdadero;
}
bool down()
{
if(pos.val>=6)return false;
s[pos.val].val^=s[pos.val+3].val ;
s[pos.val+3].val^=s[pos.val].val;
s[pos.val].val^=s[pos. val+3].val;
ruta[paso].val=1;
pos.val+=3;
devuelve verdadero;
}
bool left()
{
if(pos.val==0||pos.val==3||pos. val==6)return false;
s[pos.val].val^=s[pos.val-1].val;
s[pos.val-1 ].val^=s[pos.val].val;
s[pos.val].val^=s[pos.val-1].val;
ruta [paso].val=2;
pos.val--;
return true;
}
bool right()
{
if(pos.val==2||pos.val==5||pos.val==8)return false;
s[pos.val].val^=s[pos.val +1].val;
s[pos.val+1].val^=s[pos.val].val;
s[pos.val].val^ =s[pos.val+1].val;
ruta[paso].val=3;
pos.val++;
devuelve verdadero;
}
bool operator==(const Node&x)const
{
int i;
for( i=0;i<9;i++)if(s[i].val!=x.s[i].val)return false;
return true;
} p>
}Q[362880],S,A,tmp,top;
struct Hash
{
bool d1,d2; p>
p>
Nodo D;
}hash[362880];
inline void mkfac(){int i;for(i=2;i< =9;i++) fac[i]=fac[i-1]*i;}
inline int eval(char c){return c=='x'?0:c-'0' ;}
void o(Nodo x,Nodo y)
{
int i;
for(i=1;i <=x.step ;i++)
{
switch(x.path[i].val)
{
caso 0:putchar(' u');romper;
caso 1:putchar('d');romper;
caso 2:putchar('l');romper;
caso 3:putchar('r');break;
}
}
for(i=y.step;i >=1;i --)
switch(y.path[i].val){
caso 0:putchar('d');break;
caso 1 :putchar('u');romper;
caso 2:putchar('r');romper;
caso 3:putchar('l') ;romper;
p>}
puts("");
}
int main()
{
char buf[11];
int i,t,l,r;
bool flag;
mkfac ();
while(NULL!=gets(buf))
{
t=0;
for(i= 0;i<=7; i++)A.s[i].val=i+1;A.s[8].val=0;A.pos.val=8;
f
o(i=0;buf[i];i++)
{
if(buf[i]==' ')continuar;
S.s[ t].val=eval(buf[i]);
if(S.s[t].val==0)
S.pos.val=t;
t++;
}
l=r=0;
bandera=false;
for(i=0 ;i<362880;i++)hash[i].d1=hash[i].d2=false;
S.step=0;S.tag.val=1;
A.step=0;A.tag.val=2;
Q[r++]=S;//tag.val:1
Q[r++]=A; //tag.val:2
while(l<=r)
{
top=Q[l++];
top.step++;
tmp=top;
if(tmp.up())
{
if(tmp.tag .val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2&&hash[t].D==tmp)
{
//buscar respuesta...
o(tmp,hash[t].D);
ir a AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1&&hash[t].D==tmp )
{
//buscar respuesta...
o(hash[t].D,tmp);
ir a AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
tmp=top;
if(tmp.down())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2&&hash[t].
D==tmp)
{
//buscar respuesta...
o(tmp,hash[t].D);
ir a AA;
}
if(!hash[t].d2)hash[t].D=tmp;
} p>
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1&&hash[t ].D==tmp)
{
//buscar respuesta...
o(hash[t].D,tmp); p p>
ir a AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
tmp=top;
if(tmp.left())
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp.hashval()].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t ] .d2&&hash[t].D==tmp)
{
//buscar respuesta...
o(tmp,hash[t]. D );
ir a AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()]. d2 )
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash [ t].d1&&hash[t].D==tmp)
{
//buscar respuesta...
o(hash[t]. D ,tmp);
ir a AA;
}
if(!hash[t].d1)hash[t].D=tmp;
p>}
}
}
tmp=top;
if(tmp.right() ) p>
{
if(tmp.tag.val==1)
{
if(!hash[t=tmp .hashval( )].d1)
{
hash[t].d1=true;
Q[r++]=tmp;
if(hash[t].d2&&hash[t].D ==tmp)
{
//buscar respuesta...
o(tmp,hash[t].D);
ir a AA;
}
if(!hash[t].d2)hash[t].D=tmp;
}
}
else
{
if(!hash[t=tmp.hashval()].d2)
{
hash[t].d2=true;
Q[r++]=tmp;
if(hash[t].d1&&hash[t] .D==tmp)
{
//buscar respuesta...
o(hash[t].D,tmp);
ir a AA;
}
if(!hash[t].d1)hash[t].D=tmp;
}
}
}
}
AA:flag=true;
if(!flag) p>
puts("insoluble");
}
devuelve 0;
}
A*: p>
#include
#include
usando el espacio de nombres std;
int fac[10]={1,1 };
estructura Nodo
{
int s[9],paso,pos;
ruta char[501];
int hashval()
{
int ret=0,i,j,tmp;
bool flag[9];
memset(bandera,false,sizeof(bandera));
for(i=0;i<8;i++)
{
tmp=0;
for(j=0;j
if(!flag[j])
tmp++;
ret+=tmp*fac[8-i];
bandera[s[i]]=true;
}
devolver ret;
}
bool up()
{
if(pos<=2)return false;
s[pos]^=s[pos-3];
s[pos-3]^=s[pos];
s[pos] ^=s[pos-3];
ruta[paso]='u';
pos-=3;
devolver t
ruda;
}
bool down()
{
if(pos>=6)return false;
s[pos]^=s[pos+3];
s[pos+3]^=s[pos];
s[pos]^=s [pos+3];
ruta[paso]='d';
pos+=3;
devuelve verdadero;
}
bool left()
{
if(pos==0||pos==3||pos==6)return false;
s[pos]^=s[pos-1];
s[pos-1]^=s[pos];
s[pos] ^=s[pos-1];
ruta[paso]='l';
pos--;
devuelve verdadero;
}
bool right()
{
if(pos==2||pos==5||pos==8)return falso;
s[pos]^=s[pos+1];
s[pos+1]^=s[pos];
s [pos]^=s[pos+1];
ruta[paso]='r';
pos++;
devuelve verdadero;
}
bool operator==(const Node&x)const
{
int i;
for(i= 0;i<9;i++)if(s[i]!=x.s[i])devuelve falso;
devuelve verdadero;
}
nulo show()
{
int i,j;
for(i=0;i<=6;i+=3,cout< for(j=i;j<=i+2;j++) cout< } operador bool<(const Node&x)const { int la=0,lb=0,i; for(i=0;i <8;i++)if(s[i]!=i+1)la++;la+=(s[8]!=0); for(i=0;i<8;i++) if(x.s[i]!=i+1)lb++;lb+=(x.s[8]!=0); return la>lb; } }S,A,tmp,top; priority_queue bool hash[362880]; void mkfac(){ int i;for(i=2;i<=9;i++)fac[i]=fac[i-1]*i;} int eval(char c){return c==' x'?0:c-'0';} salida nula (Nodo x) { int i; para(i=1;i<=x.paso;i ++) putchar(x.path[i]); puts(""); } int principal () { char buf[11]; int i,t,l,r; bool flag; mkfac(); while(NULL!=gets(buf)) { t=0; for(i=0;i<=7;i++)A.s[i]=i+1;A.s[8]=0;A.pos=8; for(i=0 ;buf[i];i++) { if(buf[i]==' ')continuar; S.s[t]=eval( buf[i]); if(S.s[t]==0) S.pos=t; t++; } l=r=0; flag=false; memset(hash,false,sizeof(hash)); S.step=0; while(!Q.empty())Q.pop(); Q.push(S); while(!Q.empty()) { top=Q.top(); Q.pop(); top.step++; tmp=top; if(tmp.up()) { si (!hash[t=tmp.hashval()]) { hash[t]=true; Q.push(tmp); if(tmp==A) { //buscar respuesta... salida(tmp); ir a AA; } } } tmp=top; si (tmp.down()) { if(!hash[t=tmp.hashval()]) { hash[t]=true; Q.push(tmp); if(tmp==A) { //buscar respuesta... salida(tmp); ir a AA; } } } tmp=top; if(tmp.left()) { if(!hash[ t=tmp.hashval()]) { hash[t]=true; Q.push(tmp); if(tmp==A) { //buscar respuesta... salida(tmp); ir a AA; } } } tmp=top; if(tmp.right()) { if(!hash[t=tmp. hashval()]) { hash[t]=true; Q.push(tmp); if( tmp==A) { //buscar respuesta... salida(tmp); ir a AA; } } } } AA:flag=true; if(!flag) puts("irresoluble"); } devuelve 0; }