Write a program to solve a 10 times 10 maze. Prompt the user for the input text
ID: 3825599 • Letter: W
Question
Write a program to solve a 10 times 10 maze. Prompt the user for the input text file which contains ten rows (ten characters on each row, no space between characters). The following characters will be in the input file: '*'starting point '1' blocked path '0' possible path 'E' exit point This program should output coordinates of a possible path (starting from the starting point). If no possible path, print "no way out." Turn in: 1) Source code printouts don't forget to comment your code 2) A disk (CD/DVD or flash drive) with ONLY the pertinent *java and *.class files 3) Put everything into a folder 4) The instruction to run your program on DOS' command line in a README.txt file.Explanation / Answer
Here I am explain maze solution using c program
-Maze is a matrix of N*N which is filled with 1 or 0.
-In Maze problem a rat starts from (0,0) and has to reach at (n-1,n-1) which is the exit point of maze.
-Rat can move up, down ,left , right .
consider 3*3 matrix.
1 1 0
0 1 0
1 1 0
-The diagram filled with 1 and 0.
- 1 indicates the allowed move and 0 indicates not allowed to move.
-Rat starts from (0,0) and checks the value of different position like left,right,up and down one by one till its find the 1.
-if it finds positive it will move one by one and reach the destination exit.
-If at any step rat find that there is no possible move ahead and still it has not reached the exit point ,then it moves back one step and check the other alternatives and choose accordingly till its find the path to exit.
-rat start from (0,0) and then check for the right position which is 1.So it moves to that cell and check the right cell which is zero and then checks the down cell which is 1.
It will continue until it reach exit.
-path is given below from start to end
(0,0) -> (0,1) -> (1,1) -> (2,1) -> (3,1)
Using this observation lets code it in basic C language
#include<stdio.h>
#include<conio.h>
#define S 3 //It defines the size
//Below code specifies whether the move is not in danger or not.
int isPerfect(int a,int b,int H[S][S],int dat[J][J])
{
if(a>=0 && a<H && b>=0&&b<H && H[a][b]==1 && !Sol[a][b])
return 1;
else
return 0;
}
//the below code is recursive function and is used to check the path to the exit point.
int recMaze(int H[S][S], int Dat[J][J], int a,int b)
{
if(a==S-1 &&b==S-1)
{
Dat[a][b]=1;
return 1;
}
if(isPerfect (a,b,H,Dat)==1)
{ Sol[x][y]=1;
//This code check whether the right move is possible or not.If possible return 1 else 0.
if(recMaze( H, Dat,a,b+1)==1)
return 1;
//This code check whether the right move is possible or not.If possible return 1 else 0.
if(recMaze (H,Dat,a+1,b)==1)
return 1;
//This code check whether the left move is possible or not.If possible return 1 else 0.
if(recMaze (H, Dat,a,b-1)==1)
return 1;
//This code check whether the up move is possible or not.If possible return 1 else 0.
if(recMaze (H, Dat,a-1,b)==1) /
return 1;
Dat [a][b]=0;
}
return 0;
}
int main()
{
clrscr();//it clear the screen.
//Then it will initialize the matrix.
int H[S][S]={{1,1,0},
{0,1,0},
{1,1,0},
{1,1,1}};
int dat[S][S] = {0};
if(recMaze (H,sol,0,0)==1)
{
//If answers are there it prints.
printf("The result is available");
for(int z=0; z<S;z++)
{
for(int y=0; y<S; y++)
printf("%d ",dat[z][y]);
}
}
else
printf("there is no answers");
getch();
//if no answers it return 0
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.