[quote]
火星探测器 一小队机器人探测器将由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[/quote]

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);   }}

关于火星探测器的试题相关推荐

  1. 中国首个火星探测器天问一号发射成功!

    作者 | 静静 来源 | 网易科技(ID:tech_163) "GO TO MARS TOGTHER!"刚刚,中国火星探测任务"天问一号"成功发射,开启了我国对 ...

  2. 天问一号火星探测器已飞离地球800多万公里 多个载荷完成自检

    中新社北京8月20日电 (郭超凯)记者从中国国家航天局探月与航天工程中心获悉,截至8月19日晚23时20分,中国天问一号火星探测器距离地球约823万公里,状态正常. 8月19日22时20分起,天问一号 ...

  3. 当时我就哭了!中国首个火星探测器天问一号发射成功!

    作者|静静 出品|网易科技<态℃>栏目组 "GO TO MARS TOGTHER!"刚刚,中国火星探测任务"天问一号"成功发射,开启了我国对火星的首 ...

  4. 《安富莱嵌入式周报》第298期:迷你火星探测器,开源单片机3D实时渲染库, 开源USB工业相机,VS2022开始支持MarkDown,PC-lint 2.0发布

    往期周报汇总地址:嵌入式周报 - uCOS & uCGUI & emWin & embOS & TouchGFX & ThreadX - 硬汉嵌入式论坛 - P ...

  5. NASA教你攒火星探测器,连购物车都加好了

    西雅图IT圈:seattleit [今日作者] PowerBall选号机 身体和灵魂总有一个要 走在买PowerBall的路上 各位朋友们Happy Friday!!如果你周末还没娱乐计划,那好办了, ...

  6. 火星探测器纷纷上天,人类离移民火星还远吗?

    上个月,即使是对太空探索再漠不关心的人,也都差不多知道今年是火星大年了.而从7月中旬到8月中旬,正好就是两年多才会遇到的火星探测窗口期. 因此我们就看到,7月份的后半个月里,阿联酋.中国和美国接二连三 ...

  7. “好奇号”火星探测器正在利用AI自主寻找探测目标

    遥感杆在NASA的"好奇号"火星探测器上支撑两台科学仪器来研究探测器周围环境,两台立体导航摄像机用来驱动探测器并且规划探测器的活动.(NASA/JPL-Caltech ) 根据一份 ...

  8. 喜讯:我国首个火星探测器萤火一号顺利升空

    消息传来,我快要蹦起来了,拍手欢呼,简直忘乎所以. 我从少年时代起就神往火星上面的"人工运河",以及火星的极小卫星"火卫一"是否系人工所造.我曾经自制望远镜观察 ...

  9. libreoffice_NASA开源火星探测器,罗马采用LibreOffice,更多新闻

    libreoffice 在本期开放源代码新闻摘要中,我们了解了NASA的开放源代码"火星漫游者",罗马最新的开放源代码工作,两个新的研究项目等等. NASA火星探测器的开源设计 您 ...

最新文章

  1. 智能家庭本周锋闻:进击的情趣
  2. r怎么保存html文件,leaflet - 为什么在已保存的html文件中缺少传单地图上的杂项,但在Rstudio浏览器中可以正常打印呢? - 堆栈内存溢出...
  3. wxWidgets:显示和测试 wxDC 功能
  4. 数据库工具Navicat for MySQL
  5. SCI/EI期刊投稿 Reply Letter 常用格式总结
  6. python启动方法_python进程开启的两种方式
  7. Android从无知到有知——NO.6
  8. quartus ii 管脚映射
  9. FastDFS原理介绍
  10. mac 修改hosts不生效问题
  11. 关于a标签 中特有的tilte属性 和 字符串文字换行
  12. iptables中DNAT、SNAT和MASQUERADE的理解及限速syn
  13. More Grounded Image Captioning by Distilling Image-Text Matching Model
  14. NetGear stora 重置成功后,个人文件夹丢失的找回办法
  15. (第二场网络赛J题)Leaking Roof(bfs)
  16. CSS文本样式和CSS3文本效果以及背景
  17. 异常检测 | 用于无监督异常检测的自监督学习适应性记忆网络
  18. 什么是ADO.NET
  19. 基于YOLO的人脸检测和人脸计数(课程设计)
  20. 基于向量加权平均算法的函数寻优和工程优化

热门文章

  1. java mfcc_MFCC特征提取过程详解
  2. VScode离线安装插件报错Corrupt ZIP: end of central directory record signature not found解决
  3. python isdigit和isnumeric区别_isdigit()、isdecimal()和isnumeric python中区别【详细讲解】...
  4. SLAM知识点——P3P知识点
  5. java师傅的名人名言
  6. 树莓派不完全手册——配置树莓派系统
  7. 360安全浏览器截图快捷键怎么设置 360安全浏览器截图快捷键设置教程
  8. PowerApps教程04-屏幕切换与布局
  9. UCF 2021 Qualifying - H . Time to Eat + UCF HSPT 2020 - E . Use Giant Fans to Deal With Hurricanes?
  10. Maxthon显示界面问题