原题链接:蓝桥杯2014年第五届真题-兰顿蚂蚁
解题思路:
参考代码:
import java.util.Scanner;
public class Main {
static int arr[][];
static int row;
static int col;
static char dir;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m = sc.nextInt(); //方格的行数
int n = sc.nextInt(); //方格的列数
arr = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
arr[i][j] = sc.nextInt();
}
}
row = sc.nextInt(); //蚂蚁所在的行号
col = sc.nextInt(); //蚂蚁所在的列号
dir = sc.next().charAt(0); //蚂蚁方向
int k = sc.nextInt();
//k蚂蚁的步数循环蚂蚁的步数
for (int i = 1; i <=k; i++) {
move();
}
System.out.println(row + " " + col);
}
public static void move()
{
//若蚂蚁在黑格,右转90度,将该格改为白格
if (arr[row][col] == 1) {
if (dir == 'U') dir = 'R';
else if (dir == 'R') dir = 'D';
else if (dir == 'D') dir = 'L';
else if (dir == 'L') dir = 'U';
arr[row][col] = 0;
}
//若蚂蚁在白格,左转90度,将该格改为黑格
else if (arr[row][col] == 0)
{
if (dir == 'U') dir = 'L';
else if (dir == 'L') dir = 'D';
else if (dir == 'D') dir = 'R';
else if (dir == 'R') dir = 'U';
arr[row][col]=1;
}
//移动一格
if (dir == 'U') row--;
else if (dir == 'R') col++;
else if (dir == 'D') row++;
else if (dir == 'L') col--;
}
}0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复