qq_43706170 2018-11-19 03:03 采纳率: 50%
浏览 1815
已结题

【感谢大佬】JAVA 小作业之智能小车【震惊】大学老师想把初学码农搞秃头!!??

Description
小车都是 1X1的圆形小车,可以在一个矩形仓库中行驶,小车会按照指令顺序行驶,一条指令结束后下一条指令才会开始执行。你需要编写程序,判断小车在执行指令的过程中,有没有异常(小车撞墙或小车之间发生碰撞)发生,当异常发生时需要输出一条一异常语句,并结束程序。当没有异常时需要输出Perfect

【感谢大佬请将代码发到邮箱3098555186@qq.com附带csdn用户名 ,可以使用我会采纳谢谢】

INPUT

图片说明

第一行输入两个整数 X,Y
1≤ X,Y≤100,1≤X,Y≤100,X代表 EW 方向的长度(米),Y 代表 NS 方向的长度(米)。如图所示的矩形仓库长宽分别为 5×4,当小车驶出矩形仓库即视为撞墙。

第二行输入两个整数 N,M。N 为小车的数量,M 为指令的条数。

下面的 N 行,每行有两个整数和一个字母,代表了小车的起始位置横纵坐标和车头朝向,N、S、W、E,四个字母代表如图所示四个不同的方向。(N 行中第 i 行表示编号为 i 的小车的起始位置和方向)。

最后 M 行为小车移动的指令。

指令格式:小车编号 指令符号 重复次数

1≤重复次数≤100
指令符号说明:
L:小车左转90度
R:小车右转90度
F:小车前进一米。(如2F3表示编号为2的小车按照原来的方向前进3米)

【感谢大佬请将代码发到邮箱3098555186@qq.com附带csdn用户名 ,可以使用我会采纳谢谢】

Output

输出一行结果:
在执行指令过程中
• 如果小车 i撞到了墙,则输出 小车i撞墙了
• 如果小车 i撞到了小车 j,则输出小车i撞了小车j
• 如果指令执行结束,并没有1,2情况发生,则输出Perfect

图片说明
图片说明
图片说明

【感谢大佬请将代码发到邮箱3098555186@qq.com附带csdn用户名 ,可以使用我会采纳谢谢】

Hint
对第三个测试样例的说明:
1 1 E 代表1号小车一开始位于横纵坐标为 (1,1) 的位置,车头朝E(东)。
1 L 96 代表1号小车连续做96次左转90°操作,1 F 2 代表1号小车前进1米的动作做两次。

非常感谢大佬们的鼎力相助!!

【感谢大佬请将代码发到邮箱3098555186@qq.com附带csdn用户名 ,可以使用我会采纳谢谢】

  • 写回答

5条回答 默认 最新

  • x060508 2018-11-19 06:49
    关注

    请采纳 谢谢 已经测试通过

     package com.csdn;
    
    import java.io.InputStream;
    import java.util.Scanner;
    
    public class Car {
    
        private int carNo; // 小车编号
        private int x;// 小车横坐标
        private int y;// 小车纵坐标
        private Direction direc;// 小车朝向 枚举
    
        public Car() {
            // TODO Auto-generated constructor stub
        }
    
        public Car(int carNo, int x, int y, Direction direc) {
            super();
            this.carNo = carNo;
            this.x = x;
            this.y = y;
            this.direc = direc;
        }
    
        public int getCarNo() {
            return carNo;
        }
    
        public void setCarNo(int carNo) {
            this.carNo = carNo;
        }
    
        public int getX() {
            return x;
        }
    
        public void setX(int x) {
            this.x = x;
        }
    
        public int getY() {
            return y;
        }
    
        public void setY(int y) {
            this.y = y;
        }
    
        public Direction getDirec() {
            return direc;
        }
    
        public void setDirec(Direction direc) {
            this.direc = direc;
        }
    
        // 移动一次 具体长度在 CarMap中定义
        public void move() {
    
            switch (direc.getDirec()) {
            case 0:
                y += 1;
                break;
            case 1:
                x += 1;
                break;
            case 2:
                y -= 1;
                break;
            case 3:
                x -= 1;
                break;
            default:
                break;
            }
        }
    
        /**
         * 
         * N0 W3 E1 S2
         * 
         * 例如 : 方向3 西方向 右旋转7次 相当于右旋转7%4=3 3+3=6 % 4 =2 所以最终是在2的位置上 也就是N
         * 
         * @param rotateDirec L 向左旋转90度 R 向右旋转09度
         * @param count       旋转次数
         * @return
         */
        public void rotate(String rotateDirec, int count) {
    
            switch (rotateDirec) {
            case "R":
    
                this.direc = Direction.getDirec((this.direc.getDirec() + (count % 4)) % 4);
                break;
    
            case "L":
                int resultDirec = this.direc.getDirec() - (count % 4);
                if (resultDirec >= 0) {
                    this.direc = Direction.getDirec(resultDirec);
                } else {
                    this.direc = Direction.getDirec(resultDirec + 4);
                }
                break;
            default:
                break;
            }
    
        }
    
        public static void main(String[] args) {
            Scanner in=new Scanner(System.in);
    
            String s=in.next();
            System.out.println(s);
        }
    
    }
    
    
     package com.csdn;
    
    /**
     * N0 W3 E1 S2
     * 
     * @Description: TODO(用一句话描述该文件做什么)
     * @author zhuangjiajian_Metka
     * @date 2018年11月19日 下午12:09:46
     * @version V1.0
     */
    public enum Direction {
    
        N(0, "N"), E(1, "E"), S(2, "S"), W(3, "W");
    
        private int direc;
        private String desc;
    
        private Direction(int direc, String desc) {
            this.direc = direc;
            this.desc = desc;
        }
    
        public int getDirec() {
            return direc;
        }
    
        public void setDirec(int direc) {
            this.direc = direc;
        }
    
        public String getDesc() {
            return desc;
        }
    
        public void setDesc(String desc) {
            this.desc = desc;
        }
    
        // 通过方向编号获取Direction枚举对象
        public static Direction getDirec(int direc) {
            for (Direction direction : Direction.values()) {
                if (direction.getDirec() == direc) {
                    return direction;
                }
            }
            return null;
        }
    
        // 通过方向字符串描述获取Direction枚举对象
        public static Direction getDirec(String direc) {
            for (Direction direction : Direction.values()) {
                if (direction.getDesc().equals(direc)) {
                    return direction;
                }
            }
            return null;
        }
    }
    
    
     package com.csdn;
    
    import java.util.ArrayList;
    import java.util.List;
    
    //车地图
    public class CarMap {
    
        private int width;//图长度
        private int height;//图高度
        private List<Car> cars=new ArrayList<>();//图上有很多个car
    
    
        public int getWidth() {
            return width;
        }
    
        public void setWidth(int width) {
            this.width = width;
        }
    
        public int getHeight() {
            return height;
        }
    
        public void setHeight(int height) {
            this.height = height;
        }
    
        public List<Car> getCars() {
            return cars;
        }
    
        public void setCars(List<Car> cars) {
            this.cars = cars;
        }
    
        /**
         * 
         * @param c 要旋转的car
         * @param d 旋转方向 L/R
         * @param count 旋转次数
         */
        public void acceptRotateCommond(Car c,String d,int count) {
            c.rotate(d, count);
        }
    
        public boolean acceptMoveCommond(Car c,int distance) {
            for(int i=0;i<distance;i++) {
                c.move();//每次移动一次就查看是否有发生碰撞或者撞墙事件
                if(c.getX()<0 || c.getX()>width || c.getY()<0 ||c.getY()>height) {
                    System.out.println("小车:" +c.getCarNo()+"撞墙了");
                    return false;
                }
                if(!updateStatus(c)) {
                    return false;
                }
            }
            return true;
        }
    
        public Car getCarByCarNo(int no) {
            for (Car car : cars) {
                if(car.getCarNo() == no) {
                    return car;
                }
            }
            return null;
        }
    
        private boolean updateStatus(Car currentCar) {
            // TODO Auto-generated method stub
            for (Car car : cars) {
                if(currentCar.getCarNo()==car.getCarNo()) {
                    break;//排除自身
                }
                if(currentCar.getX()==car.getX() && currentCar.getY()==currentCar.getY()) {
                    System.out.println("小车"+currentCar.getCarNo()+"撞了小车"+car.getCarNo());
                    return false;
                }
            }
    
            return true;
        }
    
    
    }
    
    
     package com.csdn;
    
    import java.util.Scanner;
    
    public class Clietn {
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
    
            System.out.println("请输入长X");
            int width = input.nextInt();
            System.out.println("请输入高Y");
            int height = input.nextInt();
    
            CarMap carMap = new CarMap();
            // 定义
            carMap.setWidth(width);
            carMap.setHeight(height);
    
            System.out.println("请输入整数 M-小车数目 ");
            int m = input.nextInt();
            System.out.println("请输入整数 N-指令数目");
            int n = input.nextInt();
    
            // 初始化小车的位置和方向
            for (int i = 0; i < m; i++) {
    
                int x = 0;
                do {
                    System.out.println("请输入x坐标 : 0=<X<=" + carMap.getWidth());
                    x = input.nextInt();
                } while (x < 0 || x > carMap.getWidth());
    
                int y = 0;
                do {
                    System.out.println("请输入y坐标 : 0=<Y<=" + carMap.getHeight());
                    y = input.nextInt();
                } while (y < 0 || y > carMap.getHeight());
    
                String d="";
                do {
                    System.out.println("请输入小车方向  N W S E");
                    d=input.next();
                } while ("NWSE".indexOf(d)<0);
    
                Car c=new Car();
    
                c.setCarNo(i+1);
                c.setX(x);
                c.setY(y);
                c.setDirec(Direction.getDirec(d)); //通过字符串 NWSE获取枚举对象
    
                carMap.getCars().add(c);
            }
    
            //接受小车指令 N条
            for (int i = 0; i < n; i++) {
                System.out.println("请输入指令[1-F-3]以-隔开");
                String commond=input.next();
                String commonds[]=commond.split("-");
                int carNo=Integer.parseInt(commonds[0]); //小车编号
                int count=Integer.parseInt(commonds[2]); //旋转次数或者移动的距离
    
                Car currentCar=carMap.getCarByCarNo(carNo);
    
                if(commonds[1].equals("F")) {
                    //移动命令
                    if(!carMap.acceptMoveCommond(currentCar, count)) {
                        return; //如果移动失败则结束程序
                    }
                }else {
                    //旋转命令
                    carMap.acceptRotateCommond(currentCar, commonds[1], count);
                }
            }
    
            System.out.println("Perfect");
        }
    
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突