fork download
  1. import java.util.*;
  2.  
  3. class Main
  4. {
  5. public static void life(int a[][], int b[][], int m, int n) {
  6. for (int i = 0; i < m; i++) {
  7. for (int j = 0; j < n; j++) {
  8. int sum = 0;
  9. for (int k = -1; k <= 1; k++) {
  10. int I = i + k;
  11. if (I == -1) { I = m - 1; } else if (I == m) { I = 0; }
  12. for (int l = -1; l <= 1; l++) {
  13. int J = j + l;
  14. if (J == -1) { J = n - 1; } else if (J == n) { J = 0; }
  15. sum += a[I][J];
  16. }
  17. }
  18. if (sum == 3 || (sum == 4 && a[i][j] == 1)) {
  19. b[i][j] = 1;
  20. } else {
  21. b[i][j] = 0;
  22. }
  23. }
  24. }
  25. }
  26. public static void main(String args[])
  27. {
  28. int size = 500;
  29. int a[][], b[][];
  30. a = new int[size][size];
  31. b = new int[size][size];
  32. Random r = new Random();
  33. for (int i = 0; i < size; i++) {
  34. for (int j = 0; j < size; j++) {
  35. a[i][j] = r.nextInt(2);
  36. }
  37. }
  38. for (int n = 0; n < 50; n++) {
  39. life(a, b, size, size);
  40. int tmp[][];
  41. tmp = a; a = b; b = tmp;
  42. }
  43. /*
  44.   for (int[] x : a) {
  45.   for (int n : x) {
  46.   System.out.printf("%d,", n);
  47.   }
  48.   System.out.println("");
  49.   }*/
  50. }
  51. }
Success #stdin #stdout 0.95s 245824KB
stdin
Standard input is empty
stdout
Standard output is empty