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