fork(2) download
  1. import java.util.Random;
  2.  
  3. class Ideone
  4. {
  5. static final int width = 20, height = 10;
  6. static final int rooms = 30;
  7.  
  8. static boolean[][] room = new boolean[width][height];
  9.  
  10. static int neighborCount(int x, int y) {
  11. int n = 0;
  12. if (x > 0 && room[x-1][y]) n++;
  13. if (y > 0 && room[x][y-1]) n++;
  14. if (x < width-1 && room[x+1][y]) n++;
  15. if (y < height-1 && room[x][y+1]) n++;
  16. return n;
  17. }
  18.  
  19. public static void main (String[] args)
  20. {
  21. room[width/2][height/2] = true;
  22. Random r = new Random();
  23. int x, y, nc;
  24. for (int i = 0; i < rooms; i++) {
  25. while (true) {
  26. x = r.nextInt(width);
  27. y = r.nextInt(height);
  28. nc = neighborCount(x, y);
  29. if (!room[x][y] && nc == 1) break;
  30. }
  31. room[x][y] = true;
  32. }
  33. for (y = 0; y < height; y++) {
  34. for (x = 0; x < width; x++)
  35. System.out.print(room[x][y] ? "[]" : " ");
  36. System.out.println();
  37. }
  38. }
  39. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
                                        
                          []            
                  []  [][][]            
            [][][][]  []                
                  []  []                
            [][][][][][][]              
                  []  []  []            
                  []  [][][][][]        
                        []              
                      [][]