fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. interface Callback {
  5. void visit(int[] p); // n-dimensional point
  6. }
  7.  
  8. public class Main
  9. {
  10.  
  11. // bounds[] - each number the limits iteration on i'th axis from 0 to bounds[i]
  12. // current - current dimension
  13. // callback - point
  14. public static void visit(int[] bounds, int currentDimension, int[] p, Callback c) {
  15. //c.visit(p);
  16. //if (currentDimension == p.length) return; // stop recursion
  17. for (int i = 0; i < bounds[currentDimension]; i++) {
  18. p[currentDimension] = i;
  19. if (currentDimension == p.length - 1) c.visit(p);
  20. else visit(bounds, currentDimension + 1, p, c);
  21. }
  22. }
  23.  
  24. static long visits = 0;
  25. /// now visiting
  26. public static void main(String[] args) {
  27. long start = System.currentTimeMillis();
  28. visit(new int[] {50, 50, 50, 50}, 0, new int[4], new Callback() {
  29. public void visit(int[] p) {
  30. //System.out.println(Arrays.toString(p));
  31. visits++;
  32. }
  33. });
  34. long end = System.currentTimeMillis();
  35. System.out.println(visits + " visits");
  36. System.out.println("Time: " + (end - start) + " ms");
  37. }
  38.  
  39. }
Success #stdin #stdout 0.07s 245632KB
stdin
Standard input is empty
stdout
6250000 visits
Time: 45 ms