I have a question about this code and time complexity of it.
I know that BFS on matrix has nm time complexity, but here we have to travel for every cell 4 cell more, so it will be 4^(nm)?
We cant go back in our travel.
public int modDistanceSearch(Para positionZero,char dest){// miejsce na Pare czyli dwie wspolrzede dla Artura
Trojka start = new Trojka(positionZero.left,positionZero.right,0);
Queue<Trojka> queue = new LinkedList<>();
queue.add(start);
boolean[][] visited = new boolean[map.length][map[0].length];
visited[start.left][start.right] = true;
while (!queue.isEmpty()){
// System.out.println("sdfg");
Trojka cur = queue.remove();
if(map[cur.left][cur.right]==dest){
// System.out.println(cur.left+" "+cur.right);
tutajKonczyArtur.right = cur.right;
tutajKonczyArtur.left = cur.left;
return cur.distance;
}
//up, down, left, right
if(correctPosition(cur.left-1,cur.right,visited)){
queue.add(new Trojka(cur.left-1,cur.right,cur.distance+1));
visited[cur.left-1][cur.right]=true;
startToS.left =cur.left-1;
startToS.right =cur.right;
dir.a +=1;
}
if(correctPosition(cur.left+1,cur.right,visited)){
queue.add(new Trojka(cur.left+1,cur.right,cur.distance+1));
visited[cur.left+1][cur.right]=true;
startToS.left =cur.left+1;
startToS.right =cur.right;
dir.b +=1;
}
if(correctPosition(cur.left,cur.right-1,visited)){
queue.add(new Trojka(cur.left,cur.right-1,cur.distance+1));
visited[cur.left][cur.right-1]=true;
startToS.left =cur.left;
startToS.right =cur.right-1;
dir.c+=1;
}
if(correctPosition(cur.left,cur.right+1,visited)){
queue.add(new Trojka(cur.left,cur.right+1,cur.distance+1));
visited[cur.left][cur.right+1]=true;
startToS.left =cur.left;
startToS.right =cur.right+1;
dir.d+=1;
}
}
return -1;
}