Advertisement
Guest User

Untitled

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