thakur.871
BAN USERAssumption - Imaging X & Y axis dimensions. Robot will start at degree 0 & (X,Y)=(0,0).
So at start robot is pointing towards positive X axis.
if output is (0,0) i..e final co-ordinates of robot then there is a wall.
import java.lang.Math;
import java.text.DecimalFormat;
public class RoboticWall {
static double X = 0.00d;
static double Y = 0.00d;
static double currentAngle = 0.0d;
static DecimalFormat numberFormat = new DecimalFormat("#.##");
public static void main(String[] s){
//System.out.println(Math.cos(180/57.2958));
isWall();
}
public static void isWall(){
move(10);
rotate(90);
move(10);
rotate(90);
move(10);
rotate(90);
move(10);
System.out.println("X="+numberFormat.format(X)+"Y="+numberFormat.format(Y));
}
private static void rotate(double i) {
currentAngle = currentAngle + i;
}
private static void move(double i) {
X = X + i*Math.cos(currentAngle/57.29);
Y = Y + i*Math.sin(currentAngle/57.29);
}
}
Assumption - Imaging X & Y axis dimensions. Robot will start at degree & (X,Y)=(0,0).
So at start robot is pointing towards positive X axis.
if output is (0,0) i..e final co-ordinates of robot then there is a wall.
import java.lang.Math;
import java.text.DecimalFormat;
public class RoboticWall {
static double X = 0.00d;
static double Y = 0.00d;
static double currentAngle = 0.0d;
static DecimalFormat numberFormat = new DecimalFormat("#.##");
public static void main(String[] s){
//System.out.println(Math.cos(180/57.2958));
isWall();
}
public static void isWall(){
move(10);
rotate(90);
move(10);
rotate(90);
move(10);
rotate(90);
move(10);
System.out.println("X="+numberFormat.format(X)+"Y="+numberFormat.format(Y));
}
private static void rotate(double i) {
currentAngle = currentAngle + i;
}
private static void move(double i) {
X = X + i*Math.cos(currentAngle/57.29);
Y = Y + i*Math.sin(currentAngle/57.29);
}
}
output: X=0 Y=0
1. Create data-structure - hybrid of HashMap & Stack. Head = pointing to the top of the stack. Put() - put value in hash map & changes head to point to new value inserted.
2. Start iterating over the input of string
3. After completing the iteration in step-2, start stack.pop(head) & print till stack is empty.
- thakur.871 January 06, 2017