fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone{
  9. public static void fill_vertical_zero(int[][] matrix, int y){
  10. for(int i = 0; i < matrix.length ; i++){
  11. matrix[i][y] = 0;
  12. }
  13. }
  14.  
  15. public static void fill_horizontal_zero(int[][] matrix, int x){
  16. for(int i = 0; i < matrix[0].length ; i++){
  17. matrix[x][i] = 0;
  18. }
  19. }
  20.  
  21. public static void zero_matrix(int[][] matrix){
  22. int n = matrix.length;
  23. int m = matrix[0].length;
  24.  
  25. boolean[] x = new boolean[matrix.length];
  26. boolean[] y = new boolean[matrix[0].length];
  27. Arrays.fill(x, false);
  28. Arrays.fill(y, false);
  29.  
  30. for(int i = 0; i < matrix.length ; i++){
  31. for(int j = 0; j < matrix[0].length; j++){
  32. if(matrix[i][j] == 0){
  33. x[i] = true;
  34. y[j] = true;
  35. }
  36. }
  37. }
  38. for(int i = 0; i < x.length; i++ ){
  39. if(x[i]){
  40. fill_horizontal_zero(matrix, i);
  41. }
  42. }
  43.  
  44. for(int i = 0; i < y.length; i++ ){
  45. if(y[i]){
  46. fill_vertical_zero(matrix, i);
  47. }
  48. }
  49.  
  50. }
  51.  
  52. public static void print_matrix(int[][] matrix){
  53. for (int i =0 ; i < matrix.length; i++){
  54. System.out.println(Arrays.toString(matrix[i]));
  55. }
  56. }
  57.  
  58. public static void main (String[] args) throws java.lang.Exception{
  59. int n = 7;
  60. int m = 12;
  61. int[][] matrix = new int[n][m];
  62. for (int i =0 ; i < n; i++){
  63. Arrays.fill(matrix[i], -1);
  64. }
  65. matrix[2][4] = 0;
  66. matrix[5][9] = 0;
  67. print_matrix(matrix);
  68. zero_matrix(matrix);
  69. System.out.println("after");
  70. print_matrix(matrix);
  71. }
  72. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, -1, -1]
[-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]
after
[-1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1]
[-1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1]
[-1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-1, -1, -1, -1, 0, -1, -1, -1, -1, 0, -1, -1]