A robot is programmed to move forward F meters and backwards again, say B meters, in a straight line. The Robot covers 1 meter in T units of time. On Robot's path there is a ditch at a distance FD from initial position in forward direction as well as a ditch at a distance BD from initial position in backward direction. This forward and backward movement is performed repeatedly by the Robot.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Catch22
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
BufferedReader br;
br = new BufferedReader(new FileReader(args[0]));
String line;
int loc = 0 ,numOfTestCases = 0;
int F,B,T,FD,BD,distanceFromCenter , totalDistanceCovered;
while( ( line = br.readLine()) != null )
{
distanceFromCenter = 0;
totalDistanceCovered = 0;
++loc;
if ( loc > (numOfTestCases+1))
{
break;
}
else if ( loc == 1)
{
numOfTestCases = Integer.parseInt(line.trim());
continue;
}
String[] tokens = line.trim().split("\\s+");
F = Integer.parseInt(tokens[0].trim());
B = Integer.parseInt(tokens[1].trim());
T = Integer.parseInt(tokens[2].trim());
FD = Integer.parseInt(tokens[3].trim());
BD = Integer.parseInt(tokens[4].trim());
if ( ( F == B) && ( F < FD) )
{
System.out.println("No Ditch");
continue;
}
else
{
distanceFromCenter = F;
totalDistanceCovered = F;
while (true)
{
if ( distanceFromCenter >= FD)
{
int time = (totalDistanceCovered+(FD-distanceFromCenter)) * T;
System.out.println(time+" F");
break;
}
distanceFromCenter = distanceFromCenter - B;
totalDistanceCovered = totalDistanceCovered + B;
if ( distanceFromCenter < 0 )
{
if ( (-1*distanceFromCenter) >= BD)
{
int time = (totalDistanceCovered+(BD-(-1*distanceFromCenter))) * T;
System.out.println(time+" B");
break;
}
}
distanceFromCenter = distanceFromCenter + F;
totalDistanceCovered = totalDistanceCovered + F;
}
}
}
}
}
Input Format:
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 5 values delimited by space
F B T FD BD, where
First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 5 values delimited by space
F B T FD BD, where
- F denotes forward displacement in meters
- B denotes backward displacement in meters
- T denotes time taken to cover 1 meter
- FD denotes distance from Robot's starting position and the ditch in forward direction
- BD denotes distance from Robot's starting position and the ditch in backward direction
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Catch22
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
BufferedReader br;
br = new BufferedReader(new FileReader(args[0]));
String line;
int loc = 0 ,numOfTestCases = 0;
int F,B,T,FD,BD,distanceFromCenter , totalDistanceCovered;
while( ( line = br.readLine()) != null )
{
distanceFromCenter = 0;
totalDistanceCovered = 0;
++loc;
if ( loc > (numOfTestCases+1))
{
break;
}
else if ( loc == 1)
{
numOfTestCases = Integer.parseInt(line.trim());
continue;
}
String[] tokens = line.trim().split("\\s+");
F = Integer.parseInt(tokens[0].trim());
B = Integer.parseInt(tokens[1].trim());
T = Integer.parseInt(tokens[2].trim());
FD = Integer.parseInt(tokens[3].trim());
BD = Integer.parseInt(tokens[4].trim());
if ( ( F == B) && ( F < FD) )
{
System.out.println("No Ditch");
continue;
}
else
{
distanceFromCenter = F;
totalDistanceCovered = F;
while (true)
{
if ( distanceFromCenter >= FD)
{
int time = (totalDistanceCovered+(FD-distanceFromCenter)) * T;
System.out.println(time+" F");
break;
}
distanceFromCenter = distanceFromCenter - B;
totalDistanceCovered = totalDistanceCovered + B;
if ( distanceFromCenter < 0 )
{
if ( (-1*distanceFromCenter) >= BD)
{
int time = (totalDistanceCovered+(BD-(-1*distanceFromCenter))) * T;
System.out.println(time+" B");
break;
}
}
distanceFromCenter = distanceFromCenter + F;
totalDistanceCovered = totalDistanceCovered + F;
}
}
}
}
}