Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. // case de la matrice
  8. class Cell {
  9.     int x;
  10.     int y;
  11.    
  12.     public Cell(int x, int y) {
  13.         this.x = x;
  14.         this.y = y;
  15.     }
  16.    
  17.     public Iterable<Cell> getNeighbors() {
  18.         List<Cell> res = new LinkedList<>();
  19.        
  20.         for (int i = -1; i <= 1; i++) {
  21.             for (int j = -1; j <= 1; j++) {
  22.                 res.add(new Cell(i,j));
  23.             }
  24.         }
  25.        
  26.         return res;
  27.     }
  28. }
  29. /* Name of the class has to be "Main" only if the class is public. */
  30. class Ideone
  31. {
  32.    
  33.    
  34.     public static void main (String[] args) throws java.lang.Exception
  35.     {
  36.         List<Cell> listIn = new LinkedList<>(); // données
  37.         List<Cell> listOut = new LinkedList<>(); // résultat
  38.         Map<Integer, Map<Integer, Integer>> map = new LinkedHashMap<>(); // intermediaire
  39.        
  40.         // parcours de la liste pour remplir la hashmap
  41.         for (Cell cell : listIn) {
  42.             for (Cell c : cell.getNeighbors()) {
  43.                 // initialisation map secondaire si besoin
  44.                 if (map.get(c.x) == null) {
  45.                     map.put(c.x, new LinkedHashMap<>());
  46.                 }
  47.                 // initialisation int si besoin
  48.                 if (map.get(c.x).get(c.y) == null) {
  49.                     map.get(c.x).put(c.y, new Integer(0));
  50.                 }
  51.                 // incrémentation voisin
  52.                 map.get(c.x).put(c.y, map.get(c.x).get(c.y) + 1);
  53.             }
  54.         }
  55.        
  56.         // parcours hashmap pour remplir liste résultat
  57.         for (Map.Entry<Integer, Map<Integer, Integer>> entry : map.entrySet()) {
  58.             for (Map.Entry<Integer, Integer> subEntry : entry.getValue().entrySet()) {
  59.                 if (subEntry.getValue() == 3) { //alive ?
  60.                     listOut.add(new Cell(entry.getKey(), subEntry.getKey()));
  61.                 }
  62.             }
  63.         }
  64.        
  65.         // listout contient les cellules vivantes à n+1
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement