Maze
Maze
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; } } }