gitweb on Svarog

projekti pod git sistemom za održavanje verzija -- projects under the git version control system
Doterivanja objedinjenog resenja za lavirint
authorDoni Pracner <quinnuendo@gmail.com>
Sun, 4 Dec 2016 19:09:25 +0000 (20:09 +0100)
committerDoni Pracner <quinnuendo@gmail.com>
Sun, 4 Dec 2016 19:09:25 +0000 (20:09 +0100)
PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Lavirint.java
PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Mapa.java
PretrazivanjeSaVracanjem/Lavirint/ObjedinjenoResenje/Resenje.java

index 4fd8f794b24429c4499e122453934e45c3361bd2..312fa9cbb76681893efe05193a129678172eaaef 100644 (file)
@@ -34,23 +34,22 @@ public class Lavirint {
                }\r
                if (m.getMat(x, y) == Mapa.IZLAZ) {\r
                        return true;\r
-               } else {\r
-                       m.setPos(x, y, true);\r
-                       if (postojiPut(x + 1, y)) {\r
-                               return true;\r
-                       }\r
-                       if (postojiPut(x - 1, y)) {\r
-                               return true;\r
-                       }\r
-                       if (postojiPut(x, y + 1)) {\r
-                               return true;\r
-                       }\r
-                       if (postojiPut(x, y - 1)) {\r
-                               return true;\r
-                       }\r
-                       m.setPos(x, y, false);\r
-                       return false;\r
                }\r
+               m.setPos(x, y, true);\r
+               if (postojiPut(x + 1, y)) {\r
+                       return true;\r
+               }\r
+               if (postojiPut(x - 1, y)) {\r
+                       return true;\r
+               }\r
+               if (postojiPut(x, y + 1)) {\r
+                       return true;\r
+               }\r
+               if (postojiPut(x, y - 1)) {\r
+                       return true;\r
+               }\r
+               m.setPos(x, y, false);\r
+               return false;\r
        }\r
 \r
        // Poziva metodu rput da pronadje i ispise put, ako postoji\r
@@ -78,16 +77,15 @@ public class Lavirint {
                if (m.getMat(x, y) == Mapa.IZLAZ) {\r
                        System.out.println(x + " " + y);\r
                        return true;\r
-               } else {\r
-                       m.setPos(x, y, true);\r
-                       if (rput(x + 1, y) || rput(x, y + 1) || rput(x, y - 1)\r
-                                       || rput(x - 1, y)) {\r
-                               System.out.println(x + " " + y);\r
-                               return true;\r
-                       }\r
-                       m.setPos(x, y, false);\r
-                       return false;\r
                }\r
+               m.setPos(x, y, true);\r
+               if (rput(x + 1, y) || rput(x, y + 1) || rput(x, y - 1)\r
+                               || rput(x - 1, y)) {\r
+                       System.out.println(x + " " + y);\r
+                       return true;\r
+               }\r
+               m.setPos(x, y, false);\r
+               return false;\r
        }\r
 \r
        // Kreira optimalno resenje za put, pri cemu se za optimalnost resenja\r
@@ -125,19 +123,21 @@ public class Lavirint {
                if (m.getMat(x, y) == Mapa.IZLAZ) {\r
                        r.dodaj(x, y, 0);\r
                        if (optResenje == null || c.compare(r, optResenje) < 0) {\r
-                               optResenje = r.clone();\r
+                               optResenje = r.kopija();\r
                        }\r
                        r.izbaciKraj();\r
-               } else {\r
-                       m.setPos(x, y, true);\r
-                       r.dodaj(x, y, m.getMat(x, y));\r
-                       optPut(x + 1, y, r, c);\r
-                       optPut(x, y + 1, r, c);\r
-                       optPut(x, y - 1, r, c);\r
-                       optPut(x - 1, y, r, c);\r
-                       m.setPos(x, y, false);\r
-                       r.izbaciKraj();\r
+                       return;\r
                }\r
+               \r
+               // pokusavamo da trazimo dalje put\r
+               m.setPos(x, y, true);\r
+               r.dodaj(x, y, m.getMat(x, y));\r
+               optPut(x + 1, y, r, c);\r
+               optPut(x, y + 1, r, c);\r
+               optPut(x, y - 1, r, c);\r
+               optPut(x - 1, y, r, c);\r
+               m.setPos(x, y, false);\r
+               r.izbaciKraj();\r
        }\r
 }\r
 \r
index 246191bfbcf4f56b939780edbac444b03854e184..243ca682a115eb0555e52211fe59e4850ec7244d 100644 (file)
@@ -44,24 +44,31 @@ public class Mapa {
                pos = new boolean[sirina][visina];\r
        }\r
 \r
