|
void Search( stepNum, currState, stepsToSol[] )
{
if ( stepNum >= MAXSTEPS allowed )
return; // Dead end, try next branch
if ( currState is a solution )
{ print( currState ); // Print solution
print( stepsToSol[ ] ); // Print steps to achieve solution
if ( you only need 1 solution )
exit(0);
else
return; // Continue to find other solutions...
}
// Make a step and search further
for ( every legal step S in currState )
{ currState = applyStep(S, currState)
add S to stepsToSol[]
Search( stepNum+1, (new) currState, stepsToSol[] );
// Walk back the previous step when Search returns
currState = removeStep(S, currState)
remove S from stepsToSol[];
} // Loop back --> try next step S
|
void Search( stepNum, currState, stepsToSol[] )
{
if ( stepNum >= MAXSTEPS allowed )
return; // Dead end, try next branch
if ( currState is a solution )
{ print( currState ); // Print solution
print( stepsToSol[ ] ); // Print steps to achieve solution
if ( you only need 1 solution )
exit(0);
else
return; // Continue to find other solutions...
}
// Make a step and search further
for ( every legal step S in currState )
{ newState = applyStep(S, currState)
add S to stepsToSol[]
Search( stepNum+1, newState, stepsToSol[] );
// Walk back the previous step when Search returns
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(1) AA --> AB (No match --> fail)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(1) AA --> AB (No match --> fail)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(2) AB --> BB (No match --> fail)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(1) AA --> AB (No match --> fail)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
currState = BBB s=[2 1 BB, 3 1 AAB, 1 1 ABB, 2 1 BBB]
Search( 4, BBB, [2 1 BB, 3 1 AAB, 1 1 ABB, 2 1 BBB])
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
currState = BBB s=[2 1 BB, 3 1 AAB, 1 1 ABB, 2 1 BBB]
Search( 4, BBB, [2 1 BB, 3 1 AAB, 1 1 ABB, 2 1 BBB])
stepNum == S --> check if BBB is solution
No ---> return
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
currState = BBB s=[2 1 BB, 3 1 AAB, 1 1 ABB, 2 1 BBB]
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
currState = ABB s=[2 1 BB, 3 1 AAB, 1 1 ABB]
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(2) AB --> BB (Match --> make step forward)
currState = ABB s=[2 1 BB, 3 1 AAB, 1 1 ABB]
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(3) B --> AA (Match --> make step forward)
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(3) B --> AA (Match --> make step forward)
currState = AAAB s=[2 1 BB, 3 1 AAB, 1 1 ABB, 3 2 AAAB]
Search( 4, AAAB, [2 1 BB, 3 1 AAB, 1 1 ABB, 3 2 AAAB])
|
Sample input: (1) AA --> AB S = 4
(2) AB --> BB Init = AB
(3) B --> AA Final = AAAB
Search( 0, AB, [ ]) // stepNum = 0, currState = AB, stepsToSol = empty
(2) AB --> BB (Match --> make step forward)
currState = BB stepsToSol = [2 1 BB]
Search( 1, BB, [2 1 BB])
(3) B --> AA (Match --> make step forward)
currState = AAB stepsToSol = [2 1 BB, 3 1 AAB]
Search( 2, AAB, [2 1 BB, 3 1 AAB])
(1) AA --> AB (Match --> make step forward)
currState = ABB stepsToSol = [2 1 BB, 3 1 AAB, 1 1 ABB]
Search( 3, ABB, [2 1 BB, 3 1 AAB, 1 1 ABB])
(3) B --> AA (Match --> make step forward)
currState = AAAB s=[2 1 BB, 3 1 AAB, 1 1 ABB, 3 2 AAAB]
Search( 4, AAAB, [2 1 BB, 3 1 AAB, 1 1 ABB, 3 2 AAAB])
stepNum == S ---> Check for solution
AAAB == Final !
DONE
|
Data representation:
string src[3], dest[3]; // translation rules
// src[0] --> dest[0]
// src[1] --> dest[1]
// src[2] --> dest[2]
int S; // Stop length
string Init, Final; // Initial and Final string
vector<string> steps; // steps[0] = first step (e.g.: "2 1 BB")
// steps[1] = 2nd step (e.g.: "3 1 AAB")
|
Algorithm in pseudo code:
void Search(int nSteps, string currStr, vector |
Algorithm in C++: ( CCC/2019/j5.cc)
void search(int nSteps, string currStr, vector |