Advertisement
niske

NajboljiPut

Nov 27th, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3.  
  4. public class NajboljiPut {
  5.  
  6. public static int s;
  7. public static int v;
  8. public static boolean[][] posecenost;
  9. public static int[][] mapa;
  10. public static final int polje = 0;
  11. public static final int zid = -11;
  12. public static final int cilj = -99;
  13. public static Put najboljiPut;
  14. public static Put test = new Put();
  15. public static ArrayList<Put> resenja = new ArrayList<Put>();
  16. public static Comparator<Put> c;
  17.  
  18. public static void ucitajMapu(String imef) {
  19. if(Svetovid.testIn(imef)) {
  20. s = Svetovid.in(imef).readInt();
  21. v = Svetovid.in(imef).readInt();
  22. mapa = new int[v][s];
  23. posecenost = new boolean[v][s];
  24. for(int i = 0; i < v; i++) {
  25. for(int j = 0; j < s; j++) {
  26. mapa[i][j] = Svetovid.in(imef).readInt();
  27. }
  28. }
  29. }
  30. }
  31.  
  32. public static void ispisiMapu() {
  33. for(int i = 0; i < v; i++) {
  34. for(int j = 0; j < s; j++) {
  35. System.out.println(mapa[i][j] + " ");
  36. }
  37. System.out.println();
  38. }
  39. }
  40.  
  41. public static void nadjiPut(int i, int j) {
  42. if(i >= v || i < 0) {
  43. return ;
  44. }
  45. if(j >= s || j < 0) {
  46. return ;
  47. }
  48. if(mapa[i][j] == zid) {
  49. return ;
  50. }
  51. if(posecenost[i][j]) {
  52. return ;
  53. }
  54. if(mapa[i][j] == cilj) {
  55. Put kopija = test.kopiraj();
  56. kopija.dodajCvor(i, j, mapa[i][j]);
  57. resenja.add(kopija);
  58. if(najboljiPut == null || c.compare(test, najboljiPut) < 0) {
  59. najboljiPut = kopija;
  60. }
  61. return ;
  62. }
  63.  
  64. posecenost[i][j] = true;
  65. test.dodajCvor(i, j, mapa[i][j]);
  66.  
  67. nadjiPut(i-1, j);
  68. nadjiPut(i+1, j);
  69. nadjiPut(i, j+1);
  70. nadjiPut(i, j-1);
  71.  
  72. test.izbrisiCvor();
  73.  
  74. posecenost[i][j] = false;
  75. }
  76.  
  77. public static void main(String[] args) {
  78. String imef = Svetovid.in.readLine("Unesite ime fajla: ");
  79. ucitajMapu(imef);
  80.  
  81. int x;
  82. int y;
  83. do {
  84. x = Svetovid.in.readInt("Unesite x koordinatu: ");
  85. } while(x < 0 || x > v);
  86. do {
  87. y = Svetovid.in.readInt("Unesite y koordinatu: ");
  88. } while(y < 0 || y > s);
  89.  
  90. int i = Svetovid.in.readInt("Unesite kakav put zelite da nadjete: ");
  91.  
  92. switch(i) {
  93. case 1:
  94. c = Comparator.naturalOrder();
  95. break;
  96. case 2:
  97. c = new KomparatorPoVrednosti();
  98. break;
  99. case 3:
  100. c = new KomparatorPoNajvecemBlaguPojedinacno();
  101. break;
  102. case 4:
  103. c = new KomparatorPoBrojuBlaga();
  104. case 5:
  105. c = new KomparatorPoUzastopnomBlagu();
  106. }
  107. nadjiPut(x, y);
  108.  
  109. if(najboljiPut != null) {
  110. System.out.println("Put postoji.");
  111. najboljiPut.ispisiPut();
  112. int br = 0;
  113. for(int k = 0; k < resenja.size(); k++) {
  114. if(resenja.get(k).uzastopnoBlagoMax >= 3) {
  115. br++;
  116. }
  117. }
  118. System.out.println("Broj resenja sa vise od tri uzastopna blaga je " + br);
  119. }
  120. else {
  121. System.out.println("Put ne postoji.");
  122. }
  123. }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement