fork(3) download
  1. class Box {
  2. int height;
  3. int width;
  4. int[][] a;
  5.  
  6. public Box(int height) {
  7. this.height = height;
  8. this.width = height;
  9. a = new int[height][width];
  10. for (int i = 0; i < height; i++)
  11. for (int j = 0; j < width; j++)
  12. a[i][j] = 0;
  13. }
  14.  
  15. public void printBox() {
  16. for (int i = 0; i < height; i++) {
  17. for (int j = 0; j < width; j++)
  18. System.out.print(a[i][j]);
  19. System.out.println();
  20. }
  21. System.out.println();
  22. }
  23.  
  24. public void plus(int[] i, int[] j) {
  25. for (int n = 0; n < i.length; n++)
  26. plus(i[n], j[n]);
  27. }
  28.  
  29. public void plus(int i, int j) {
  30. if (inBounds(i) && inBounds(j))
  31. a[i][j] = (a[i][j] + 1) % 2;
  32. if (inBounds(i - 1) && inBounds(j))
  33. a[i - 1][j] = (a[i - 1][j] + 1) % 2;
  34. if (inBounds(i) && inBounds(j - 1))
  35. a[i][j - 1] = (a[i][j - 1] + 1) % 2;
  36. if (inBounds(i + 1) && inBounds(j))
  37. a[i + 1][j] = (a[i + 1][j] + 1) % 2;
  38. if (inBounds(i) && inBounds(j + 1))
  39. a[i][j + 1] = (a[i][j + 1] + 1) % 2;
  40. }
  41.  
  42. public boolean inBounds(int n) {
  43. return n >= 0 && n < height;
  44. }
  45. }
  46. public class Main
  47. {
  48. public static void main(String[] args) {
  49. Box b = new Box(5);
  50. b.printBox();
  51. int[] i = { 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4 };
  52. int[] j = { 0, 2, 4, 1, 3, 0, 4, 1, 3, 0, 2, 4 };
  53. b.plus(i, j);
  54. b.printBox();
  55. }
  56. }
Success #stdin #stdout 0.08s 380160KB
stdin
Standard input is empty
stdout
00000
00000
00000
00000
00000

11111
11111
11011
11111
11111