X-Git-Url: http://svarog.pmf.uns.ac.rs/gitweb/?p=spa2-materijali.git;a=blobdiff_plain;f=PretrazivanjeSaVracanjem%2FLavirint%2FObjedinjenoResenje%2FLavirint.java;h=312fa9cbb76681893efe05193a129678172eaaef;hp=4fd8f794b24429c4499e122453934e45c3361bd2;hb=3c24990b2b5db35d728a4a706ba4963d3c936a76;hpb=e9a069bea132ac74682a92ba56dad7f813992032 diff --git a/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java b/PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java index 4fd8f79..312fa9c 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 = r.kopija(); } 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(); } }