zoukankan      html  css  js  c++  java
  • 一个关于月球车的笔试题--求讨论

    题目如下:

    请用Java或者Ruby语言实现以下题目以及测试代码。

    一队机器人漫游车将被美国宇航局降落在火星高原上。漫游车将在这个奇怪的长方形高原上巡游,以便他们的机载摄像头可以获得周围地形的完整视图,并将其发送回地球。漫游者的坐标和位置由x和y坐标的组合以及代表四个方向(E, S, W, N)的字母表示。高原划分为网格以简化导航。比如位置0,0,N,表示漫游车位于左下角并面向北。为了控制漫游车,美国宇航局发送一串简单的字母。指令字母是'L','R'和'M'。 'L'和'R'使漫游车分别向左或向右旋转90度,而不会从当前地点移动。 'M'表示前进一个网格点,并保持相同的方向。

    假设从(x,y)直接向北移动,就到了(x,y + 1)。

    INPUT:

    第一行输入是平台的右上角坐标,左下角坐标被假定为0,0。

    其余的输入是有关已部署的漫游车的信息。每个漫游车都有两行输入。第一行给出了漫游车的位置,第二行是告诉漫游车如何探索高原的一系列指令。位置由两个整数和一个由空格分隔的字母组成,对应于x和y坐标以及漫游车当前的方向。

    每个漫游车将按顺序完成,这意味着第二个漫游车在第一个漫游车完成移动之前不会开始移动。

    OUTPUT:

    每个漫游车的输出应该是其最终的坐标和位置。

    输入输出例子

    输入:

    5 5

    1 2 N

    LMLMLMLMM

    3 3 E

    MMRMMRMRRM

    预期产出:

    1 3 N

    5 1 E

    我的解题想法及代码:

    package com.zzkj.zhy.action;

    import java.util.Scanner;

    import org.junit.Test;
    /**
     *
     * @author Administrator
     *1.将方向ESWN换为1234,方便编程
     *2.如果输入命令超出火星高原的长宽则让小车最后接触边界点位置
     */
    public class MyAction {
            private Scanner scanner = new Scanner(System.in);
            private final Integer X;
            private final Integer Y;
            {
                    System.out.println("火星高原的左下角坐标为(0,0),右上角坐标为:'格式--> x y 请指定:'");
                    String location = scanner.nextLine();
                    String[] split = location.split(" ");
                    X = getIntByString(split[0]);
                    Y = getIntByString(split[1]);
            }
            @Test
            public void test(){
                    System.out.println("请输入漫游车 1 的起始位置:(格式:x y E)");
                    String location1 = scanner.nextLine();
                    System.out.println("请输入漫游车 1 需要完成的命令:(格式:LRM这三个字母的组合)");
                    String command1 = scanner.next();
                    scanner.nextLine();
                    System.out.println("请输入漫游车 2 的起始位置:(格式:x y E)");
                    String location2 = scanner.nextLine();
                    System.out.println("请输入漫游车 2 需要完成的命令:(格式:LRM这三个字母的组合)");
                    String command2 = scanner.next();
                    System.out.println("漫游车 1 的开始执行。。");
                    myRun(location1,command1);
                    System.out.println("漫游车 2 的开始执行。。");
                    myRun(location2,command2);
            }
            public void myRun(String location,String command){
                    String[] split = location.split(" ");
                    if(split.length == 3 && split[2].length() == 1
                                    && "ESWN".contains(split[2])){
                            try {
                                    Integer x = getIntByString(split[0]);
                                    Integer y = getIntByString(split[1]);
                                    Integer num = getNum(split[2]);
                                    String success = move(x,y,num,command);
                                    System.out.println("执行成功,结果为:"+success);
                            } catch (Exception e) {
                                    System.out.println("您的输入不符合规范!!!。。。");
                            }
                    }else{
                            System.out.println("您的输入不符合规范!!!。。。");
                    }
            }
            /**
             *
             * @param x
             * @param y
             * @param num 代表方向(1,2,3,4)==》(E,S,W,N)
             * @param command
             * @return
             */
            public String move(Integer x,Integer y,Integer num,String command){
                    if(x <= X && y <= Y){
                            for (int i=0;i<command.length();i++) {
                                    if("L".equals(command.charAt(i)+"")){
                                            if(num==1){
                                                    num=4;
                                            }else{
                                                    num--;
                                            }
                                    }else if("R".equals(command.charAt(i)+"")){
                                            if(num==4){
                                                    num=1;
                                            }else{
                                                    num++;
                                            }
                                    }else if("M".equals(command.charAt(i)+"")){                                       
    switch(num){                                               
    case1:                                                        x
    ++;                                                       
    break;                                               
    case2:                                                        y
    --;                                                       
    break;                                               
    case3:                                                        x
    --;                                                           
    break;                                               
    case4:                                                        y
    ++;                                                       
    break;                                       
    }                                       
    if(x > X || y > Y){                                               
    String direction=getDirection(num);                                               
    return X +" "+ Y +" "+ direction;                                       
    }                               
    }                       
    }                       
    String direction=getDirection(num);                       
    return x +" "+ y +" "+ direction;               
    }               
    return"输入不合理,请检查。。";       
    }       
    publicString getDirection(Integer num){               
    String direction=null;               
    switch(num){                       
    case1:                                direction
    ="E";                               
    break;                       
    case2:                                direction
    ="S";                               
    break;                       
    case3:                                direction
    ="W";                                 
    break;                       
    case4:                                direction
    ="N";                               
    break;               
    }               
    return direction;       
    }       
    /**         * 将方向转化为整数类型方便后续  操作         * @param direction         * @return         */



           
    publicInteger getNum(String direction){               
    Integer num=null;               
    switch(direction){                       
    case"E":                                num
    =1;                               
    break;                       
    case"S":                                num
    =2;                               
    break;                       
    case"W":                                num
    =3;                                 
    break;                       
    case"N":                                num
    =4;                               
    break;               
    }               
    return num;       
    }       
    /**         * 将一个纯数字字符串转化为Integer类型         * @param s         * @return         */



           
    publicInteger getIntByString(String s){               
    returnInteger.valueOf(s);       
    }
    }
    如有疑问,欢迎留言讨论。
  • 相关阅读:
    laravel 的passport Oauth 认证登录请求 的 oauth_token 重置
    多个php版本的composer使用
    MySQL查询语句练习题(面试时可能会遇到哦!)
    tp5 url 线上访问 在nginx 上 出现404错误,解决办法(1.80nginx 配置 pathInfo)
    源码编译安装lnmp环境(nginx-1.14.2 + mysql-5.6.43 + php-5.6.30 )------踩了无数坑,重装了十几次服务器才会的,不容易啊!
    Mysql错误处理: /usr/bin/mysqld_safe: line xxx: xxxx Killed ... (mysql自动停止 Plugin FEDERATED is disabled 的完美解决方法)
    Plugin 'FEDERATED' is disabled. /usr/sbin/mysqld: Table 'mysql.plugin' doesn't exist
    thinkphp5的mkdir() Permission denied问题
    微信小程序 Unexpected end of JSON input/Unexpected token o in JSON at position 1
    服务器 apache配置https,http强制跳转https(搭建http与https共存)
  • 原文地址:https://www.cnblogs.com/zhhy/p/9492590.html
Copyright © 2011-2022 走看看