Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //int dir = 4;
- //bool direction[Max_Maze][Max_Maze][dir];
- bool visited[Max_Maze][Max_Maze];
- for (row = 0; row < size; ++ row)
- {
- for (col = 0; col < size; ++ col)
- {
- visited[row][col] = false;
- /*
- for (dir = 0; dir < 4; ++ dir)
- {
- direction[row][col][dir]=false;
- }
- */
- }
- }
- bool notSolved = true;
- int path = 0;
- row = 0;
- col = 0;
- rowStack.push(row);
- colStack.push(col);
- while (notSolved){
- //from perspective of person looking at maze on screen
- 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
- //direction[row][col][0] = true; //and you have not gone in that direction yet, go up
- row--;
- visited[row][col] = true;
- rowStack.push(row);
- colStack.push(col);
- path++;
- }
- 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
- //direction[row][col][1] = true;
- col++;
- visited[row][col] = true;
- rowStack.push(row);
- colStack.push(col);
- path++;
- }
- 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
- //direction[row][col][2] = true;
- row++;
- visited[row][col] = true;
- rowStack.push(row);
- colStack.push(col);
- path++;
- }
- 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
- //direction[row][col][3] = true;
- col--;
- visited[row][col] = true;
- rowStack.push(row);
- colStack.push(col);
- path++;
- }
- else{//if stuck
- if (path == 0){
- cout << "No Solution Path" << endl;
- notSolved = false;
- }
- else{
- rowStack.pop();
- colStack.pop();
- row = rowStack.top();
- col = colStack.top();
- path--;
- }
- }
- if((maze[row][col] == 0) && (row == (size - 1) && col == (size - 1))){//if we reached an exit
- cout << "Solution Path:(in reverse)" << endl;
- for (int i = 0; i <= path; i++){
- cout << "row:" << rowStack.top() << " col:" << colStack.top() << endl;
- rowStack.pop();
- colStack.pop();
- }
- notSolved = false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement