Help me!

P

Piotre Ugrumov

I have a museum map of this type:

7-8-3
| | |
6-4-2
| | |
5-0-1

the | and the - are the corridors between the rooms of the museum.
I have created a class Room, every object of this class has an id, a name,
an array where are inserted the near room(for example for the room 2 the
near are 3, 1, 4 this id are inserted in the array vicini), an array
percorsi where are memorized the path to all the rooms of the musem (for
example, in the room 0 the path to go to the room 8 is memorized in
percorsi[8] and is 0 4 8).
I have in a main an array of room that rapresent the museum.
I have a lot of problems in the determination of the path.
Now I pass you the class Room with the implementation of the method that
determinates the path.

Class Room:

public class Room {

private int id;
private String nome; //the name of the room
private String percorsi[]= new String[9]; //in this array the are the
path for all the destination
private int vicini[]=new int[4]; //this array contains the near room
private boolean verifica[]=new boolean [9]; //if verifica[x] is set to
true means that there is the path to the destination x

public Room() {
id=100;
nome="";
for(int i=0; i<9; i++){
percorsi="";
}
}


public Room(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<4; i++)
vicini=100; //the 100 means that there are not near room
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}

public void setRoom(int a, String s, String p, String o){
setId(a);
setNome(s);
for(int i=0; i<9; i++){
percorsi="";
percorsi+=id;
}
}

public void setId(int a){
id=a;
verifica[id]=true; //the path to itself is always true
}

public void setNome(String s){
nome=s;
}

public void addVicino(int v){
for(int i=0; i<4; i++){
if(vicini==100 && v!=100){
vicini=v;
verifica[v]=true;
percorsi[v]+=" " + v; //this line of code permit to set the
path to the near room in fact the path is, for example for the room 3 if is
inserted the near room 2, 3 2
break;
}
}
}

public int getVicino(int v){
return vicini[v];
}

public int getId(){
return id;
}

public String getNome(){
return nome;
}


public void Percorsi(Room museo[]){//crea i percorsi

for(int l=0; l<museo.length-1; l++){ //the cycle is repeated to
determinate all the path
for(int j=0; j<verifica.length; j++){
if(verifica[j]==false){//verify if the path to the
destination j is determinated
for(int h=0; h<vicini.length; h++){
if(vicini[h]!=100 &&
museo[getVicino(h)].verifica[j]){ //if the near room is valid(that is the
number in the array vicini is different from 100) and if the near room has
the path to the destination j
percorsi[j]+= " " +
museo[getVicino(h)].getPercorsi(j); //in the path to j(in this path there
is already the id of the room, for example all the path of the room 0 start
with 0) is added the path to j through the near room tha permit to arrive to
j, for example if I am in the room 8 I want go to the room 2(j), the path
is to the path to 3 of 8 (that is 8) and the path to 2 of the room 3(that is
3 2)
the path complete is 8 3 2. This method seems work but there are some little
problems, if I try to run this method I have seen that the path from the
room 0
to the rooms 3 and 7 is uncorrect in fact the path that I receive is equal
to 0. If I try with the room 3 to see the path to the room 6, the method run
but the path that I receive is 3 2 1 0 4 6, this path is considerably longer
than 3 8 7 6. How can I resolve thes problems?

verifica[j]=true; //it signals that now there is
the path
j=0; //permit to start from 0 again the cycle,
in this way all the messagges are swapped
}
}
}
}

}
}


public String getPercorsi(int i){
return percorsi;
}


public boolean verifyVicino(int v){
for(int i=0; i<4; i++){
if(vicini!=100 && vicini==v && v!=100)
return true;
}
return false;
}

public boolean percorsoC(int v){
return verifica[v];
}


}
Thanks.
 
M

Michael Dunn

: I have a museum map of this type:
:
: 7-8-3
: | | |
: 6-4-2
: | | |
: 5-0-1
:
: the | and the - are the corridors between the rooms of the museum.
: I have created a class Room, every object of this class has an id, a name,
: an array where are inserted the near room(for example for the room 2 the
: near are 3, 1, 4 this id are inserted in the array vicini), an array
: percorsi where are memorized the path to all the rooms of the musem (for
: example, in the room 0 the path to go to the room 8 is memorized in
: percorsi[8] and is 0 4 8).
: I have in a main an array of room that rapresent the museum.
: I have a lot of problems in the determination of the path.
<snip>


If all you want is a way to determine the path between 2 rooms,
here's something to play around with
(enter the 2 room numbers as command line/main method arguments)

class Paths
{
int[] room = {7,8,3,6,4,2,5,0,1};
int[] directions = {-1,-3,3,1};
int startPoint, endPoint;
String shortSolution = "";
String roomsPath = "";
boolean firstSolution = true;

public Paths(int roomFrom,int roomTo)
{
startPoint = findElement(roomFrom);
endPoint = findElement(roomTo);
if(startPoint >=0 && endPoint >= 0)
{
findSolution(startPoint,"");
String[] bits = shortSolution.split("-");
for(int x = 0; x < bits.length; x++) roomsPath += "-"+room[Integer.parseInt(bits[x])];
System.out.println("Path = " + roomsPath.substring(1));
}
else System.out.println("Error - invalid room numbers, terminating");
System.exit(0);
}
private int findElement(int num)
{
for(int x = 0; x < room.length; x++)if(num == room[x]) return x;
return -1;
}
private void findSolution(int currentPos, String moveList)
{
moveList += "-"+currentPos;
if(currentPos == endPoint)
{
if(firstSolution)
{
shortSolution = moveList.substring(1);
firstSolution = false;
}
else if(moveList.substring(1).length() < shortSolution.length())
{
shortSolution = moveList.substring(1);
}
return;
}
else
{
for(int i = 0; i < directions.length; i++)
{
int nextPos = currentPos + directions;
if(nextPos >= 0 && nextPos < room.length &&
Math.abs(currentPos % 3 - nextPos % 3)<=1 &&
moveList.indexOf(""+nextPos) < 0)
{
findSolution(nextPos, moveList);
}
}
}
}
public static void main(String[] args){new Paths(Integer.parseInt(args[0]),Integer.parseInt(args[1]));}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
DewittMill
Top