📄 8puzzle.cpp
字号:
};
int tile_y[ BOARD_WIDTH * BOARD_HEIGHT ] =
{
/* TL_SPACE */ 1,
/* TL_1 */ 0,
/* TL_2 */ 0,
/* TL_3 */ 0,
/* TL_4 */ 1,
/* TL_5 */ 2,
/* TL_6 */ 2,
/* TL_7 */ 2,
/* TL_8 */ 1,
};
s=0;
// score 1 point if centre is not correct
if( tiles[(BOARD_HEIGHT*BOARD_WIDTH)/2] != nodeGoal.tiles[(BOARD_HEIGHT*BOARD_WIDTH)/2] )
{
s = 1;
}
for( i=0; i<(BOARD_HEIGHT*BOARD_WIDTH); i++ )
{
// this loop adds up the totaldist element in h and
// the sequence score in s
// the space does not count
if( tiles[i] == TL_SPACE )
{
continue;
}
// get correct x and y of this tile
cx = tile_x[tiles[i]];
cy = tile_y[tiles[i]];
// get actual
ax = i % BOARD_WIDTH;
ay = i / BOARD_WIDTH;
// add manhatten distance to h
h += abs( cx-ax );
h += abs( cy-ay );
// no s score for center tile
if( (ax == (BOARD_WIDTH/2)) && (ay == (BOARD_HEIGHT/2)) )
{
continue;
}
// score 2 points if not followed by successor
if( correct_follower_to[ tiles[i] ] != tiles[ clockwise_tile_of[ i ] ] )
{
s += 2;
}
}
// mult by 3 and add to h
t = h + (3*s);
return (float) t;
}
bool PuzzleState::IsGoal( PuzzleState &nodeGoal )
{
return IsSameState( nodeGoal );
}
// Helper
// Return the x and y position of the space tile
void PuzzleState::GetSpacePosition( PuzzleState *pn, int *rx, int *ry )
{
int x,y;
for( y=0; y<BOARD_HEIGHT; y++ )
{
for( x=0; x<BOARD_WIDTH; x++ )
{
if( pn->tiles[(y*BOARD_WIDTH)+x] == TL_SPACE )
{
*rx = x;
*ry = y;
return;
}
}
}
assert( false && "Something went wrong. There's no space on the board" );
}
int PuzzleState::GetMap( int x, int y, TILE *tiles )
{
if( x < 0 ||
x >= BOARD_WIDTH ||
y < 0 ||
y >= BOARD_HEIGHT
)
return GM_OFF_BOARD;
if( tiles[(y*BOARD_WIDTH)+x] == TL_SPACE )
{
return GM_SPACE;
}
return GM_TILE;
}
// Given a node set of tiles and a set of tiles to move them into, do the move as if it was on a tile board
// note : returns false if the board wasn't changed, and simply returns the tiles as they were in the target
// spx and spy is the space position while tx and ty is the target move from position
bool PuzzleState::LegalMove( TILE *StartTiles, TILE *TargetTiles, int spx, int spy, int tx, int ty )
{
int t;
if( GetMap( spx, spy, StartTiles ) == GM_SPACE )
{
if( GetMap( tx, ty, StartTiles ) == GM_TILE )
{
// copy tiles
for( t=0; t<(BOARD_HEIGHT*BOARD_WIDTH); t++ )
{
TargetTiles[t] = StartTiles[t];
}
TargetTiles[ (ty*BOARD_WIDTH)+tx ] = StartTiles[ (spy*BOARD_WIDTH)+spx ];
TargetTiles[ (spy*BOARD_WIDTH)+spx ] = StartTiles[ (ty*BOARD_WIDTH)+tx ];
return true;
}
}
return false;
}
// This generates the successors to the given PuzzleState. It uses a helper function called
// AddSuccessor to give the successors to the AStar class. The A* specific initialisation
// is done for each node internally, so here you just set the state information that
// is specific to the application
bool PuzzleState::GetSuccessors( AStarSearch<PuzzleState> *astarsearch, PuzzleState *parent_node )
{
PuzzleState NewNode;
int sp_x,sp_y;
GetSpacePosition( this, &sp_x, &sp_y );
bool ret;
if( LegalMove( tiles, NewNode.tiles, sp_x, sp_y, sp_x, sp_y-1 ) == true )
{
ret = astarsearch->AddSuccessor( NewNode );
if( !ret ) return false;
}
if( LegalMove( tiles, NewNode.tiles, sp_x, sp_y, sp_x, sp_y+1 ) == true )
{
ret = astarsearch->AddSuccessor( NewNode );
if( !ret ) return false;
}
if( LegalMove( tiles, NewNode.tiles, sp_x, sp_y, sp_x-1, sp_y ) == true )
{
ret = astarsearch->AddSuccessor( NewNode );
if( !ret ) return false;
}
if( LegalMove( tiles, NewNode.tiles, sp_x, sp_y, sp_x+1, sp_y ) == true )
{
ret = astarsearch->AddSuccessor( NewNode );
if( !ret ) return false;
}
return true;
}
// given this node, what does it cost to move to successor. In the case
// of our map the answer is the map terrain value at this node since that is
// conceptually where we're moving
float PuzzleState::GetCost( PuzzleState &successor )
{
return 1.0f; // I love it when life is simple
}
// Main
int main( int argc, char *argv[] )
{
cout << "STL A* 8-puzzle solver implementation\n(C)2001 Justin Heyes-Jones\n";
bool bUserBoard = false;
if( argc > 1 )
{
char *userboard = argv[1];
int i = 0;
int c;
while( c = argv[1][i] )
{
if( isdigit( c ) )
{
int num = (c - '0');
PuzzleState::g_start[i] = static_cast<PuzzleState::TILE>(num);
}
i++;
}
}
// Create an instance of the search class...
AStarSearch<PuzzleState> astarsearch;
int NumTimesToSearch = NUM_TIMES_TO_RUN_SEARCH;
while( NumTimesToSearch-- )
{
// Create a start state
PuzzleState nodeStart( PuzzleState::g_start );
// Define the goal state
PuzzleState nodeEnd( PuzzleState::g_goal );
// Set Start and goal states
astarsearch.SetStartAndGoalStates( nodeStart, nodeEnd );
unsigned int SearchState;
unsigned int SearchSteps = 0;
do
{
SearchState = astarsearch.SearchStep();
#if DEBUG_LISTS
float f,g,h;
cout << "Search step " << SearchSteps << endl;
cout << "Open:\n";
PuzzleState *p = astarsearch.GetOpenListStart( f,g,h );
while( p )
{
((PuzzleState *)p)->PrintNodeInfo();
cout << "f: " << f << " g: " << g << " h: " << h << "\n\n";
p = astarsearch.GetOpenListNext( f,g,h );
}
cout << "Closed:\n";
p = astarsearch.GetClosedListStart( f,g,h );
while( p )
{
p->PrintNodeInfo();
cout << "f: " << f << " g: " << g << " h: " << h << "\n\n";
p = astarsearch.GetClosedListNext( f,g,h );
}
#endif
// Test cancel search
#if 0
int StepCount = astarsearch.GetStepCount();
if( StepCount == 10 )
{
astarsearch.CancelSearch();
}
#endif
SearchSteps++;
}
while( SearchState == AStarSearch<PuzzleState>::SEARCH_STATE_SEARCHING );
if( SearchState == AStarSearch<PuzzleState>::SEARCH_STATE_SUCCEEDED )
{
#if DISPLAY_SOLUTION_FORWARDS
cout << "Search found goal state\n";
#endif
PuzzleState *node = astarsearch.GetSolutionStart();
#if DISPLAY_SOLUTION_FORWARDS
cout << "Displaying solution\n";
#endif
int steps = 0;
#if DISPLAY_SOLUTION_FORWARDS
node->PrintNodeInfo();
cout << endl;
#endif
for( ;; )
{
node = astarsearch.GetSolutionNext();
if( !node )
{
break;
}
#if DISPLAY_SOLUTION_FORWARDS
node->PrintNodeInfo();
cout << endl;
#endif
steps ++;
};
#if DISPLAY_SOLUTION_FORWARDS
// todo move step count into main algorithm
cout << "Solution steps " << steps << endl;
#endif
////////////
node = astarsearch.GetSolutionEnd();
#if DISPLAY_SOLUTION_BACKWARDS
cout << "Displaying reverse solution\n";
#endif
steps = 0;
node->PrintNodeInfo();
cout << endl;
for( ;; )
{
node = astarsearch.GetSolutionPrev();
if( !node )
{
break;
}
#if DISPLAY_SOLUTION_BACKWARDS
node->PrintNodeInfo();
cout << endl;
#endif
steps ++;
};
#if DISPLAY_SOLUTION_BACKWARDS
cout << "Solution steps " << steps << endl;
#endif
//////////////
// Once you're done with the solution you can free the nodes up
astarsearch.FreeSolutionNodes();
}
else if( SearchState == AStarSearch<PuzzleState>::SEARCH_STATE_FAILED )
{
#if DISPLAY_SOLUTION_INFO
cout << "Search terminated. Did not find goal state\n";
#endif
}
else if( SearchState == AStarSearch<PuzzleState>::SEARCH_STATE_OUT_OF_MEMORY )
{
#if DISPLAY_SOLUTION_INFO
cout << "Search terminated. Out of memory\n";
#endif
}
// Display the number of loops the search went through
#if DISPLAY_SOLUTION_INFO
cout << "SearchSteps : " << astarsearch.GetStepCount() << endl;
#endif
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -