Added IO (maze construction) #1
No files matched your search
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
|
||||||
|
public interface ASetInterface {
|
||||||
|
|
||||||
|
/** Add a vertex to the ASet */
|
||||||
|
public void add(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/** Check if a vertex belongs to ASet */
|
||||||
|
public boolean contains(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class Dijkstra {
|
||||||
|
|
||||||
|
private PreviousInterface dijkstra(GraphInterface g, VertexInterface r, ASetInterface a, PiInterface pi, PreviousInterface previous) {
|
||||||
|
|
||||||
|
a.add(r) ; ;
|
||||||
|
VertexInterface pivot = r ;
|
||||||
|
pi.setPi(0, r) ;
|
||||||
|
|
||||||
|
ArrayList<VertexInterface> vertexList = g.getVertexes() ;
|
||||||
|
|
||||||
|
for (VertexInterface vertex: vertexList) {
|
||||||
|
if (vertex != r) {
|
||||||
|
pi.setPi(Integer.MAX_VALUE, vertex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = g.getVertexNumber();
|
||||||
|
for (int j = 0; j < n; j++) {
|
||||||
|
for (VertexInterface vertex: vertexList) {
|
||||||
|
if (! a.contains(vertex) && previous.getPrevious(vertex).compareTo(pivot)) {
|
||||||
|
if (pi.getPi(pivot) + g.getCost(pivot, vertex) < pi.getPi(vertex)) {
|
||||||
|
pi.setPi(pi.getPi(pivot) + g.getCost(pivot, vertex), vertex) ;
|
||||||
|
previous.setPrevious(pivot, vertex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int piMax = Integer.MAX_VALUE ;
|
||||||
|
for (VertexInterface vertex: vertexList) {
|
||||||
|
if (! a.contains(vertex) && pi.getPi(vertex) < piMax) {
|
||||||
|
pivot = vertex ;
|
||||||
|
piMax = pi.getPi(vertex) ;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
a.add(pivot);
|
||||||
|
}
|
||||||
|
|
||||||
|
return previous ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
public class MainTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
Box[][] box = new Box[2][2] ;
|
||||||
|
Maze mazeTest = new Maze(box) ;
|
||||||
|
mazeTest.initFromTextFile("data/labyrinthe.txt");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class Maze
|
public class Maze
|
||||||
@@ -46,6 +48,38 @@ public class Maze
|
|||||||
return vertexList ;
|
return vertexList ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public final void initFromTextFile(String filename) {
|
||||||
|
|
||||||
|
FileReader fr = null ;
|
||||||
|
BufferedReader br = null ;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fr = new FileReader(filename) ;
|
||||||
|
br = new BufferedReader(fr) ;
|
||||||
|
|
||||||
|
|
||||||
|
int lineLength = br.readLine().length() - 1 ;
|
||||||
|
int currentLineLength ;
|
||||||
|
int cInt = br.read() ;
|
||||||
|
|
||||||
|
while (cInt != -1)
|
||||||
|
{
|
||||||
|
currentLineLength = br.readLine().length() ;
|
||||||
|
if (currentLineLength != lineLength) {
|
||||||
|
throw new MazeReadingException(filename) ;
|
||||||
|
}
|
||||||
|
cInt = br.read() ;
|
||||||
|
}
|
||||||
|
} catch (MazeReadingException mre) {
|
||||||
|
mre.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
try { br.close() ;} catch (Exception e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public int getDimensionX() {
|
public int getDimensionX() {
|
||||||
return dimensionX;
|
return dimensionX;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
public class MazeReadingException
|
||||||
|
extends Exception {
|
||||||
|
|
||||||
|
private String filename ;
|
||||||
|
|
||||||
|
public MazeReadingException (String filename) {
|
||||||
|
|
||||||
|
super("Invalid maze format :" + filename) ;
|
||||||
|
this.filename = filename ;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFilename() {
|
||||||
|
return filename ;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
public interface PiInterface {
|
||||||
|
|
||||||
|
/** Returns the value of pi(v) */
|
||||||
|
public int getPi(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/** Sets the Pi value of an element */
|
||||||
|
public void setPi(int i, VertexInterface vertex) ;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
|
||||||
|
public interface PreviousInterface {
|
||||||
|
|
||||||
|
/** Returns the father of an element */
|
||||||
|
public VertexInterface getPrevious(VertexInterface vertex) ;
|
||||||
|
|
||||||
|
/** Sets the father of an element to another element */
|
||||||
|
public void setPrevious(VertexInterface father, VertexInterface son) ;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user