Last active
August 29, 2015 14:08
-
-
Save anuradhawick/7c2c1f93479f65c3249e to your computer and use it in GitHub Desktop.
Program to clear an object(shape) by recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int spc[5][5]={{1,0,0,0,0},{1,1,0,0,0},{0,1,1,0,0},{0,0,0,0,0},{0,0,1,1,1}}; | |
int rs=5,cs=5; | |
int main(){ | |
int i; | |
int rc[2]; | |
printArr(spc,rs,cs); | |
printf("Enter the row number and column number\n"); | |
for(i=0;i<2;i++){ | |
scanf("%d",&rc[i]); | |
} | |
clear(rc[0],rc[1]); | |
int j; | |
for(i=0;i<rs;i++){ | |
for(j=0;j<cs;j++){ | |
printf("%5d",spc[i][j]); | |
} | |
printf("\n\n"); | |
} | |
return 1501; | |
} | |
void clear(int r,int c){ | |
if (spc[r][c]==1){ | |
spc[r][c] = 0; | |
if(r>=0 && r<rs){ | |
if(c>=0 && c<cs){ | |
clear(r,c-1); | |
clear(r,c+1); | |
clear(r-1,c); | |
clear(r+1,c); | |
} | |
} | |
} | |
} | |
void printArr(int A[rs][cs],int r,int c){ | |
int i,j; | |
for(i=0;i<r;i++){ | |
for(j=0;j<c;j++){ | |
printf("%4d",A[i][j]); | |
} | |
printf("\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is recursive and can be used for cell simulations, path finders, blocked cells, blocked roots and so on recursively