Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.lang.*;
- import java.io.*;
- class Main
- {
- // case de la matrice
- class Cell {
- int x;
- int y;
- public Iterable<Cell> getNeighbors() {
- List<Cell> res = new LinkedList<>();
- for (int i = -1; i <= 1; i++) {
- for (int j = -1; j <= 1; j++) {
- res.put(new Cell(i,j));
- }
- }
- }
- }
- public static void main (String[] args) throws java.lang.Exception
- {
- List<Cell> listIn = new LinkedList<>(); // données
- List<Cell> listOut = new LinkedList<>(); // résultat
- Map<int, Map<int, Integer>> map = new LinkedHashMap<>(); // intermediaire
- // parcours de la liste pour remplir la hashmap
- for (Cell cell : listIn) {
- for (Cell c : cell.getNeighbors()) {
- // initialisation map secondaire si besoin
- if (map.get(c.x) == null) {
- map.put(c.x, new LinkedHashMap<>());
- }
- // initialisation int si besoin
- if (map.get(c.x).get(c.y) == null) {
- map.get(c.x).put(c.y, new Integer(0));
- }
- // incrémentation voisin
- map.get(c.x).put(c.y, map.get(c.x).get(c.y) + 1);
- }
- }
- // parcours hashmap pour remplir liste résultat
- for (Map.Entry<int, int> entry : map.entrySet()) {
- for (Map.Entry<int, Integer> subEntry : entries.entrySet()) {
- if (subEntry.getValue() == 3) { //alive ?
- listOut.add(new Cell(entry.getKey(), subEntry.getKey()));
- }
- }
- }
- // listout contient les cellules vivantes à n+1
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement