题目:
一队机器人要登录到一个平台上,并在平台上按指令行进。这个平台目前是矩形的,机器人在其上行进以执行任务。一组机器人会按顺序行动,第二个机器人在第一个没有结束时不会行动。机器人的位置由X,Y坐标(整数)和一个代表朝向的字母(E,W,S,N)表示。平台可以认为是一个网格以简化导航。例如0,0,N,表示机器人在平台的左下角并朝向北方。为了控制一个机器人,控制中心会发送一组指令,这些指令由一个字符串表示,可能的字母是"L"-左转90度,不移动,"R"-右转90度,不移动,"M"-向前移动一格,朝向不变。例如"M"指令会让一个朝向N的机器人从(x,y)移动到(x,y+1)。测试输入数据的格式:
第一行是平台的右上角坐标,之后的每两行代表一个机器人,这两行中的第一行是机器人的着陆坐标和着陆朝向,第二行是控制中心发给该机器人的一组指令。期望输出数据的格式:
每个机器人最后的坐标和朝向测试输入:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM期望的输出:
1 3 N
5 1 E

解决方案 »

  1.   

    我是在linux下做的,linux与windows下的换行有点区别。。我这里是可以正确运行的如果有问题请查阅两个系统换行回车的区别并修改。。package com.xiaoyong;public class Robot {
    public final static String NORTH = "N";
    public final static String EAST = "E";
    public final static String SOUTH = "S";
    public final static String WEST = "W"; private int rows;
    private int cols;
    private String direction = NORTH; private int x;
    private int y; public int getRows() {
    return rows;
    } public void setRows(int rows) {
    this.rows = rows;
    } public int getCols() {
    return cols;
    } public void setCols(int cols) {
    this.cols = cols;
    } public String getDirection() {
    return direction;
    } public void setDirection(String dir) {
    if (dir.equals(NORTH) || dir.equals(SOUTH) || dir.equals(EAST)
    || dir.equals(WEST)) {
    direction = dir;
    return;
    }
    throw new IllegalArgumentException("Invalid direction");
    } 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;
    } private void trunLeft() {
    if (getDirection().equals(NORTH)) {
    setDirection(WEST);
    } else if (getDirection().equals(WEST)) {
    setDirection(SOUTH);
    } else if (getDirection().equals(SOUTH)) {
    setDirection(EAST);
    } else if (getDirection().equals(EAST)) {
    setDirection(NORTH);
    }
    } private void trunRight() {
    if (getDirection().equals(NORTH)) {
    setDirection(EAST);
    } else if (getDirection().equals(EAST)) {
    setDirection(SOUTH);
    } else if (getDirection().equals(SOUTH)) {
    setDirection(WEST);
    } else if (getDirection().equals(WEST)) {
    setDirection(NORTH);
    }
    } private void move() {
    if (getDirection().equals(NORTH)) {
    this.y++;
    } else if (getDirection().equals(SOUTH)) {
    this.y--;
    } else if (getDirection().equals(EAST)) {
    this.x++;
    } else if (getDirection().equals(WEST)) {
    this.x--;
    }
    } public String toString() {
    return this.getX() + " " + this.getY() + " " + this.getDirection();
    } public void parseOrders(String orders) {
    String[] strs = orders.split("\r\n");
    int size = strs.length;
    String[] rowAndCol = strs[0].split(" ");
    int row = Integer.parseInt(rowAndCol[0]);
    int col = Integer.parseInt(rowAndCol[1]);
    for (int i = 1; i < size; i += 2) {
    Robot robot = new Robot();
    robot.setRows(row);
    robot.setCols(col);
    String[] xAndy = strs[i].split(" ");
    robot.setX(Integer.parseInt(xAndy[0]));
    robot.setY(Integer.parseInt(xAndy[1]));
    robot.setDirection(xAndy[2]);
    String order = strs[i + 1];
    char[] os = order.toCharArray();
    int length = os.length;
    for (int j = 0; j < length; j++) {
    if (os[j] == 'L' || os[j] == 'l') {
    robot.trunLeft();
    } else if (os[j] == 'R' || os[j] == 'r') {
    robot.trunRight();
    } else {
    robot.move();
    }
    }
    System.out.println(robot.toString());
    }
    } public static void main(String[] args) {
    new Robot()
    .parseOrders("5 5\r\n1 2 N\r\nLMLMLMLMM\r\n3 3 E\r\nMMRMMRMRRM");
    }}