应邀接链接
http://community.csdn.net/Expert/topic/5566/5566851.xml?temp=.9874842

解决方案 »

  1.   

    输入 d盘的input.txt
    5 5
    1 2 N
    LMLMLMLMM
    3 3 E
    MMRMMRMRRM
    输出
    d盘的output.txt
    1 3 N
    5 1 E
    ========================================
    MarsRoamerTest.java
    ============================
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    public class MarsRoamerTest {
        
        /** Creates a new instance of MarsRoamerTest */
        public MarsRoamerTest() {
            
        }
        //main函数
        public static void main(String[] args){
            //用一个数组存放4个方向
            String[] directionArray = {"N","E","S","W"};
            //新建一个List存放火星漫游者,因为不知道具体有多少个,所以不宜用定长数组
            List list = new ArrayList();
            try {
                FileReader fr = new FileReader("D:\\input.txt"); //path 为输入文件的绝对路径
                BufferedReader br = new BufferedReader(fr);
                /*逐行读取*/
                String line = br.readLine();
                //读取出来后, 用split分割成String[]
                String[] strTopRight = line.split(" ");
                //根据第一行分割出来的String,得到(right,top)的值,也就是右上角的坐标
                int top = Integer.parseInt(strTopRight[1]);
                int right = Integer.parseInt(strTopRight[0]);
                //用while循环不断按行读取,直到读出的为null
                while (line != null) {
                    line = br.readLine();
                    if( line == null) break;
                    //读取出来后, 用split分割成String[]
                    String[] strDirection = line.split(" ");
                    //取得火星漫游者的x,y坐标
                    int marsRoamerX = Integer.parseInt(strDirection[0]);
                    int marsRoamerY = Integer.parseInt(strDirection[1]);
                    //取得火星漫游者的初始化方向, 1:N 2:E 3:S 4W 顺时针方向
                    int marsRoamerDirection = 1;
                    for(int i = 0;i < directionArray.length;i++)
                    {
                        if(strDirection[2].equals(directionArray[i]) ){
                            marsRoamerDirection = i +1;
                            break;
                        }
                    }
                    //根据取得的数据,新建立一个火星漫游者对象
                    MarsRoamer marsRoamer = new MarsRoamer(marsRoamerX,marsRoamerY,marsRoamerDirection,top,right);
                    //输出火星漫游者的初始化信息,测试用
                    //System.out.println("x,y" + marsRoamer.getX() + "," + marsRoamer.getY() + " " + marsRoamer.getDirection());
                    line = br.readLine();
                    if( line == null) break;
                    //执行一系列命令
                    marsRoamer.operations(line.trim().replaceAll(" ",""));
                    //将火星漫游者对象放入List中,在输出时用到
                    list.add(marsRoamer);
                    //将marsRoamer指向null,准备在下一循环中指向新的火星漫游者对象
                    marsRoamer = null;
                }
                //关闭流
                br.close();
                fr.close();
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            //遍历List,将火星漫游者的最终位置输出到文件中去
             PrintWriter pw = null;
            try{
                pw = new PrintWriter(new FileWriter("D:\\output.txt"));//设置输出文件的位置
                for(int i = 0;i < list.size();i++){
                MarsRoamer  mr = (MarsRoamer)list.get(i);
                String strDirection = directionArray[mr.getDirection()-1];   
                //在命令行输出
                //System.out.println(mr.getX() + " " + mr.getY() + " " + strDirection);
                //写入输出文件
                pw.println(mr.getX() + " " + mr.getY() + " " + strDirection);
                }
               
            }
             catch(Exception ex){
                ex.printStackTrace();
            } finally{
                if( pw != null){
                    pw.close();
                }
            }
        }
        
    }//火星漫游者类,作为内部类使用
    class MarsRoamer{
        //左下角为0,0, right,top为右上角坐标,在执行前进命令时,必须在保证不会超出高原范围的前提下,命令才执行
        private int top;
        private int right;
        private int x;
        private int y;
        private int direction;
        //direction: 1 N 北 2 E 东 3 S 南 4 E 西
        //构造函数
        public MarsRoamer(int x, int y, int direction,int top,int right){
            this.x = x;
            this.y = y;
            this.direction = direction;
            this.top = top;
            this.right = right;
        }
        //取得火星漫游者x坐标
        public int getX(){
            return this.x;
        }
        //取得火星漫游者y坐标
        public int getY(){
            return this.y;
        }
        //取得火星漫游者方向 1 N 北 2 E 东 3 S 南 4 E 西
        public int getDirection(){
            return this.direction;
        }
        // 1 L  2 R  3 M 执行命令
        public void operation(int type){
            switch(type){
                //左转命令
                case 1:
                    direction--;
                    if( direction < 1){
                        direction = 4;
                    }
                    break;
                 //右转命令
                case 2:
                    direction++;
                    if( direction > 4){
                        direction = 1;
                    }
                    break;
                //前进命令
                case 3:
                    switch(direction){
                        //向北走一步
                        case 1:
                            if(y+1 <= top)
                                y++;
                            break;
                         //向东走一步
                        case 2:
                            if(x+1 <= right)
                                x++;
                            break;
                        //向南走一步
                        case 3:
                            if( y-1 >= 0)
                                y--;
                            break;
                        //向西走一步
                        case 4:
                            if( x-1 >=0)
                                x--;
                            break;
                        default:
                            break;
                    }//swith(direction)
                    break;//case type 3
                default:
                    break;
                    
            }//switch(type)
        }
        //执行一系列命令
        public void operations(String strOperation){
            String[] operationArray = {"L","R","M"};
            if( strOperation.trim().length() <=0) {
                return; //你别搞乱!
            } else{
                for(int i = 0;i < strOperation.length();i++){
                     String strTemp = strOperation.substring(i,i+1);
                    for(int j = 0; j < operationArray.length;j++){
                        if(strTemp.equals(operationArray[j]) ){
                            int type = j + 1;
                            operation(type);
                        }
                    }
                }
            }
            
        }
    }
      

  2.   

    描述:
    &#61550;若干个火星漫游者(机器人)被NASA(美国国家航空和宇宙航行局)送到火星对火星的一个高原进行科学探测,这个高原是一个矩形,火星漫游者将漫游整个高原,并通过照相机对高原拍照,将照片发回地球,以了解这个高原的全貌。
    &#61550;任意一个漫游者的位置将由x,y坐标和一个代表方向的字母确定(N-北,S-南,E-东,W-西),为了简化漫游,将高原划分为网格。例如(0,0,N)表示火星漫游者位于高原的左下角,方向朝北。
    &#61550;NASA通过发送命令字符串控制火星漫游者,命令字符串是字符’L’,’R’,’M’的组合,’L’,’R’表示使漫游者向左或右旋转90°,但坐标不变;’M’表示使漫游者沿当前方向移动一个网格点,并保持方向不变。
    &#61550;假设高原的正北方向是从点(x,y)到点(x,y+1)的方向
    输入:
    &#61550;输入采用文件,文件第一行是高原的右上角坐标,左下角坐标默认为(0,0)
    &#61550;文件的剩余部分是每一个火星漫游者的信息,每一个火星漫游者有两行信息,第一行给出漫游者的初始位置,第二行是一系列控制漫游者的命令
    &#61550;位置由两个整数和一个字母组成并用空格分隔,分别对应x,y坐标和方向
    &#61550;每个漫游者按顺序漫游高原,即下一个漫游者必须在上一个漫游者结束漫游后才开始漫游。
    输出:
    &#61550;输出每一个漫游者的最终坐标和方向
    输入输出示例:
    &#61550;Test Input:
    5 5
    1 2 N
    LMLMLMLMM
    3 3 E
    MMRMMRMRRM&#61550;Expected Output:
    1 3 N
    5 1 E