-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRatMaze.c
76 lines (59 loc) · 1.21 KB
/
RatMaze.c
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
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
int prow[]={0,0,-1,1};
int pcol[]={1,-1,0,0};
bool canmove(int a[4][4],int visit[4][4],int row,int col);
void ratmaze(int a[4][4],int visit[4][4],int row,int col,int desrow,int descol,int move)
{
if((row==desrow) && (col==descol))
{
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
printf("%d ",visit[i][j]);
}
printf("\n");
}
}
else{
int size= sizeof(prow)/sizeof(prow[0]);
for(int index=0;index<size;index++)
{
int newrow= row + prow[index];
int newcol= col + pcol[index];
if(canmove(a,visit,newrow,newcol))
{
move++;
visit[newrow][newcol]=move;
ratmaze(a,visit,newrow,newcol,desrow,descol,move);
move--;
visit[newrow][newcol]=0;
}
}
}
}
bool canmove(int a[4][4],int visit[4][4],int row,int col)
{
if((row>=0&&row<4) && (col>=0&&col<4) && (visit[row][col]==0) &&(a[row][col]==1))
{
return true;
}
return false;
}
int main()
{
int a[4][4]={ {1,1,0,1},
{0,1,1,1},
{0,1,0,1},
{0,1,1,1} };
int visit[4][4]={
{0,0,0,0},
{0,0,0,0},
{0,0,0,0},
{0,0,0,0}};
visit[0][0]=1;
ratmaze(a,visit,0,0,3,3,1);
return 0;
}