fork(5) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. class MakeRowColumnAsOne {
  4.  
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9. int [][] mat = new int[][] { {1, 0, 0, 1},{0, 0, 1, 0},{0, 0, 0, 0}};
  10.  
  11. System.out.println("Input Matrix \n");
  12. printMatrix(mat);
  13.  
  14. modifyMatrix(mat);
  15.  
  16. System.out.println("Matrix after modification \n");
  17. printMatrix(mat);
  18.  
  19.  
  20. }
  21.  
  22. public static void printMatrix(int[][] mat)
  23. {
  24. int i, j;
  25. for (i = 0; i < mat.length; i++)
  26. {
  27. for (j = 0; j < mat[i].length; j++)
  28. {
  29. System.out.print(mat[i][j]);
  30. }
  31. System.out.println("\n");
  32. }
  33. }
  34.  
  35.  
  36. public static void modifyMatrix(int[][] mat) {
  37. int i, j;
  38. boolean paintRow1=false, paintCol1=false;
  39. i = 0;j=0;
  40. while(j<mat[0].length && !paintRow1){
  41. if(mat[0][j]==1){
  42. paintRow1=true;
  43. }
  44. j++;
  45. }
  46. i = 0;j=0;
  47. while(i<mat.length && !paintCol1){
  48. if(mat[i][0]==1){
  49. paintCol1=true;
  50. }
  51. i++;
  52. }
  53.  
  54. for (i = 1; i < mat.length; i++)
  55. for (j = 1; j < mat[i].length; j++)
  56. {
  57. if(mat[i][j]==1){
  58. mat[i][0]=1;
  59. mat[0][j]=1;
  60. }
  61. }
  62. for (i = 1; i < mat.length; i++)
  63. for (j = 1; j < mat[i].length; j++)
  64. {
  65. if(mat[0][j]==1 || mat[i][0]==1){
  66. mat[i][j]=1;
  67. }
  68. }
  69. if(paintCol1 ){
  70. for(i=0;i<mat.length;i++) mat[i][0]=1;
  71. }
  72. if(paintRow1){
  73. for(j=0;j<mat[0].length;j++) mat[0][j]=1;
  74. }
  75. }
  76. }
  77.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Input Matrix 

1001

0010

0000

Matrix after modification 

1111

1111

1011