火星漫游者
描述:
 若干个火星漫游者(机器人)被NASA(美国国家航空和宇宙航行局)送到火星对火星的一个高原进行科学探测,这个高原是一个矩形,火星漫游者将漫游整个高原,并通过照相机对高原拍照,将照片发回地球,以了解这个高原的全貌。
 任意一个漫游者的位置将由x,y坐标和一个代表方向的字母确定(N-北,S-南,E-东,W-西),为了简化漫游,将高原划分为网格。例如(0,0,N)表示火星漫游者位于高原的左下角,方向朝北。
 NASA通过发送命令字符串控制火星漫游者,命令字符串是字符’L’,’R’,’M’的组合,’L’,’R’表示使漫游者向左或右旋转90°,但坐标不变;’M’表示使漫游者沿当前方向移动一个网格点,并保持方向不变。
 假设高原的正北方向是从点(x,y)到点(x,y+1)的方向
输入:
 输入采用文件,文件第一行是高原的右上角坐标,左下角坐标默认为(0,0)
 文件的剩余部分是每一个火星漫游者的信息,每一个火星漫游者有两行信息,第一行给出漫游者的初始位置,第二行是一系列控制漫游者的命令
 位置由两个整数和一个字母组成并用空格分隔,分别对应x,y坐标和方向
 每个漫游者按顺序漫游高原,即下一个漫游者必须在上一个漫游者结束漫游后才开始漫游。
输出:
 输出每一个漫游者的最终坐标和方向
输入输出示例:
 Test Input:
5 5
1 2 N
LMLMLMLMM
3 3 E
MMRMMRMRRM

 Expected Output:
1 3 N
5 1 E
我这么编的,但得不出要求结果来,哪里错了?高手指教
 import java.io.*;
 class GaoYuan{
  int x;int y;
  public GaoYuan(int x,int y){
    this.x = x;this.y = y;
  }
 }
 class Explorer{
   public int x;
   public int y;
   public char a;
   public Explorer(int x,int y,char a){
     this.x = x;
     this.y = y;
     this.a = a;
   }
   public void L(){
     if ('N'==(this.a)){
        this.a = 'W';
     }
     else if ('W'==(this.a)){
        this.a = 'S';
     }
     else if ('S'==(this.a)){
        this.a = 'E';
     }
     else if ('E'==(this.a)){
        this.a = 'N';
    }
   }
   public void R(){
    if ('N'==(this.a)){
        this.a = 'E';
     }
     else if ('E'==(this.a)){
        this.a = 'S';
     }
     else if ('S'==(this.a)){
        this.a = 'W';
     }
     else if ('W'==(this.a)){
        this.a = 'N';
    }
  }
  public void M(){
     if ('N'==(this.a)){
        this.y = +1;
     }
     else if ('W'==(this.a)){
        this.x = -1;
     }
     else if ('S'==(this.a)){
        this.y = -1;
     }
     else if ('E'==(this.a)){
        this.a = +1;
    }
  }
 }
 public class ExplorerMove{
    public static void main (String[] args) throws IOException{
    String temp;
    BufferedReader bufferedReader = new BufferedReader (new
InputStreamReader(System.in));
    temp = bufferedReader.readLine();
    int x = int(temp.charAt(0)-'0');
    int y = int(temp.charAt(1)-'0');
    GaoYuan gaoYuan = new GaoYuan(x,y);
   for (int i=1;i>=0;i--){
    temp = bufferedReader.readLine();
    int x1  = int(temp.charAt(0)-'0');
    int y1  = int(temp.charAt(1)-'0');
    char a  =  temp.charAt(2);
    Explorer explorer = new Explorer(x1,y1,a);
    temp = bufferedReader.readLine();
    for (int j=0;j<temp.length();j++){
      if ('L'==(temp.charAt(j))){
        explorer.L();
      }
      if ('R'==(temp.charAt(j))){
        explorer.R();
      }
      if ('M'==(temp.charAt(j))){
        explorer.M();
      }
    }
    System.out.println(explorer.x+""+explorer.y+""+explorer.a);
   }
  }
 }

