Permutation.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package pathgame.algorithm;
  2. public class Permutation {
  3. private int size;
  4. private int[] vals;
  5. private int[] minVals;
  6. private int minCost = Integer.MAX_VALUE;
  7. private int thingsPerPos;
  8. private boolean overFlow = false;
  9. //public int tempCounter1 = 0;
  10. //public int tempCounter2 = 0;
  11. public Permutation(int listSize, int thingsPerPos) {
  12. this.thingsPerPos = thingsPerPos;
  13. this.size = (listSize/thingsPerPos) - 1;
  14. vals = new int[size];
  15. }
  16. public int getValAtPos (int pos) {
  17. return vals[pos];
  18. }
  19. public boolean isPosAtMax (int pos) {
  20. if(vals[pos] == getMaxOfPos(pos)) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. public int getMaxOfPos (int pos) {
  26. return thingsPerPos * (size - pos);
  27. }
  28. public int size() {
  29. return size;
  30. }
  31. public void increaseAtPos (int pos) {
  32. vals[pos]++;
  33. for(int i = pos+1; i < size; i++) {
  34. vals[i] = 0;
  35. }
  36. }
  37. public void increaseLowest() {
  38. boolean over = true;
  39. for(int i = size-1; i >= 0; i--) {
  40. if(getValAtPos(i) < getMaxOfPos(i)) {
  41. increaseAtPos(i);
  42. over = false;
  43. break;
  44. }
  45. }
  46. if(over) {
  47. overFlow = true;
  48. }
  49. }
  50. public int getMinCost() {
  51. return minCost;
  52. }
  53. public void setMinCost(int minCost) {
  54. this.minCost = minCost;
  55. minVals = vals.clone();
  56. }
  57. public void makePermutMinimal() {
  58. vals = minVals.clone();
  59. }
  60. public void printPermut() {
  61. for(int i = 0; i < size; i++) {
  62. System.out.print(vals[i] + " ");
  63. }
  64. System.out.println();
  65. }
  66. public boolean isOverFlowing() {
  67. return overFlow;
  68. }
  69. public void printMinPermut() {
  70. for(int i = 0; i < size; i++) {
  71. System.out.print(minVals[i] + " ");
  72. }
  73. System.out.println();
  74. }
  75. }