Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 30th, 2011  |  syntax: C++  |  size: 2.93 KB  |  views: 42  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //int dir = 4;
  2.         //bool direction[Max_Maze][Max_Maze][dir];
  3.         bool visited[Max_Maze][Max_Maze];
  4.  
  5.  
  6.         for (row = 0; row < size; ++ row)
  7.         {
  8.           for (col = 0; col < size; ++ col)
  9.           {
  10.             visited[row][col] = false;
  11.             /*
  12.             for (dir = 0; dir < 4; ++ dir)
  13.             {
  14.               direction[row][col][dir]=false;
  15.             }
  16.             */
  17.           }
  18.         }
  19.  
  20.         bool notSolved = true;
  21.         int path = 0;
  22.         row = 0;
  23.         col = 0;
  24.  
  25.         rowStack.push(row);
  26.         colStack.push(col);
  27.  
  28.         while (notSolved){
  29.  
  30.         //from perspective of person looking at maze on screen
  31.         if (((row-1)>=0)&&(maze[row - 1][col] == 0)/*&&(direction[row][col][0]==false)*/&&(visited[row-1][col]==false)){//if that space is not out of bounds and if you can go up
  32.         //direction[row][col][0] = true;                                                                    //and you have not gone in that direction yet, go up
  33.         row--;
  34.         visited[row][col] = true;
  35.         rowStack.push(row);
  36.         colStack.push(col);
  37.         path++;
  38.         }
  39.         else if (((col+1)<size)&&(maze[row][col + 1] == 0)/*&&(direction[row][col][1]==false)*/&&(visited[row][col+1]==false)){//else if you can go right etc., go right
  40.         //direction[row][col][1] = true;
  41.         col++;
  42.         visited[row][col] = true;
  43.         rowStack.push(row);
  44.         colStack.push(col);
  45.         path++;
  46.         }
  47.         else if (((row+1)<size)&&(maze[row + 1][col] == 0)/*(direction[row][col][2]==false)*/&&(visited[row+1][col]==false)){//else if you can go down etc., go down
  48.         //direction[row][col][2] = true;
  49.         row++;
  50.         visited[row][col] = true;
  51.         rowStack.push(row);
  52.         colStack.push(col);
  53.         path++;
  54.         }
  55.         else if (((col-1)>=0)&&(maze[row][col - 1] == 0)/*&&(direction[row][col][3]==false)*/&&(visited[row][col-1]==false)){//else if you can go left etc., go left
  56.         //direction[row][col][3] = true;
  57.         col--;
  58.         visited[row][col] = true;
  59.         rowStack.push(row);
  60.         colStack.push(col);
  61.         path++;
  62.         }
  63.         else{//if stuck
  64.             if (path == 0){
  65.             cout << "No Solution Path" << endl;
  66.             notSolved = false;
  67.             }
  68.             else{
  69.             rowStack.pop();
  70.             colStack.pop();
  71.             row = rowStack.top();
  72.             col = colStack.top();
  73.             path--;
  74.             }
  75.         }
  76.             if((maze[row][col] == 0) && (row == (size - 1) && col == (size - 1))){//if we reached an exit
  77.                 cout << "Solution Path:(in reverse)" << endl;
  78.                 for (int i = 0; i <= path; i++){
  79.                     cout << "row:" << rowStack.top() << " col:" << colStack.top() << endl;
  80.                     rowStack.pop();
  81.                     colStack.pop();
  82.                 }
  83.             notSolved = false;
  84.             }
  85.  
clone this paste RAW Paste Data