题目:
一队机器人要登录到一个平台上,并在平台上按指令行进。这个平台目前是矩形的,机器人在其上行进以执行任务。一组机器人会按顺序行动,第二个机器人在第一个没有结束时不会行动。机器人的位置由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
一队机器人要登录到一个平台上,并在平台上按指令行进。这个平台目前是矩形的,机器人在其上行进以执行任务。一组机器人会按顺序行动,第二个机器人在第一个没有结束时不会行动。机器人的位置由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
解决方案 »
- 一个流程图的图像表示转换为二维数组的实现方法问题,急!!!!
- 用myeclipse开发struts出的错误。。请高人解答。。急!!!
- 我有个问题想请教大家,刚刚接触计算机编程,对一些术语还是不怎么了解!
- jsp问题
- 刚刚的笔试题,大牛们帮我看看
- 如何使用 rmic 命令创建 *_stub.class文件????急盼复!!!
- 同时添加1000个元素,LinkedList比ArrayLis用的时间长
- 怎样指定JButton的位置,精确到坐标位置
- 怎么办??
- 字符串操作求教??字符串转换为字符数组,请介绍代码最少的办法,谢谢
- 急需jdk5.0注释学习资源
- 用swing做界面的内存溢出问题,大家来帮忙看看
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");
}}