Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.51 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Arrays;
  3. import java.util.HashMap;
  4. import java.util.HashSet;
  5.  
  6. /**
  7.  * Created by Piotruś on 23.04.2017.
  8.  */
  9. public class Algorithms
  10. {
  11.     public static final int N = 3;
  12.     public static final int c = N % 2 == 0 ? 2 * N : 2 * N + 1;
  13.     HashMap<Pair, ArrayList<Integer>> domain;
  14.     public final ArrayList<Integer> colors;
  15.     int[][] matrix;
  16.  
  17.  
  18.     {
  19.         HashMap<Pair, ArrayList<Integer>> d = new HashMap<>();
  20.         colors = new ArrayList<>();
  21.         for (int i = 1; i < c + 1; i++)
  22.             colors.add(i);
  23.         for (int i = 0; i < N; i++)
  24.             for (int j = 0; j < N; j++)
  25.             {
  26.                 d.put(new Pair(i, j), new ArrayList<>(colors));
  27.             }
  28.         domain = d;
  29.     }
  30.  
  31.     Algorithms()
  32.     {
  33.         matrix = new int[N][N];
  34.     }
  35.  
  36.  
  37.     public int backtracking(int[][] matrix, Pair<Integer, Integer> position, int acc, HashSet<HashSet<Integer>> edges)
  38.     {
  39.         for (Integer color : colors)
  40.         {
  41.             int[][] copy = matrix.clone();
  42.             HashSet<HashSet<Integer>> edgesC = new HashSet<>(edges);
  43.             if (constraintsOk(copy, position, color, edgesC))//ograniczenia spełnione
  44.             {
  45.                 copy[position.getFirst()][position.getSecond()] = color;
  46.                 if (isFinished(copy, position))
  47.                 {
  48.                     acc+=1;
  49.                     continue;
  50.                 }
  51.                 acc = backtracking(copy, getNext(copy, position), acc, edgesC);
  52.             }
  53.         }
  54.         return acc;
  55.     }
  56.  
  57.     boolean isFinished(int[][] tab, Pair<Integer, Integer> position)
  58.     {
  59.         return position.getFirst() == tab.length - 1 && position.getSecond() == tab[1].length - 1;
  60.     }
  61.  
  62.     Pair<Integer, Integer> getNext(int[][] matrix, Pair<Integer, Integer> pos)
  63.     {
  64.         if (pos.getSecond() == matrix[0].length - 1)
  65.             return new Pair(pos.getFirst() + 1, 0);
  66.         else
  67.             return new Pair(pos.getFirst(), pos.getSecond() + 1);
  68.     }
  69.  
  70.     boolean constraintsOk(int[][] matrix, Pair<Integer, Integer> pos, int color ,HashSet<HashSet<Integer>> edges)
  71.     {
  72.         boolean flag = false;
  73.         if (pos.getFirst() == 0 && pos.getSecond() == 0)
  74.         {
  75.             flag = true;
  76.         }
  77.         else if (pos.getFirst() == 0)
  78.         {
  79.             if (matrix[0][pos.getSecond() - 1] != color && !edges.contains(new HashSet<>(Arrays.asList(color, matrix[0][pos.getSecond() - 1]))))
  80.             {
  81.                 flag = true;
  82.                 edges.add(new HashSet<>(Arrays.asList(color,matrix[0][pos.getSecond() - 1] )));
  83.             }
  84.             else
  85.                 flag = false;
  86.         } else if (pos.getSecond() == 0)
  87.         {
  88.             if (matrix[pos.getFirst() - 1][0] != color && !edges.contains(new HashSet<>(Arrays.asList(color, matrix[pos.getFirst() - 1][0]))))
  89.             {
  90.                 flag = true;
  91.                 edges.add(new HashSet<>(Arrays.asList(color,matrix[pos.getFirst()-1][0])));
  92.             }
  93.             else
  94.                 flag = false;
  95.         }
  96.         else if (matrix[pos.getFirst() - 1][pos.getSecond()] != color && matrix[pos.getFirst()][pos.getSecond() - 1] != color && !edges.contains(new HashSet<>(Arrays.asList(color, matrix[pos.getFirst() - 1][pos.getSecond()]))) && !edges.contains(new HashSet<>(Arrays.asList(color, matrix[pos.getFirst()][pos.getSecond() - 1]))))
  97.         {
  98.             flag = true;
  99.             edges.add(new HashSet<>(Arrays.asList(color,matrix[pos.getFirst()][pos.getSecond() - 1])));
  100.             edges.add(new HashSet<>(Arrays.asList(color,matrix[pos.getFirst() - 1][pos.getSecond()])));
  101.         }
  102.  
  103.         return flag;
  104.     }
  105.  
  106.  
  107.     /*public static void main(String args[])
  108.     {
  109.         HashSet<HashSet<Integer>> edges = new HashSet<>();
  110.         Algorithms alg = new Algorithms();
  111.         System.out.println(alg.backtracking(alg.matrix, new Pair(0, 0), 0, edges));
  112.     }*/
  113.  
  114.    /* public int forwardChecking(int[][] matrix, Pair<Integer, Integer> position, int acc, HashSet<HashSet<Integer>> edge)
  115.     {
  116.         for (Integer color : colors)
  117.         {
  118.             if (constraintsOk(matrix, position, color, edges))//ograniczenia spełnione
  119.             {
  120.                 matrix[position.getFirst()][position.getSecond()] = color;
  121.                 if (isFinished(matrix, position))
  122.                     return 1;
  123.                 acc += backtracking(matrix, getNextSmallestDomain(matrix, position), acc, edges);
  124.             }
  125.         }
  126.         return acc;
  127.     }*/
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement