I am writing Pacman, for the pursuit algorithm I took the wave algorithm, but how not to twist, it is heavy. When updating the coordinates of the peckman, the path of the ghost to him is updated, and here the search for the shortest path makes itself felt. What are more efficient algorithms on matrices?
Here is the code for finding the path:
void Enemy::getPathToTarget() { pathToPacman.clear (); const Position * playerPosition = Player::playerPosition (); Field = Maze::getField (); int height = Field.size(); int width = Field[0].size(); int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; int d, x, y, k; bool stop; Field[playerPosition->x][playerPosition->y] = 1; if (Field[position->x][position->y] == 0 || Field[playerPosition->x][playerPosition->y] == 0) return; d = 6; Field[position->x][position->y] = 6; do { stop = true; for ( y = 0; y < height; ++y ) for ( x = 0; x < width;++x ) if ( Field[y][x] == d ) { for ( k = 0; k < 4; ++k ) { int iy=y + dy[k]; int ix = x + dx[k]; if ( iy >= 0 && iy < height && ix >= 0 && ix < width && ((Field[iy][ix] == 1) || (Field[iy][ix] == 2) || (Field[iy][ix] == 3))) { stop = false; Field[iy][ix] = d + 1; } } } ++d; } while ( !stop && (Field[playerPosition->x][playerPosition->y] == 1 || (Field[playerPosition->x][playerPosition->y] == 2) || Field[playerPosition->x][playerPosition->y] == 3)); if (Field[playerPosition->x][playerPosition->y] == 1) return; d = Field[playerPosition->x][playerPosition-> y = playerPosition->x; x = playerPosition->y; int val = 0; while ( d > 6 ) { pathToPacman.push (Position(y, x)); d--; for (k = 0; k < 4; ++k) { int iy=y + dy[k], ix = x + dx[k]; if ( iy >= 0 && iy < height && ix >= 0 && ix < width && Field[iy][ix] == d) { x = x + dx[k]; y = y + dy[k]; break; } } } timer.start (10000); }
O(n), wherenis the number of vertices of the lattice graph. - Zealint 5:09do { for ( y = 0; y < height; ++y ) for ( x = 0; x < width;++x )Nothing that is already a potential complexity instead ofO(N*M)O(N*N*M*M)did not read further. This is not a wave circuit and has nothing to do with it. I tried to find a minute 2 in this code for aочередьor something (an array, a list, a vector) like it, failed. - pavel