2012-01-11 98 views
1

我遇到了一个我为创建ascii迷宫而编写的算法的问题。该代码使用递归回到跟踪器,以及伪代码本质是:递归回溯迷宫生成算法堆栈循环

1. Make the initial cell the current cell and mark it as visited 
2. While there are unvisited cells 
    1. If the current cell has any neighbours which have not been visited 
     1. Choose randomly one of the unvisited neighbours 
     2. Push the chosen cell to the stack 
     3. Remove the wall between the current cell and the chosen cell 
     4. Make the chosen cell the current cell and mark it as visited 
    2. Else 
     1. Pop a cell from the stack 
     2.Make it the current cell 

,我有是代码被卡在增加了栈,然后出栈的循环问题。该代码目前第19行使用一个系统命令的Linux,如果任何人需要改变它的Windows机器

#include<iostream> 
#include<cstdlib> 
#include<stack> 
#include<ctime> 

#define NORTH 0 
#define SOUTH 1 
#define EAST 2 
#define WEST 3 
#define SIZEX 20 
#define SIZEY 20 

using namespace std; 

int nGood = 0; 
int locX = 1, locY = 1; 

void printGrid(char grid[SIZEY][SIZEX]){ 
system("clear"); 
for (int i = 0; i < SIZEY; i++){ 
    for(int j = 0; j < SIZEX; j++){ 
     cout << grid[i][j]; 
    } 
    cout << endl; 
} 
} 

int moveEW(int direction, int x){ 
    if (direction == EAST) 
      return x + 1; 
    else if (direction == WEST) 
      return x - 1; 
    else 
      return x; 
} 

int moveNS(int direction, int y){ 
    if (direction == NORTH) 
      return y - 1; 
    else if (direction == SOUTH) 
      return y + 1; 
    else 
      return y; 
} 

bool isGood(int x, int y, int direction, char grid[SIZEY][SIZEX]){ 
x = moveEW(direction,x); 
y = moveNS(direction,y); 

if (grid[x][y] == '.' || x >= (SIZEX - 1) || x <= 0 || y <= 0 || y >= (SIZEY - 1)){ 
    return false; 
} 

// check cardinal directions 
if (direction == NORTH){ 
    if (grid[y][x-1] != '.' && grid[y-1][x] != '.' && grid[y][x+1] != '.' && grid[y-1][x-1] != '.' && grid[y-1][x+1] != '.'){ 
     return true; 
    } 
} 
if (direction == SOUTH){ 
      if (grid[y][x-1] != '.' && grid[y+1][x] != '.' && grid[y][x+1] != '.' && grid[y+1][x-1] != '.' && grid[y+1][x+1] != '.'){ 
        return true; 
      } 
    } 
if (direction == EAST){ 
      if (grid[y][x+1] != '.' && grid[y-1][x] != '.' && grid[y+1][x] != '.' && grid[y-1][x+1] != '.' && grid[y+1][x+1] != '.'){ 
        return true; 
      } 
    } 
if (direction == WEST){ 
      if (grid[y][x-1] != '.' && grid[y-1][x] != '.' && grid[y+1][x] != '.' && grid[y-1][x-1] != '.' && grid[y+1][x-1] != '.'){ 
        return true; 
      } 
    } 
return false; 
} 

main(){ 
char grid[SIZEY][SIZEX]; 

// init grid 
for (int i = 0; i < SIZEY; i++){ 
      for(int j = 0; j < SIZEX; j++){ 
        grid[i][j] = '#'; 
      } 
    } 

//init rand 
srand(time(0)); 

//init stacks for xy coords 
stack<int> xValues; 
stack<int> yValues; 

nGood = 0; 
int direction = 0; 

do{ 
    //find n good moves 
    for (int i = 0; i < 4; i++){ 
     if (isGood(locX,locY,i,grid)) 
      nGood++; 
    } 

    // if only 1 good move, move there 
    if (nGood == 1){ 
     if (isGood(locX,locY,NORTH,grid)) 
      locY = moveNS(NORTH,locY); 
     else if (isGood(locX,locY,SOUTH,grid)) 
      locY = moveNS(SOUTH,locY); 
     else if (isGood(locX,locY,EAST,grid)) 
          locX = moveEW(EAST,locX); 
        else if (isGood(locX,locY,WEST,grid)) 
          locX = moveEW(WEST,locX); 
    } 

    // if no good moves, move back in stack 
    else if (nGood == 0){ 
     locX = xValues.top(); 
     locY = yValues.top(); 
     xValues.pop(); 
     yValues.pop(); 
    } 

    //if more than 1 good move, push stack 
    else if (nGood > 1){ 
     xValues.push(locX); 
     yValues.push(locY); 

     //direction to move randomly chosen 
     do{ 
      direction = rand() % 4; 
     }while (!isGood(locX,locY,direction,grid)); 

     locX = moveEW(direction,locX); 
     locY = moveNS(direction,locY); 
    } 

    // set grid 
    grid[locY][locX] = '.'; 
    //output grid to show creation 
    printGrid(grid); 
    //reset nGood value 
      nGood = 0; 

}while(!xValues.empty()); 

//final maze output 
printGrid(grid); 
return 0; 
} 
+1

这是一个惊人的问题真棒标题。 – Dennis 2012-01-11 14:36:09

回答

1

您在isGood功能交换在第一X和Y如果(网格[X] [Y]代替格[Y] [X])。这至少有时会导致问题。

+0

非常感谢,我不敢相信我错过了这一点 – dhalik 2012-01-13 03:08:28