// Input a 2-dimensional matrix of size 5x5 from the user and print the number of // 0-regions in the matrix. Also for each region, print the coordinates of any one // cell inside it. Assume that the user inputs only 0's and 1's. Also assume that // the top-left cell is (0,0). // Author:rahule@cse.iitk.ac.in #include int a[5][5]; //the matrix declared globally.. void mark(int,int); main() { int i,j; printf("\nenter the matrix\n"); for(i=0;i<5;i++) { printf("\n"); for(j=0;j<5;j++) { printf("(%d,%d) :",i,j); scanf("%d",&a[i][j]); printf(" "); } } printf("\nthe 0 regions in the matrix are.\n"); for(i=0;i<5;i++) for(j=0;j<5;j++) { if(a[i][j]==0) //if (i,j) is 0,mark all o cells reachable from it as -1 { printf("(%d,%d), ",i,j); mark(i,j); } } printf("\n"); } void mark(int i,int j) //mark all the cells with 0 reachable from (i,j) as -1. { a[i][j]=-1; //mark (i,j) as -1 if(j<4 && a[i][j+1]==0) //if LHS cell of (i,j) is also 0,call the mark funtion recursively for it mark(i,j+1); if(i<4 && a[i+1][j]==0) //if the cell bellow (i,j) is also 0,call the mark funtion recursively for it mark(i+1,j); if(j>0 && a[i][j-1]==0) //if the RHS cell of (i,j) is also 0,call the mark funtion recursively for it mark(i,j-1); if(i>0 && a[i-1][j]==0) //if the cell above (i,j) is also 0,call the mark funtion recursively for it mark(i,j+1); }