Advertisement
Guest User

Untitled

a guest
Jul 7th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 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.                 if (j == 0) {
  23.                     if (i == 0) {
  24.                         continue;
  25.                     }
  26.                 }
  27.                 res.add(new Cell(i,j));
  28.             }
  29.         }
  30.                
  31.         return res;
  32.     }
  33. }
  34. /* Name of the class has to be "Main" only if the class is public. */
  35. class Ideone
  36. {
  37.    
  38.    
  39.     public static void main (String[] args) throws java.lang.Exception
  40.     {
  41.         List<Cell> listIn = new LinkedList<>(); // données
  42.         List<Cell> listOut = new LinkedList<>(); // résultat
  43.         Map<Integer, Map<Integer, Integer>> map = new LinkedHashMap<>(); // intermediaire
  44.        
  45.         // parcours de la liste pour remplir la hashmap
  46.         for (Cell cell : listIn) {
  47.             for (Cell c : cell.getNeighbors()) {
  48.                 // initialisation map secondaire si besoin
  49.                 if (map.get(c.x) == null) {
  50.                     map.put(c.x, new LinkedHashMap<>());
  51.                 }
  52.                 // initialisation int si besoin
  53.                 if (map.get(c.x).get(c.y) == null) {
  54.                     map.get(c.x).put(c.y, new Integer(0));
  55.                 }
  56.                 // incrémentation voisin
  57.                 map.get(c.x).put(c.y, map.get(c.x).get(c.y) + 1);
  58.             }
  59.         }
  60.        
  61.         // parcours hashmap pour remplir liste résultat
  62.         for (Map.Entry<Integer, Map<Integer, Integer>> entry : map.entrySet()) {
  63.             for (Map.Entry<Integer, Integer> subEntry : entry.getValue().entrySet()) {
  64.                 if (subEntry.getValue() == 3) { //alive ?
  65.                     listOut.add(new Cell(entry.getKey(), subEntry.getKey()));
  66.                 }
  67.             }
  68.         }
  69.        
  70.         // listOut contient les cellules vivantes à n+1
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement