vi oldId =
id( oldState
);
int&
oldDir = direction[oldId];
//--- Apply all applicable moves to it and handle the new state.
for(
int move=0;
move<18;
move++ ){
if(
applicableMoves[phase]
& (1
<< move) ){
//--- Apply the move.
vi newState
= applyMove(
move, oldState
);
vi newId
= id(
newState );
int&
newDir = direction[newId];
//--- Have we seen this state (id) from the other direction already?
//--- I.e. have we found a connection?
if(
newDir && newDir
!= oldDir ){
//--- Make oldId represent the forwards and newId the backwards search state.
if(
oldDir > 1
){
swap(
newId, oldId
);
move =
inverse( move
);
}
//--- Reconstruct the connecting algorithm.
vi algorithm(
1, move
);
while(
oldId != currentId
){
algorithm.insert(
algorithm.begin(),
lastMove[ oldId
] );
oldId =
predecessor[ oldId
];
}
while(
newId != goalId
){
algorithm.push_back(
inverse( lastMove[
newId ] ));
newId =
predecessor[ newId
];
}
//--- Print and apply the algorithm.
for(
int i=0;
i<(int)algorithm.size();
i++ ){
cout <<
"UDFBLR"[algorithm[i]/3]
<< algorithm[i]%3+1;
currentState
= applyMove(
algorithm[i],
currentState );
}
//--- Jump to the next phase.
goto nextPhasePlease;
}
//--- If we've never seen this state (id) before, visit it.
if(
! newDir ){
q.push(
newState );
newDir =
oldDir;
lastMove[
newId ] =
move;
predecessor[
newId ] =
oldId;
}
}
}
}
nextPhasePlease:
;
}
}