最近在找工作中, 遇到的一些笔试题
题目:
一队机器人漫游车将被美国宇航局降落在火星高原上。漫游车将在这个奇怪的长方形高原上巡游,以便他们的机载摄像头可以获得周围地形的完整视图,并将其发送回地球。漫游者的坐标和位置由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 cn.itcast.eneity;
```
public class Vehicle {
private int x;//X轴坐标
private int y;//y轴坐标
private String direction; //方向
private int max_x;//最大X值
private int max_y;//最大y值
public Vehicle() {
}
public Vehicle(int x, int y, String direction) {
this.x = x;
this.y = y;
this.direction = direction.trim();
}
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 int getMax_x() {
return max_x;
}
public void setMax_x(int max_x) {
this.max_x = max_x;
}
public int getMax_y() {
return max_y;
}
public void setMax_y(int max_y) {
this.max_y = max_y;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
//前进一步
public void advance(){
if("E".equals(this.direction)){
this.x += 1;
}else if ("S".equals(this.direction)) {
this.y -= 1;
}else if ("W".equals(this.direction)) {
this.x -= 1;
}else if ("N".equals(this.direction)) {
this.y += 1;
}
}
//想左转
public void gotoLeft(){
if("E".equals(this.direction)){
this.direction = "N";
}else if ("S".equals(this.direction)) {
this.direction = "E";
}else if ("W".equals(this.direction)) {
this.direction = "S";
}else if ("N".equals(this.direction)) {
this.direction = "W";
}
}
//N - North北 S - South南 E - East东 W - West西
//想右转
public void gotoRight(){
if("E".equals(this.direction)){
this.direction = "S";
}else if ("S".equals(this.direction)) {
this.direction = "W";
}else if ("W".equals(this.direction)) {
this.direction = "N";
}else if ("N".equals(this.direction)) {
this.direction = "E";
}
}
}
package cn.itcast.eneity;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Test {
static int Max_x;
static int Max_y;
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
//创建车的集合
List<Vehicle> vehicleList = new ArrayList<Vehicle>();
//输出
System.out.println("请输入平台右上角坐标: ");
Max_x = sc.nextInt();
Max_y = sc.nextInt();
System.out.println("你输入的平台右上角坐标为: (" + Max_x +","+ Max_y +"), 确定请输入YES");
while (sc.next().equals("YES")) {
System.out.println("请输入车的坐标及方向, N ==> 北; S ==> 南; E ==> 东;W ==> 西");
int x = sc.nextInt();
if(x<0 || x>Max_x){
System.out.println("X输入错误");
return;
}
int y = sc.nextInt();
if(y<0 || y>Max_y){
System.out.println("Y输入错误");
return;
}
String direction = sc.next();
if (!("E".equals(direction) || "S".equals(direction) || "W".equals(direction) || "N".equals(direction))) {
System.out.println("方向输入错误");
return;
}
String path = sc.next();
if("".equals(path) || path.length()<0){
System.out.println("运动路径输入错误");
return;
}
System.out.println("车输入的坐标为:("+ x +","+ y +"),方向为:"+ direction +",运动路径为:"+ path);
Run(x,y,direction,vehicleList,path);
for (int i=0;i<vehicleList.size();i++){
System.out.println(vehicleList.get(i).getX()+" "+vehicleList.get(i).getY()+" "+vehicleList.get(i).getDirection());
}
}
}
private static void Run(int x, int y, String direction, List<Vehicle> vehicleList, String path) {
Vehicle vehicle = new Vehicle();
vehicle.setDirection(direction);
vehicle.setX(x);
vehicle.setY(y);
for (int i=0;i<path.length();i++){
if("M".equals(String.valueOf(path.charAt(i)))){
vehicle.advance();
}
if("L".equals(String.valueOf(path.charAt(i)))){
vehicle.gotoLeft();
}
if("R".equals(String.valueOf(path.charAt(i)))){
vehicle.gotoRight();
}
}
vehicleList.add(vehicle);
return;
}
}
未通过...