解决方案 »

  1.   

    好了,帮你搞定了,程序如下:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;class GaoYuan {
        int x;
        int y;    public GaoYuan(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }class Explorer {
        public int x;
        public int y;
        public char a;    public Explorer(int x, int y, char a) {
            this.x = x;
            this.y = y;
            this.a = a;
        }    public void L() {
            if ('N' == a) {
                this.a = 'W';
            } else if ('W' == a) {
                this.a = 'S';
            } else if ('S' == a) {
                this.a = 'E';
            } else if ('E' == a) {
                this.a = 'N';
            }
        }    public void R() {
            if ('N' == a) {
                this.a = 'E';
            } else if ('E' == a) {
                this.a = 'S';
            } else if ('S' == a) {
                this.a = 'W';
            } else if ('W' == a) {
                this.a = 'N';
            }
        }    public void M() {
            if ('N' == a) {
                this.y += 1;
            } else if ('W' == a) {
                this.x -= 1;
            } else if ('S' == a) {
                this.y -= 1;
            } else if ('E' == a) {
                this.x += 1;
            }
        }
        
        public String toString() {
            return x + " " + y + " " + a;
        }
    }public class ExplorerMove {
        public static void main(String[] args) throws IOException {
            String temp;
            StringTokenizer st;
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(System.in));
            temp = bufferedReader.readLine();
            //st = new StringTokenizer(temp);
            //int x = Integer.valueOf(st.nextToken());
            //int y = Integer.valueOf(st.nextToken());
            //GaoYuan gaoYuan = new GaoYuan(x, y);
            for (int i = 1; i >= 0; i--) {
                temp = bufferedReader.readLine();
                st = new StringTokenizer(temp);
                int x1 = Integer.valueOf(st.nextToken());
                int y1 = Integer.valueOf(st.nextToken());
                char a = st.nextToken().charAt(0);
                Explorer explorer = new Explorer(x1, y1, a);
                temp = bufferedReader.readLine();
                for (int j = 0; j < temp.length(); j++) {
                    if ('L' == (temp.charAt(j))) {
                        explorer.L();
                    } else if ('R' == (temp.charAt(j))) {
                        explorer.R();
                    } else if ('M' == (temp.charAt(j))) {
                        explorer.M();
                    }
                }
                System.out.println(explorer);
            }
        }
    }说明:
    1、考虑到边界可能不只是5、5这样的个位数,故不能用chatAt方法读取,改成程序中的StringTokenizer方法来分割;
    2、程序中类似于this.y = +1;这样的地方都是错的,应该改成this.y += 1;
    3、this.a = +1;是错的,改成this.x += 1;(2和3的this我在程序中都去掉了)
    4、//int x = Integer.valueOf(st.nextToken());
       //int y = Integer.valueOf(st.nextToken());
       //GaoYuan gaoYuan = new GaoYuan(x, y);
    这几行代码程序中没用到,所以我注释掉了。当然,程序中也没用到GaoYuan这个类。
    5、如果设计得更好一些,还有一些地方需要提高,包括移动的过程中对于是否越界的判断和处理,包括NWSEM等字母大小写的兼容,包括对异常情况的处理等等。
      

  2.   

    我测试过,没问题。当然,前提是严格按照题目中给的输入数据,空格不能去掉(否则,如果第一行输入155,这表示是15行5列呢还是1行55列?)。即:
    5 5
    1 2 N
    LMLMLMLMM
    3 3 E
    MMRMMRMRRM
      

  3.   

    唉,看看我的源程序,我真觉得自己很差,竟然出了这种低级错误!其实想想不用StringTokenizer我的源程序是能运行出正确结果的,虽然不严谨!自己还是太粗心了!希望高手以后多帮帮我。