原题链接:蓝桥杯2014年第五届真题-兰顿蚂蚁
import java.util.Scanner;
class Ant {
int x;
int y;
int step;
String direction;
public Ant(int x, int y, int step, String direction) {
super();
this.x = x;
this.y = y;
this.step = step;
this.direction = direction;
}
}
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] a = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = scanner.nextInt();
}
}
int x = scanner.nextInt();
int y = scanner.nextInt();
String s = scanner.next();
int k = scanner.nextInt();
Ant ant = new Ant(x, y, 0, s);
while (k > 0) {
int colour = a[ant.x][ant.y]; // 判断当前蚂蚁所在格子的颜色 ,1为黑,0为白
int[] arr = check(colour, ant.direction); // 获取下一步移动的坐标
// System.out.println("(" + ant.x + " ," + ant.y + ")" + ",colour=" + colour);
if (arr[2] == 1) {
ant.direction = "R";
}
if (arr[2] == 2) {
ant.direction = "L";
}
if (arr[2] == 3) {
ant.direction = "U";
}
if (arr[2] == 4) {
ant.direction = "D";
}
if (colour == 1) { // 把当前的格子颜色反转
a[ant.x][ant.y] = 0;
}
if (colour == 0) {
a[ant.x][ant.y] = 1;
}
ant.x += arr[0];
ant.y += arr[1];
ant.step++;
k--;
}
System.out.println(ant.x + " " + ant.y);
}
private static int[] check(int colour, String direction) {
if (colour == 1) { // 在黑格 数组最后一个数字代表调转之后的方位
if (direction.equals("U")) { // 上
return new int[] { 0, 1, 1 }; // 上->右
} else if (direction.equals("D")) { // 下
return new int[] { 0, -1, 2 }; // 下->左
} else if (direction.equals("L")) { // 左
return new int[] { -1, 0, 3 }; // 左->上
} else if (direction.equals("R")) { // 右
return new int[] { 1, 0, 4 }; // 右->下
}
}
if (colour == 0) { // 在白格子
if (direction.equals("U")) { // 上
return new int[] { 0, -1, 2 };// 上->左
} else if (direction.equals("D")) { // 下
return new int[] { 0, 1, 1 }; // 下->右
} else if (direction.equals("L")) { // 左
return new int[] { 1, 0, 4 }; // 左->下
} else if (direction.equals("R")) { // 右
return new int[] { -1, 0, 3 }; // 右->上
}
}
return null;
}
}
9.9 分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复