0% found this document useful (0 votes)
205 views6 pages

Maze

This C++ program uses a stack data structure to solve maze problems. It reads a maze from a file, finds the exit, then traces paths from different entrance points to the exit. It handles decisions at intersections by pushing alternative paths to a second stack. The optimal path is output to a separate file.

Uploaded by

Bishoy Emile
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views6 pages

Maze

This C++ program uses a stack data structure to solve maze problems. It reads a maze from a file, finds the exit, then traces paths from different entrance points to the exit. It handles decisions at intersections by pushing alternative paths to a second stack. The optimal path is output to a separate file.

Uploaded by

Bishoy Emile
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include<iostream> #include<fstream> #include <string> using namespace std; ifstream rff("maze.txt");//rff:ReadFromFile ofstream ptf("mazesolution.

txt");//ptf:Print To File struct point { int row; int col; }; struct node { point data; node*next; }; struct stack { node*top; int count; }; bool isEmpty(stack*&s) { if (s->count==0) return true; else return false; } bool isFull(stack*&s) { node*temp = new node; if (temp==0) return true; else return false; } bool push(stack*&s,point p) { if (isFull(s)==false) { node*temp= new node; temp->data.row= p.row; temp->data.col= p.col; temp->next=s->top; s->top=temp; s->count ++; return true; } else return false; } bool pop(stack*&s,point&p) { if (isEmpty(s)==false) { p.row=s->top->data.row; p.col=s->top->data.col; s->count --; node*temp=s->top; s->top=s->top->next;

delete temp; return true; } else return false; } bool top(stack*&s,point&p) { if (isEmpty(s)==false) { p.row=s->top->data.row; p.col=s->top->data.col; return true; } else return false; } bool createStack(stack*&s) { s=new stack; s->top=NULL; s->count=0; return true; } void createArray(int** &maze,int row,int col) { maze = new int*[row]; for(int i=0;i<row;i++) maze[i]= new int[col]; } void enterMatrix (int **&M,int row,int col) { char ch; for (int i=0;i<row;i++) { int counter=0; for(int j=0;j<col*2-1;j++) { if(j%2==0) { rff>>M[i][counter]; counter++; } else rff>>ch; } } } point findExit(int**M,int r,int c) { int i=0,j=0; point p; while (M[i][j] !=0) { if ( j==0&&i!=r-1)//in the first column and not reaching the las t row { i++; } else if(i==r-1&&j !=c-1)// in the last row but not the last colu

mn { j++; } else if(j==c-1&& i!=0)//in the last column but not the first row { i--; } else if (i==0 && j!=0)//in the first row still searching for the exit j--; if( i==0 && j==0)// no exit at all break; } p.row=i; p.col=j; return p; } bool isInvalid(int **A,point pos,point size) { if( (pos.row>=size.row||pos.col>=size.col||pos.row<0||pos.col<0))//outsi de the matrix return true; else if(A[pos.row][pos.col]==1) return true; else return false; } bool isContinue(int**M,point pos ) { int count=0; point c=pos; for (int i=0;i<4;i++) { pos=c; if(i==0) pos.col++; else if(i==1) pos.col--; else if(i==2) pos.row--; else pos.row++; if (M[pos.row][pos.col]==0) count++; } return (count==1); } bool isIntersection(int **&M,point pos,int &control ) { int count=0; point c=pos; for (int i=0;i<4;i++) { pos=c; if(i==0) pos.col++; else if(i==1) pos.col--; else if(i==2)

pos.row--; else pos.row++; if (M[pos.row][pos.col]==0) count++; } control=count; return (count>1); } void path(int **Maze,point mouse,point dest,point size) { if (isInvalid(Maze,mouse,size))//checking whether the mouse is in a wron g place or not { ptf<<"Invalid"; return; } int control;//Control number of decisions; point pos; point decision; point c_p;//Saves last visited position; decision .row=-1; decision.col=-1; stack *visited,*alt; createStack(visited); createStack(alt); push(visited,mouse); Maze[mouse.row][mouse.col]=2;//any visited spot will be equal 2 so as no t to be passed more than once top(visited,pos); while (dest.row!=pos.row || dest.col !=pos.col) { if (isContinue(Maze,pos)) { top(visited,c_p); for (int i=0;i<4;i++) { pos=c_p; if (i==0) pos.col++; else if(i==1) pos.col--; else if (i==2) pos.row--; else pos.row++; if (Maze[pos .row][pos.col]==0) { push(visited,pos); Maze[pos.row][pos.col]=2; break; } } } else if(isIntersection(Maze,pos,control)) { top(visited,c_p); for (int j=1;j<control;j++) { push(visited,decision);

} for (int i=0;i<4;i++) { pos=c_p; if (i==0) pos.col++; else if(i==1) pos.col--; else if (i==2) pos.row--; else pos.row++; if (Maze[pos .row][pos.col]==0) { push(alt,pos); Maze[pos.row][pos.col]=3;//so as not to take any alternative twice in the same stack } } pop(alt,pos); push(visited,pos); Maze[pos.row][pos.col]=2; } else { if(isEmpty(alt)) { ptf<<"Trapped"; return; } while(pos.row!=decision.row ||pos.col ! =decision.col) { pop(visited,pos); if (pos.row!=decision.row ||pos .col !=decision.col) { Maze[pos.row][pos.col]= 4; } } pop(alt,pos); push(visited,pos); Maze[pos.row][pos.col]=2; } }//Reaching exit stack *path; createStack(path); while (!isEmpty(visited)) { pop(visited,pos); if(pos.row != decision.row && pos.col !=decisio n.col)//removing decision tokens { push(path,pos); } } while (!isEmpty(path)) { pop(path,pos);

ptf<<"("<<pos.row<<","<<pos.col<<")"<<" "; if (!isEmpty(path)) { ptf<<"-"<<" "; } } } void deleteMat(int **M,int row) { for (int i=0;i<row;i++) { delete M[i]; } delete M; } int main() { while(!rff.eof()) { int **M=NULL; string mazename; char ch; rff>>mazename; point size; rff>>size.row; rff>>ch; rff>>size.col; createArray(M,size.row,size.col); enterMatrix(M,size.row,size.col); point dest=findExit(M,size.row,size.col); int n; rff>>n; ptf<<mazename; ptf<<endl<<endl; for(int i=1;i<=n;i++) { point mouse; rff>>mouse.row; rff>>ch; rff>>mouse.col; ptf<<"Entrance:"<<" "<<mouse.row<<ch<<mouse.col<<" "; path(M,mouse,dest,size); ptf<<endl; ptf<<endl; } } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy