I need to solve the mouse come out from the maze.This is my coding..Can anyone help me solve the problem??

moon5896

Estimable
Nov 10, 2014
2
0
4,510
C++:
#include<conio.h>
#include<stdio.h>
#define SIZE 15
#include<stdlib.h>
void main()
{
	int maze[SIZE][SIZE],mark[SIZE][SIZE],stack[SIZE][3];
	static int move[8][2]={-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1};
	int i,j,m,n,top,mov,g,h;
	system("cls");
	printf("enter size");
	scanf("%d%d",&m,&n);
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=n;j++)
		{
			scanf("%d",&maze[i][j]);
		}
	}
	for(i=0;i<=n+1;i++)
	{
		maze[0][i]=1;
	}
	
	for(i=0;i<=n+1;i++)
	{
		maze[m+1][i]=1;
	}
	for(i=0;i<=m+1;i++)
	{
		maze[i][0]=1;
	}
	for(i=0;i<=m+1;i++)
	{
		maze[i][n+1]=1;
	}
	for(i=1;i<=m;i++)
	{
		for(j=1;j<=n;j++)
		{
			mark[i][j]=0;
		}
	}
mark[1][1]=1;
stack[0][0]=1;
stack[0][1]=1;
stack[0][2]=2;
top=1;
while(top!=0)
{
i=stack[0][0];
j=stack[0][1];
mov=stack[0][2];
top=top-1;
while(mov<=7)
{
g=i+move[mov][0];
h=j+move[mov][1];

if(mark[g][h]==0&&maze[g][h]==0)
{
mark[g][h]=1;
top++;
stack[top][0]=i;
stack[top][1]=j;
mov=-1;
i=g;j=h;
}
mov=mov+1;
if(g==m&&h==n)
{
printf("path made by the rat is");
for(i=1;i<=top;i++)
printf("%d %d",stack[i][0],stack[i][1]);
printf("%d %d",m,n);
getch();
exit(0);
}
}
}
}
C++:
 
Solution
It seems to be "a solution" for classic maze problem, implemented with local stack instead of recursively. I see following problems with it:
- there is no check that maze' sizes (m, n) are valid (positive, less than SIZE);
- there is no check that there is still free space in the stack when storing next move;
- entering initial data would be a nightmare
It seems to be "a solution" for classic maze problem, implemented with local stack instead of recursively. I see following problems with it:
- there is no check that maze' sizes (m, n) are valid (positive, less than SIZE);
- there is no check that there is still free space in the stack when storing next move;
- entering initial data would be a nightmare
 
Solution