-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.c
More file actions
36 lines (32 loc) · 645 Bytes
/
Copy pathpath.c
File metadata and controls
36 lines (32 loc) · 645 Bytes
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
#include<stdio.h>
#include<stdbool.h>
int main()
{
int m = 2, n = 3;
int mat[2][3] = {{1,2,3}, {4,5,6}};
bool path[2][3] = {{false}};
printpath(mat,0,0,m,n,path); //from starting point (0,0)
}
int printpath(int mat[][3],int i,int j,int m, int n, bool path[][3]) //i=current row, j=current column
{
if(i<0 || i>m || j<0 || j>n || path[i][j])
return 0;
path[i][j] = true;
if(i==m-1 && j==n-1)
{
for(int i = 0; i<m; i++)
{
for(int j=0;j<n;j++)
{
if(path[i][j])
{
printf("%d ", mat[i][j]);
}
}
}
printf("\n");
}
printpath(mat,i,j+1,m,n,path);
printpath(mat,i+1,j,m,n,path);
path[i][j] = false;
}