package arrays;
public class Matrix_0_1_set_space {
public static void main(String[] args){
int a[][] = {{1,0,0,1},{0,0,1,0},{0,0,0,0}};
int rowsize = a.length;
int colsize = a[0].length;
int rowflg=0,colflg=0;
System.out.println(rowsize + ":" + colsize);
//To find whether first row needs to be set
for(int i=0;i<rowsize;i++) {
if(a[i][0]==1)
rowflg=1;
break;
}
//To find whether firsst column needs to be set
for(int j=0;j<colsize;j++){
if(a[0][j]==1)
colflg = 1;
break;
}
// Use 1st row n 1st column to chk
for(int i=1;i<rowsize;i++)
for(int j=1;j<colsize;j++)
if(a[i][j] == 1)
{
a[0][j]=1;
a[i][0]=1;
}
for(int i=1;i<rowsize;i++)
for(int j=1;j<colsize;j++)
if(a[0][j]==1 || a[i][0]==1)
a[i][j]=1;
if(rowflg==1){
for(int j=0;j<colsize;j++)
a[0][j]=1;
}
if(colflg==1){
for(int i=0;i<rowsize;i++)
a[i][0]=1;
}
for(int i=0;i<rowsize;i++){
System.out.println();
for(int j=0;j<colsize;j++)
System.out.print(a[i][j]);
}
}
}
No comments:
Post a Comment