Weird problem upon calling same function twice
Hi all this is my 3rd post with the same problem. i am calling a function from a class which should calculate a rounting path. Algorithm works fine it does return the path as i want it when i call it first time. Second time though when i call this function it returns me the same path. I can't understand why. I paste here my class and in red will be the code i found out that the secodn time the algorithm doesn't go into.
Code:
import java.util.*;
//import PathFinder.Position;
public class PathFinder
{
public static class Position {
private int row;
private int col;
Position(int trow,int tcol){
row = trow;
col = tcol;
}
};
public static int[][] map; // map size depends on the tests
public static int length;// = 5; //initial map length is 5
public static int height;
public static int pathLength;
//private ArrayQueue q;
public Position[] path;
public static Position start, finish;
public int startx,starty,finishx,finishy;
public PathFinder(int maplength, int mapheight, int[][] mapp, int startPx, int startPy, int finishPx,int finishPy){
length = maplength;
height = mapheight;
//map = new int[length+2][height+2];
map = mapp;
startx = startPx;
starty = startPy;
finishx = finishPx;
finishy = finishPy;
setBounds();
start = new Position(startx, starty);
finish = new Position(finishx, finishy);
//q = new ArrayQueue();
}
public PathFinder(int maplength, int mapheight, int[][] mapp, int agentId, int finishPx, int finishPy){
length = maplength;
height = mapheight;
map = mapp;
finishx = finishPx;
finishy = finishPy;
setBounds();
}
public void setBounds(){
for (int i=0;i<=length+1;i++){
map[0][i] = map[length+1][i] = 1;
map[i][0] = map[i][length+1] = 1;
}
}
public static void printMap(){
for(int i = 0; i < length+2; i++){
for(int j = 0; j<length+2; j++)
System.out.print(map[i][j] + " ");
System.out.println();
}
}
public boolean findPath(){
ArrayQueue q = new ArrayQueue();
System.out.println("searching path...");
if ((start.row == finish.row)&(start.col == finish.col)){
pathLength = 0;
return false;
}
Position[] offset = new Position[4];
offset[0] = new Position (0,1);
offset[1] = new Position (1,0);
offset[2] = new Position (0,-1);
offset[3] = new Position (-1,0);
Position here = new Position(start.row,start.col);
int nrOfNbrs = 4;
Position nbr = new Position(0,0);
[COLOR="Red"]do{
for (int i=0;i<nrOfNbrs;i++)
{
nbr.row = here.row + offset[i].row;
nbr.col = here.col + offset[i].col;
if (map[nbr.row][nbr.col] == 0)
{ // unlabeled nbr, label it
map[nbr.row][nbr.col] = map[here.row][here.col] + 1;
if ((nbr.row == finish.row) && (nbr.col == finish.col))
break; // done
q.add(new Position(nbr.row,nbr.col));
}
else if(map[nbr.row][nbr.col] == 1){
break;
}
}
if ((nbr.row == finish.row) && (nbr.col == finish.col))
break;
if (q.isEmpty()) return false; // no path
here = (Position) q.remove(); // get next position
} while(true);[/COLOR]
pathLength = map[finish.row][finish.col] - 2;
path = new Position [pathLength];
here = finish;
for (int j = pathLength - 1; j >= 0; j--)
{
path[j] = here;
// find predecessor position
for (int i = 0; i < nrOfNbrs; i++)
{
nbr.row = here.row + offset[i].row;
nbr.col = here.col + offset[i].col;
if (map[nbr.row][nbr.col] == j + 2)
break;
}
here = new Position(nbr.row, nbr.col); // move to predecessor
}
return true;
}
synchronized public int[][] outputPath()
{System.out.println("Path finder says: pathLength is:" + pathLength);
int[][] result = new int[pathLength][2];
System.out.println("The wire path is");
for (int i = 0; i < pathLength; i++){
//System.out.println("path is:" + path[i].row + " " + path[i].col);
//map[path[i].row][path[i].col] = 8;}
result[i][0] = path[i].row;
result[i][1] = path[i].col;
}
return result;
}
/** entry point for wire routing program */
// public static void main(String [] args)
// {
// inputData();
// if (findPath())
// outputPath();
// else System.out.println("There is no wire path");
// printMap();
// }
}
Any help will be gratefull.
Thank you all