直接模拟兰顿蚂蚁的行动,按照规则改变
中出了一个bug,正在找到底是哪里的问题(也许不会去找_(:з」∠)_)
/*未完成
* 题目描述:兰顿蚂蚁 landun 输入格式 输入数据的第一行是 m n 两个整数(3 < m, n < 100),表示正方形格子的行数和列数。 接下来是
* m 行数据。 每行数据为 n 个被空格分开的数字。0 表示白格右转,1 表示黑格左转。 接下来是一行数据:x y s k, 其中x
* y为整数,表示蚂蚁所在行号和列号(行号从上到下增长,列号从左到右增长,都是从0开始编号)。s
* 是一个大写字母,表示蚂蚁头的朝向,我们约定:上下左右分别用:UDLR表示。k 表示蚂蚁走的步数。 输出格式 输出数据为两个空格分开的整数 p q,
* 分别表示蚂蚁在k步后,所处格子的行号和列号。
*/
class landun {
public static void go(Scanner in) {
int l = in.nextInt();
int w = in.nextInt();
boolean[][] map = new boolean[l][w];
for (boolean[] f : map)
for (boolean fo : f)
fo = in.nextInt() == 0 ? false : true;
// 0是false,1是true
int x = in.nextInt() - 1;
int y = in.nextInt() - 1;
String s = in.next();
char ch = s.charAt(0);
int face = -1;
if (ch == 'L')
face = 0;
if (ch == 'U')
face = 1;
if (ch == 'R')
face = 2;
if (ch == 'U')
face = 3;
// 0,1,2,3=>左上右下(如果记得mc的数据值就按mc的规则赋值了TAT)
int foo = in.nextInt();
for (int i = foo; i > 0; i--) {
// System.out.println((x + 1) + "," + (y + 1) + "|" + face);
try {// 注意有没有数组越界
if (map[x][y]) {// 1.true,左转
face += face == 0 ? 3 : -1;
} else {// 0.false,右转
face += face == 3 ? -3 : 1;
}
} catch (ArrayIndexOutOfBoundsException rua) {
if (x < 0)
x += 1;
if (x > l)
x -= 1;
if (y < 0)
y += 1;
if (y > w)
y -= 1;
}
switch (face) {// 前进!
case 0:
y -= 1;
break;
case 1:
x -= 1;
break;
case 2:
y += 1;
break;
case 3:
x += 1;
break;
}
}
System.out.println(x + " " + y);
}
}