final commit, project finished
[co1314.git] / RandomSolver.java
1 import java.util.ArrayList;
2 import java.util.Collections;
3 import java.util.Random;
4
5 /* Generate a random solution which not is necessarily optimal. */
6 public class RandomSolver implements IPetproblemSolver {
7 @Override
8 public int[] solve(int n, int m, int[][] compatibility) {
9 int[] result = new int[n];
10 ArrayList<Integer> possibilities = new ArrayList<Integer>();
11
12 // Init list of possible pets
13 for(int m_i = 0; m_i < m; m_i++)
14 possibilities.add(m_i);
15
16 // If number of children is bigger then number of pets
17 if(n > m)
18 for(int i = 0; i < n - m; i++)
19 possibilities.add(-1); // Add possibility of drawing "no pet"
20
21 // Shuffle the available options
22 Collections.shuffle(possibilities, new Random());
23
24 // Assign a possible pet to all children
25 for(int n_i = 0; n_i < n; n_i++)
26 result[n_i] = possibilities.get(n_i);
27
28 return result;
29 }
30 }