Advertisement
madegoff

blatt5_algodat

Jun 3rd, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. import org.junit.Test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Iterator;
  6. import java.util.LinkedList;
  7. import java.util.stream.Collector;
  8. import java.util.stream.Collectors;
  9.  
  10. /**
  11. * This class implements a game of Row of Bowls.
  12. * For the games rules see Blatt05. The goal is to find an optimal strategy.
  13. */
  14. public class RowOfBowls {
  15.  
  16. public RowOfBowls() {
  17. }
  18. int[] values;
  19. int[][] score_tab;
  20. /**
  21. * Implements an optimal game using dynamic programming
  22. * @param values array of the number of marbles in each bowl
  23. * @return number of game points that the first player gets, provided both parties play optimally
  24. */ //
  25. public int maxGain(int[] values)
  26. {
  27. // TODO
  28. // return maxGain_help(values, 0);
  29. //die Tabelle fuer die besten scores erstellt
  30. int n = values.length;
  31. int[][] score_tab = new int[n][n]; //schon mit nullen initialisiert?
  32. //int len; //laenge der betrachteten zahlenfolge (1 fuer 1 schuessel, 2 fuer zwei schuessel usw)
  33.  
  34. if (n==0) return 0; //keine schuessel
  35.  
  36. // Basisfälle: Nur eine Schüssel
  37. for (int k = 0; k < n; k++) {
  38. score_tab[k][k] = values[k];
  39. }
  40.  
  41. for (int len = 2; len <= n; len++) {
  42. for (int i = len-1; i < n; i++) {
  43. int j = i - len + 1;
  44. int pickLeft = (j >= 0 && j+1 < n) ? values[j] - score_tab[i][j+1] : 0;
  45. int pickRight = (i-1 >= 0 && j >= 0) ? values[i] - score_tab[i-1][j] : 0;
  46. score_tab[i][j] = Math.max(pickLeft, pickRight);
  47. }
  48. }
  49. this.score_tab = score_tab;
  50. this.values = values;
  51. return score_tab[n-1][0];
  52. }
  53.  
  54.  
  55.  
  56. /**
  57. * Implements an optimal game recursively.
  58. *
  59. * @param values array of the number of marbles in each bowl
  60. * @return number of game points that the first player gets, provided both parties play optimally
  61. */
  62. public int maxGainRecursive(int[] values) {
  63. if (values.length == 0) return 0;
  64. return maxGainRecursive(values, 0, values.length - 1);
  65. }
  66.  
  67. private int maxGainRecursive(int[] values, int left, int right) {
  68. if (right == left){
  69. return values[right];
  70. }
  71.  
  72. int res;
  73. int score_right;
  74. int score_left;
  75.  
  76. //linken teilbaum
  77. int pickLeft = values[left];
  78. score_left = pickLeft-maxGainRecursive(values, left+1, right);
  79. // fuer 7 4 3 2 wenn 7 nehmen: score = 7 - maxScore(4 3 2)
  80.  
  81. //rechten teilbaum
  82. int pickRight = values[right];
  83. score_right = pickRight-maxGainRecursive(values, left, right-1);
  84. // fuer 7 4 3 2 wenn 2 nehmen: score = 2 - maxScore(7 4 3)
  85.  
  86. res = Math.max(score_left, score_right);
  87. return res;
  88. }
  89.  
  90.  
  91. /**
  92. * Calculates an optimal sequence of bowls using the partial solutions found in maxGain(int values)
  93. * @return optimal sequence of chosen bowls (represented by the index in the values array)
  94. */
  95.  
  96. public Iterable<Integer> optimalSequence(){
  97. int n = values.length;
  98.  
  99. int best_score = maxGain(values); //speichert score_tab schonmal
  100. //dies brauchen wir nicht wirklich, aber moechten sicherstellen dass die Funktion die score_tab berechnet hat
  101. ArrayList<Integer> sequence = new ArrayList<>();
  102. int i = n - 1, j = 0;
  103. while (i >= 0 && j < n) {
  104. int pickLeft = (j >= 0 && j + 1 < n) ? values[j] - score_tab[i][j + 1] : Integer.MIN_VALUE;
  105. int pickRight = (i > 0 && j >= 0) ? values[i] - score_tab[i - 1][j] : Integer.MIN_VALUE;
  106. if (pickLeft > pickRight) {
  107. sequence.add(j);
  108. if (sequence.toArray().length == n) break;
  109. j++;
  110. } else {
  111. sequence.add(i);
  112. if (sequence.toArray().length == n) break;
  113. i--;
  114. }
  115.  
  116. }
  117.  
  118.  
  119. return sequence;
  120. }
  121.  
  122. public static void main(String[] args)
  123. {
  124. // For Testing
  125. RowOfBowls game = new RowOfBowls();
  126. int[] values = {7, 4, 3, 2};
  127. System.out.println("Max gain (recursive): " + game.maxGainRecursive(values));
  128. //game.testRowOfBowls();
  129. }
  130. }
  131.  
  132.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement