shortest path using strings
class shortest{
public static void main(String[] args) {
int x2=0,y2=0,x1=0,y1=0;
//Time Complexity is O(n)
String str="WNEENESENNN";
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='W'){
x2=x2-1;
}
if(str.charAt(i)=='E'){
x2=x2+1;
}
if(str.charAt(i)=='N'){
y2=y2+1;
}
if(str.charAt(i)=='S'){
y2=y2-1;
}
}
int f=(int)Math.sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
System.out.println("Shortest path is "+f );
}
}
Comments
Post a Comment