ChaudharyTheCoder
BAN USERpackage com.pc.careerCup;
public class MetricPath{
public static void main(String args[]){
int[][] a={{0,0,1,0,1},
{0,0,0,0,0},
{0,1,1,1,1},
{0,1,1,0,0}};
int[][] visited= a;
System.out.println("Result : " + isPath(a, 0, 3,3, 0, visited));
}
private static boolean isPath(int[][] a, int x, int y, int endX, int endY, int[][] visited){
System.out.print("{" + x + "," + y + "} ->");
if(x == endX && y==endY){
return true;
}
boolean result=false;
visited[x][y]=1;
//Move up
if(x-1>=0 && (visited[x-1][y]!=1)){
result=isPath(a, x-1, y, endX, endY, visited);
}
//Move down
if((!result) && (x+1 < a.length) && (visited[x+1][y]!=1)){
result=isPath(a, x+1, y, endX, endY, visited);
}
//Move left
if((!result) && (y-1 >=0 )&& (visited[x][y-1]!=1)){
result=isPath(a, x, y-1,endX, endY, visited);
}
//Move right
if((!result) && (y + 1 <a[0].length) && (visited[x][y+1]!=1)){
result=isPath(a, x, y+1,endX, endY, visited );
}
return result;
}
}
i/p: {0, 3} ,{ 3, 0}
- ChaudharyTheCoder June 06, 2015o/p: Result:True,
{0,3} ->{1,3} ->{1,2} ->{1,1} ->{0,1} ->{0,0} ->{1,0} ->{2,0} ->{3,0}
i/p {0, 3} ,{ 3, 3}
o/p: Result: True