The goal for your program is to safely land the \"Mars Lander\" shuttle, the lan
ID: 3583331 • Letter: T
Question
The goal for your program is to safely land the "Mars Lander" shuttle, the landing ship which contains the Opportunity rover. Mars Lander is guided by a program, and right now the failure rate for landing on the NASA simulator is unacceptable.
This puzzle is the last level of the "Mars Lander" trilogy. The controls are the same as the previous levels but the surface is more complex...
Rules
Built as a game, the simulator puts Mars Lander on a limited zone of Mars sky.
The zone is 7000m wide and 3000m high.
There is a unique area of flat ground on the surface of Mars, which is at least 1000 meters wide.
Every second, depending on the current flight parameters (location, speed, fuel ...), the program must provide the new desired tilt angle and thrust power of Mars Lander:
Angle goes from -90° to 90° . Thrust power goes from 0 to 4 .
The game simulates a free fall without atmosphere. Gravity on Mars is 3.711 m/s² . For a thrust power of X, a push force equivalent to X m/s² is generated and X liters of fuel are consumed. As such, a thrust power of 4 in an almost vertical position is needed to compensate for the gravity on Mars.
For a landing to be successful, the ship must:
land on flat ground
land in a vertical position (tilt angle = 0°)
vertical speed must be limited ( ? 40m/s in absolute value)
horizontal speed must be limited ( ? 20m/s in absolute value)
Note
Tests and validators are only slightly different. A program that passes a given test will pass the corresponding validator without any problem.
Game Input
The program must first read the initialization data from standard input. Then, within an infinite loop, the program must read the data from the standard input related to Mars Lander's current state and provide to the standard output the instructions to move Mars Lander.
Initialization input
Line 1: the number surfaceN of points used to draw the surface of Mars.
Next surfaceN lines: a couple of integers landX landY providing the coordinates of a ground point. By linking all the points together in a sequential fashion, you form the surface of Mars which is composed of several segments. For the first point, landX = 0 and for the last point, landX = 6999
Input for one game turn
A single line with 7 integers: X Y hSpeed vSpeed fuel rotate power
X,Y are the coordinates of Mars Lander (in meters).
hSpeed and vSpeed are the horizontal and vertical speed of Mars Lander (in m/s). These can be negative depending on the direction of Mars Lander.
fuel is the remaining quantity of fuel in liters. When there is no more fuel, the power of thrusters falls to zero.
rotate is the angle of rotation of Mars Lander expressed in degrees.
power is the thrust power of the landing ship.
Output for one game turn
A single line with 2 integers: rotate power :
rotate is the desired rotation angle for Mars Lander. Please note that for each turn the actual value of the angle is limited to the value of the previous turn +/- 15°.
power is the desired thrust power. 0 = off. 4 = maximum power. Please note that for each turn the value of the actual power is limited to the value of the previous turn +/- 1.
Constraints
2 ? surfaceN < 30
0 ? X < 7000
0 ? Y < 3000
-500 < hSpeed, vSpeed < 500
0 ? fuel ? 2000
-90 ? rotate ? 90
0 ? power ? 4
Response time per turn ? 100ms
Example
Initialization input
No output expected
You can ignore this but you need to read the values.
Input for turn 1
Output for turn 1
Requested rotation to the right, maximum thrust power
Input for turn 2
Tilt angle changed only by 15° and thrust power only by 1
Output for turn 2
Same request as previous turn
Input for turn 3
Output for turn 3
Same request as previous turn
Synopsis
Same place, the day after next. The meeting room stinks of sweat. Left-over pizzas are scattered on the tables.
“I have a good feeling about this. I am starting to see the light. Mike do you see the light?”
“Well, it's not too bad.”
“Not too bad? A little enthusiasm wouldn't hurt! This rover will land, I'm telling you!”
Jeff, who is overexcited, is frantically chewing at a match whilst giving you his latest encouragements:
“ OK, this is the last percent. The most hardened of our developers have lost their shirts on that one. I know you can do it. So DON'T let me down!”
This is what I have
import java.util.*;
import java.math.*;
class Player {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] surfaceX = new int[N];
int[] surfaceY = new int[N];
for (int i = 0; i < N; i++) {
surfaceX[i] = in.nextInt();
surfaceY[i] = in.nextInt();
}
while (true) {
int X = in.nextInt();
int Y = in.nextInt();
int dX = in.nextInt();
int dY = in.nextInt();
in.nextInt(); in.nextInt(); in.nextInt();
// Finds landing altitude by Looking for 2 consecutive points w/ same Y
int groundY = -1;
for (int i = 0; (i < N && groundY == -1); i++)
if (surfaceX[i] <= X && X <= surfaceX[i+1])
groundY = surfaceY[i];
/* Finds out if it is safe to start braking only next turn, by
simulating it and analyzing vertical speed at landing
If it is safe, waits, else starts braking*/
double vdY = dY - 8.555;
double vY = Y - 36.665 + 5*dY;
long t = Math.round((-40-vdY)/0.289);
if (vY + t*(vdY+0.289*(1+t)/2) > groundY)
System.out.println("0 0");
else
System.out.println("0 4");
}
}
}
Explanation / Answer
import java.util.*;
class MarsLander{
int xvalue;
int yvalue;
int hspeed;
int vspeed;
int fuel;
int rotate;
int power;
String state;
}
public class Player {
public static void main(String args[]) {
MarsLander ml=new MarsLander();
ArrayList<MarsLander> mlist=new ArrayList<MarsLander>();
final int MAXX=6999;
final int MAXY=3000;
System.out.println("Enter number of surfaces:");
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int[] surfaceX = new int[N];
int[] surfaceY = new int[N];
int angle;
for (int i = 0; i < N; i++) {
System.out.println("Enter "+i+"th surface for generating"+N+" lines:");
if(i<MAXX)
surfaceX[i] = in.nextInt();
if(i<MAXY)
surfaceY[i] = in.nextInt();
}
String c="yes";
while (!c.equals("no")) {
System.out.println("Enter Input to test:");
System.out.println("Enter XValue:");
ml.xvalue = in.nextInt();
if(ml.xvalue>7000 || ml.xvalue<0)
ml.xvalue = in.nextInt();
System.out.println("Enter YValue:");
ml.yvalue = in.nextInt();
if(ml.yvalue>3000 || ml.yvalue<0)
ml.yvalue = in.nextInt();
System.out.println("Enter horizontal speed:");
ml.hspeed = in.nextInt();
if(ml.hspeed>500 || ml.hspeed<-500)
ml.hspeed = in.nextInt();
System.out.println("Enter vertical speed:");
ml.vspeed = in.nextInt();
if(ml.vspeed>500 || ml.vspeed<-500)
ml.vspeed = in.nextInt();
System.out.println("Enter fuel speed:");
ml.fuel=in.nextInt();
if(ml.fuel>2000 || ml.fuel<0)
ml.fuel = in.nextInt();
System.out.println("Enter rotation angle:");
ml.rotate=in.nextInt();
if(ml.rotate>90 || ml.rotate<-90)
ml.rotate = in.nextInt();
System.out.println("Enter power:");
if(ml.fuel==0) ml.power=0;
else
ml.power=in.nextInt();
if(ml.power>4 || ml.power<0)
ml.power = in.nextInt();
// Finds landing altitude by Looking for 2 consecutive points w/ same Y
int groundY = -1;
for (int i = 0; (i < N && groundY == -1); i++)
if (surfaceX[i] <= ml.xvalue && ml.xvalue <= surfaceX[i+1])
groundY = surfaceY[i];
/* Finds out if it is safe to start braking only next turn, by
simulating it and analyzing vertical speed at landing
If it is safe, waits, else starts braking*/
double vdY = ml.vspeed - 8.555;
double vY = ml.yvalue- 36.665 + 5*ml.vspeed;
long t = Math.round((-40-vdY)/0.289);
if (vY + t*(vdY+0.289*(1+t)/2) > groundY){
System.out.println("0 0");
ml.state="crashed";
}
else{
System.out.println("0 4");
ml.state="landed";
mlist.add(ml);
}
System.out.println("Do you want to continue(Yes/No):");
c= in.next();
}
for(MarsLander m:mlist){
if(m.state.equals("landed")){
System.out.println(m);
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.