日期:2014-05-20  浏览次数:20732 次

有个问题不是很明白,高手指教!
火星漫游者
描述:
 若干个火星漫游者(机器人)被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.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);