-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAZE PROBLEM(RECURSION)RegNo-149105006(CSE Sec C), Samuel Wilson.cpp
126 lines (87 loc) · 1.84 KB
/
MAZE PROBLEM(RECURSION)RegNo-149105006(CSE Sec C), Samuel Wilson.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
int j=-1;
int C,R; // store the indices of the exit(can be explicitly defined or input by the user
const int x=9,y=8; //size of the maze(no. of rows and columns, respectively). Ideally, to
static int a[1000];//stores the moves in sequence
int m[x][y]={{1,0,0,1,1,1,1,1},{1,1,0,0,0,0,0,1},{0,0,0,1,0,1,1,1},{1,1,0,1,1,1,1,1},{1,1,0,1,1,1,1,1},{1,1,0,0,0,1,1,1},{1,1,0,1,0,1,1,1},{1,1,0,1,0,0,1,1},{1,1,1,1,1,0,1,1}};//could be as big as the memory allows
int mazeRec(int r,int c,int i)
{
if(i!=0)
a[++j]=i;
if(c==C && r==R)
return 1;
if(r!=0 && i!=3)
{
if(m[r-1][c]!=1)
{
if(mazeRec(r-1,c,1)==1)
return 1;
}
}
if(c!=(y-1) && i!=4)
{
if(m[r][c+1]!=1)
{
if(mazeRec(r,c+1,2)==1)
return 1;
}
}
if(r!=(x-1) && i!=1)
{
if(m[r+1][c]!=1)
{
if(mazeRec(r+1,c,3)==1)
return 1;
}
}
if(c!=0 && i!=2)
{
if(m[r][c-1]!=1)
{
if(mazeRec(r,c-1,4)==1)
return 1;
}
}
a[j--]=0;
return 0;
}
int main()
{
int r,c,k=0,l;
printf("Enter the starting indices(row, then column)\n");//indices start from 0. Eg. 0,0 for the first cell
scanf("%d",&r);
scanf("%d",&c);
if(m[r][c]!=0)
{
printf("The entered cell is not part of a valid pathway in the maze under consideration");
exit(0);
}
printf("Enter the ending indices(row, then column)\n");
scanf("%d",&R);
scanf("%d",&C);
if(m[R][C]!=0)
{
printf("The entered cell is not part of a valid pathway in the maze under consideration");
exit(0);
}
l=mazeRec(r,c,0);
if(l==0)
{
printf("There is no way out, Sucker!\n");
exit(0);
}
while(a[k]!=0)
{
if(a[k]==1)
printf("Top\n");
if(a[k]==2)
printf("Right\n");
if(a[k]==3)
printf("Down\n");
if(a[k]==4)
printf("Left\n");
k++;
}
}