引用
火星探测器 一小队机器人探测器将由NASA送上火星高原,探测器将在这个奇特的矩形高原上行驶。 用它们携带的照相机将周围的全景地势图发回到地球。每个探测器的方向和位置将由一个x,y系坐标图和一个表示地理方向的字母表示出来。为了方便导航,平原将被划分为网格状。位置坐标示例:0,0,N,表示探测器在坐标图的左下角,且面朝北方。为控制探测器,NASA会传送一串简单的字母。可能传送的字母为:'L','R'和'M'。 'L',和'R'分别表示使探测器向左、向右旋转90度,但不离开他所在地点。'M' 表示向前开进一个网格的距离,且保持方向不变。假设以广场(高原)的直北方向为y轴的指向。 输入:首先输入的line是坐标图的右上方,假定左下方顶点的坐标为0,0。剩下的要输入的是被分布好的探测器的信息。每个探测器需要输入wo lines。第一条line 提供探测器的位置,第二条是关于这个探测器怎样进行高原探测的一系列说明。位置是由两个整数和一个区分方向的字母组成,对应了探测器的(x,y)坐标和方向。每个探测器的移动将按序完成,即后一个探测器不能在前一个探测器完成移动之前开始移动。 输出:每个探测器的输出应该为它行进到的最终位置坐标和方向。输入和输出 测试如下: 期待的输入:5 5 1 2 N LMLMLMLMM 3 3 E MMRMMRMRRM 期待的输出 1 3 N 5 1 E
Action.java:
public interface Action {
public String getOutput(InputStream input);
}
ActionImpl.java:
public class ActionImpl implements Action {
//the width and the height of each grid
private int grid_width = 1;
private int grid_height = 1;
//the max width and the max height of the whole grids
private int max_grid_width;
private int max_grid_height;
//the min width and the max height of the whole grids
private int min_grid_width;
private int min_grid_height;
private String output;
public ActionImpl(){
}
public ActionImpl(int min_grid_width, int min_grid_height){
this.min_grid_height = min_grid_height;
this.min_grid_width = min_grid_width;
}
public ActionImpl(int... args) throws Exception{
if(args.length == 4){
this.max_grid_width = args[0];
this.max_grid_height = args[1];
this.min_grid_width = args[2];
this.min_grid_height = args[3];
}else{
throw new Exception("There is not enough args");
}
}
public String getOutput(InputStream input){
List<String> input_divided = devideInput(input);
if(Integer.parseInt(input_divided.get(0).toString())<=this.min_grid_height
|| Integer.parseInt(input_divided.get(1).toString())<=this.min_grid_width){
try {
throw new Exception("you entered the uncorrect args");
} catch (Exception e) {
e.printStackTrace();
}
}
this.max_grid_height = Integer.parseInt(input_divided.get(0).toString());
this.max_grid_width = Integer.parseInt(input_divided.get(1).toString());
List<Robot> robots = new ArrayList<Robot>();
try {
robots = generateRobots(robots, input_divided);
} catch (Exception e) {
e.printStackTrace();
}
this.output = "";
for(int i=0;i<robots.size();i++){
try {
this.output += explore((Robot) robots.get(i)) + " ";
} catch (Exception e) {
e.printStackTrace();
}
}
return this.output.trim();
}
private List<Robot> generateRobots(List<Robot> robots, List<String> input_divided) throws Exception{
for(int i=2;i<input_divided.size();i=i+4){
Robot robot = new Robot();
robot.setX(Integer.parseInt(input_divided.get(i).toString()));
robot.setY(Integer.parseInt(input_divided.get(i+1).toString()));
isBeyondTheMark(robot);
robot.setDirection(input_divided.get(i+2).toString().charAt(0));
robot.setAction(input_divided.get(i+3).toString());
robots.add(robot);
}
return robots;
}
private List<String> devideInput(InputStream input){
Scanner scan = new Scanner(input);
List<String> input_divided = new ArrayList<String>();
while(scan.hasNext()){
String each_input = scan.next();
if(each_input=="stop"||each_input.equals("stop")){
break;
}
input_divided.add(each_input);
}
return input_divided;
}
private String explore(Robot robot) throws Exception{
char[] action = robot.getAction().toCharArray();
for(int i=0;i<action.length;i++){
switch(action[i]){
case 'L':
turn(robot, 'L');
break;
case 'R':
turn(robot, 'R');
break;
case 'M':
turn(robot, 'M');
break;
}
}
return robot.getX() + " " + robot.getY() + " " + robot.getDirection();
}
private void turn(Robot robot, char direction) throws Exception{
if(direction == 'L'){
for(int j=0;j<Robot.directions.length;j++){
if(robot.getDirection()==Robot.directions[j]){
if(j==0){
robot.setDirection(Robot.directions[3]);
}else{
robot.setDirection(Robot.directions[j-1]);
}
break;
}
}
}else if(direction == 'R'){
for(int j=0;j<Robot.directions.length;j++){
if(robot.getDirection()==Robot.directions[j]){
if(j==3){
robot.setDirection(Robot.directions[0]);
}else{
robot.setDirection(Robot.directions[j+1]);
}
break;
}
}
}else if(direction == 'M'){
if(robot.getDirection()==Robot.directions[0]){
robot.setY(robot.getY()+1);
}else if(robot.getDirection()==Robot.directions[1]){
robot.setX(robot.getX()+1);
}else if(robot.getDirection()==Robot.directions[2]){
robot.setY(robot.getY()-1);
}else if(robot.getDirection()==Robot.directions[3]){
robot.setX(robot.getX()-1);
}
}
isBeyondTheMark(robot);
}
private void isBeyondTheMark(Robot robot) throws Exception{
if(robot.getX()>this.max_grid_height||robot.getY()>this.max_grid_width
||robot.getX()<this.min_grid_height||robot.getY()<this.min_grid_width){
throw new Exception("beyond the mark");
}
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
}
Robot.java:
public class Robot {
public static final char[] directions = {'N', 'E', 'S', 'W'};
// the position of the robotic rover
private int x;
private int y;
private char direction;
private String action;
}
Main.java:
public class Main {
public static void main(String[] args) {
Action action = new ActionImpl();
String output = action.getOutput(System.in);
System.out.println(output);
}
}
分享到:
相关推荐
火星探测器进入段自适应滑模制导律设计,许康,马广富,针对火星探测器进入段制导问题,设计了自适应滑模制导律。首先,建立了火星探测器进入段运动学模型,并对侧向运动方程进行线性化
针对火星探测器降落伞在拉直过程中出现的“绳帆”现象,以及火星探测器降落伞开伞前初始参数和 大气密度与地球环境下的差异,建立了火星探测器降落伞拉直过程的数学模型,研究了火星环境下,伞包弹射速 度、开伞前...
火星探测器模拟小程序,Java编写. 模拟远程对火星探测器的操控。
数学建模,内含建模思路以及完整代码。
1. 仿效教材第24章例子,开发展示NASA的火星探测器所拍摄照片的app 2. 利用持久化保存技术对网络下载的图片进行缓存,对已缓存的图片直接从本地读取,减 少重复的网络请求 3. 参考WEB API: ...
#资源达人分享计划#
最优控制,火星探测器小推力变轨,可显示逃逸速度
火星探测器3D模型,FBX格式带贴图
为了防止火星探测器自主着陆过程中降落到坡度较高的坡面造成翻转,在探测器着陆过程中对拟着陆面进行坡度估计,提出了一种基于机器视觉的坡度估计方案,并完成坡度估计方案的仿真验证。软件部分采用matlab2014a进行...
利用重力场模型MRO120D分析火星探测器的轨道演化.docx
火星探测器Mars Rover 这个项目的代码是用 Java 和 NetBeans 开发的。 您可以下载整个项目或在另一个 IDE 中运行它,您只需要 Source 文件夹的全部内容。 从控制台运行 PlateauMain.java 这将启动以下消息:“输入要...
1. 仿效教材第24章例子,开发展示NASA的火星探测器所拍摄照片的app 2. 利用持久化保存技术对网络下载的图片进行缓存,对已缓存的图片直接从本地读取,减 少重复的网络请求 3. 参考WEB API: ...
编程测试火星探测器测试美国宇航局最近将火星车降落在火星高原上。 这个特殊的高原是奇怪的矩形,必须由漫游车导航,以便车载摄像机可以完整地查看周围的地形,并将其发送回地球上的任务控制中心。 漫游车的位置和...
基于脉冲星和火星观测的深空探测器自主导航,刘劲,房建成,为了消除脉冲星星历误差并提高火星探测巡航段末端导航精度,提出了一种基于脉冲星和火星观测的组合导航方法。由于当前测量技术有
行业资料-交通装置-一种大椭圆轨道火星探测器自主对火定向方法.zip
他们基本上说“自己找到一个API并为其创建一个应用程序”,所以我制作了一个应用程序来演示NASA火星探测器拍摄的照片。 安装 npm install 配置 由于提供了默认值,因此严格不要求进行任何配置,尽管由于强烈建议您...
该文件为游戏航空模拟器附加资源包
火星探测无人机建模与拉起平飞控制
火星机器人探测.pdf