X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?a=blobdiff_plain;f=PretrazivanjeSaVracanjem%2FLavirint%2FObjedinjenoResenje%2FLavirint.java;h=c4c19809de2272f8305d26dcc19f817fbab5311e;hb=a6cda178fb5053f3854f2866dbe4967b3e106606;hp=4fd8f794b24429c4499e122453934e45c3361bd2;hpb=078fa0a015841d5b6ff25789a2cdc4b56b7f8a5a;p=spa2-materijali.git diff --git a/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java b/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java index 4fd8f79..c4c1980 100644 --- a/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java +++ b/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java @@ -34,23 +34,22 @@ public class Lavirint { } if (m.getMat(x, y) == Mapa.IZLAZ) { return true; - } else { - m.setPos(x, y, true); - if (postojiPut(x + 1, y)) { - return true; - } - if (postojiPut(x - 1, y)) { - return true; - } - if (postojiPut(x, y + 1)) { - return true; - } - if (postojiPut(x, y - 1)) { - return true; - } - m.setPos(x, y, false); - return false; } + m.setPos(x, y, true); + if (postojiPut(x + 1, y)) { + return true; + } + if (postojiPut(x - 1, y)) { + return true; + } + if (postojiPut(x, y + 1)) { + return true; + } + if (postojiPut(x, y - 1)) { + return true; + } + m.setPos(x, y, false); + return false; } // Poziva metodu rput da pronadje i ispise put, ako postoji @@ -78,16 +77,15 @@ public class Lavirint { if (m.getMat(x, y) == Mapa.IZLAZ) { System.out.println(x + " " + y); return true; - } else { - m.setPos(x, y, true); - if (rput(x + 1, y) || rput(x, y + 1) || rput(x, y - 1) - || rput(x - 1, y)) { - System.out.println(x + " " + y); - return true; - } - m.setPos(x, y, false); - return false; } + m.setPos(x, y, true); + if (rput(x + 1, y) || rput(x, y + 1) || rput(x, y - 1) + || rput(x - 1, y)) { + System.out.println(x + " " + y); + return true; + } + m.setPos(x, y, false); + return false; } // Kreira optimalno resenje za put, pri cemu se za optimalnost resenja @@ -125,19 +123,21 @@ public class Lavirint { if (m.getMat(x, y) == Mapa.IZLAZ) { r.dodaj(x, y, 0); if (optResenje == null || c.compare(r, optResenje) < 0) { - optResenje = r.clone(); + optResenje = new Resenje(r); } r.izbaciKraj(); - } else { - m.setPos(x, y, true); - r.dodaj(x, y, m.getMat(x, y)); - optPut(x + 1, y, r, c); - optPut(x, y + 1, r, c); - optPut(x, y - 1, r, c); - optPut(x - 1, y, r, c); - m.setPos(x, y, false); - r.izbaciKraj(); + return; } + + // pokusavamo da trazimo dalje put + m.setPos(x, y, true); + r.dodaj(x, y, m.getMat(x, y)); + optPut(x + 1, y, r, c); + optPut(x, y + 1, r, c); + optPut(x, y - 1, r, c); + optPut(x - 1, y, r, c); + m.setPos(x, y, false); + r.izbaciKraj(); } }