-       public static Mapa ucitajIzFajla(String imeFajla) {\r
+       public Mapa(String imeFajla) {\r
                if (!Svetovid.testIn(imeFajla)) {\r
-                       return null;\r
+                       throw new RuntimeException("Fajl za kreiranje mape ("\r
+                               + imeFajla + ") nije prisupacan");\r
                }\r
 \r
-               int sirina = Svetovid.in(imeFajla).readInt();\r
-               int visina = Svetovid.in(imeFajla).readInt();\r
-               if (sirina >= 0 && visina >= 0) {\r
-                       Mapa res = new Mapa(sirina, visina);\r
-                       for (int j = 0; j < visina; j++)\r
-                               for (int i = 0; i < sirina; i++)\r
-                                       res.mat[i][j] = Svetovid.in(imeFajla).readInt();\r
-                       Svetovid.closeIn(imeFajla);\r
-                       return res;\r
-               } else {\r
-                       Svetovid.closeIn(imeFajla);\r
+               sirina = Svetovid.in(imeFajla).readInt();\r
+               visina = Svetovid.in(imeFajla).readInt();\r
+               mat = new int[sirina][visina];\r
+               pos = new boolean[sirina][visina];\r
+               for (int j = 0; j < visina; j++) {\r
+                       for (int i = 0; i < sirina; i++) {\r
+                               mat[i][j] = Svetovid.in(imeFajla).readInt();\r
+                       }\r
+               }\r
+               Svetovid.closeIn(imeFajla);\r
+       }\r
+\r
+       public static Mapa ucitajIzFajla(String imeFajla) {\r
+               if (!Svetovid.testIn(imeFajla)) {\r
                        return null;\r
                }\r
+               \r
+               return new Mapa(imeFajla);\r
+\r
        }\r
 \r
        public void stampaj() {\r
@@ -75,4 +82,8 @@ public class Mapa {
                        }\r
                }\r
        }\r
+       \r
+       public String toString() {\r
+               return "Mapa velicine " + sirina + " x " + visina;\r
+       }\r
 }\r
index 5732427a158e1dfdd9afcd2de5a6b12ea6c6eb26..ffb2622692d8a7ebe91b7eac5661e8accc9ebaeb 100644 (file)
@@ -11,14 +11,26 @@ import java.util.ArrayList;
 import java.util.List;\r
 import java.util.Collections;\r
 \r
-public class Resenje implements Cloneable {\r
+public class Resenje {\r
        private ArrayList<Polje> polja;\r
        private List<Polje> nepromenljivaListaPolja;\r
 \r
-       Resenje() {\r
+       public Resenje() {\r
                polja = new ArrayList<Polje>();\r
                nepromenljivaListaPolja = Collections.unmodifiableList(polja);\r
        }\r
+       \r
+       /**\r
+               Pravi novo resenje sa istim sadrzajem kao original\r
+        */\r
+       public Resenje(Resenje original) {\r
+               // pozovemo "podrazumevani" konstruktor\r
+               this();\r
+               // iskopiramo sva polja iz originala\r
+               for (Polje p : original.getPolja()) {\r
+                       dodaj(p.getX(), p.getY(), p.getV());\r
+               }\r
+       }\r
 \r
        // Dodaje pulje u resenje\r
        public void dodaj(int x, int y, int v) {\r
@@ -40,6 +52,20 @@ public class Resenje implements Cloneable {
                for (int i = 0; i < getLength(); i++)\r
                        System.out.println(polja.get(i));\r
        }\r
+       \r
+       public String toString() {\r
+               StringBuilder sb = new StringBuilder();\r
+               sb.append("Resenje: [ ");\r
+               if (getLength()>0) {\r
+                       sb.append(polja.get(0));\r
+                       for (int i = 1; i < getLength(); i++) {\r
+                               sb.append(", " + polja.get(i));\r
+                       }\r
+               }\r
+               sb.append(" ]");\r
+               \r
+               return sb.toString();\r
+       }\r
 \r
        // Vraca duzinu resenja\r
        public int getLength() {\r
@@ -58,9 +84,8 @@ public class Resenje implements Cloneable {
                return nepromenljivaListaPolja;\r
        }\r
 \r
-       // Kreira klon od resenja\r
-       @Override\r
-       public Resenje clone() {\r
+       // Kreira nezavisnu kopiju ovog resenja\r
+       public Resenje kopija() {\r
                Resenje rez = new Resenje();\r
                for (Polje p : polja) {\r
                        rez.dodaj(p.getX(), p.getY(), p.getV());\r
Svarog.pmf.uns.ac.rs/gitweb maintanance Doni Pracner