1. import java.util.Scanner;
  2. class Ant {
  3. int x;
  4. int y;
  5. int step;
  6. String direction;
  7. public Ant(int x, int y, int step, String direction) {
  8. super();
  9. this.x = x;
  10. this.y = y;
  11. this.step = step;
  12. this.direction = direction;
  13. }
  14. }
  15. public class Main {
  16. public static void main(String[] args) {
  17. Scanner scanner = new Scanner(System.in);
  18. int m = scanner.nextInt();
  19. int n = scanner.nextInt();
  20. int[][] a = new int[m][n];
  21. for (int i = 0; i < m; i++) {
  22. for (int j = 0; j < n; j++) {
  23. a[i][j] = scanner.nextInt();
  24. }
  25. }
  26. int x = scanner.nextInt();
  27. int y = scanner.nextInt();
  28. String s = scanner.next();
  29. int k = scanner.nextInt();
  30. Ant ant = new Ant(x, y, 0, s);
  31. while (k > 0) {
  32. int colour = a[ant.x][ant.y]; // 判断当前蚂蚁所在格子的颜色 ,1为黑,0为白
  33. int[] arr = check(colour, ant.direction); // 获取下一步移动的坐标
  34. // System.out.println("(" + ant.x + " ," + ant.y + ")" + ",colour=" + colour);
  35. if (arr[2] == 1) {
  36. ant.direction = "R";
  37. }
  38. if (arr[2] == 2) {
  39. ant.direction = "L";
  40. }
  41. if (arr[2] == 3) {
  42. ant.direction = "U";
  43. }
  44. if (arr[2] == 4) {
  45. ant.direction = "D";
  46. }
  47. if (colour == 1) { // 把当前的格子颜色反转
  48. a[ant.x][ant.y] = 0;
  49. }
  50. if (colour == 0) {
  51. a[ant.x][ant.y] = 1;
  52. }
  53. ant.x += arr[0];
  54. ant.y += arr[1];
  55. ant.step++;
  56. k--;
  57. }
  58. System.out.println(ant.x + " " + ant.y);
  59. }
  60. private static int[] check(int colour, String direction) {
  61. if (colour == 1) { // 在黑格 数组最后一个数字代表调转之后的方位
  62. if (direction.equals("U")) { // 上
  63. return new int[] { 0, 1, 1 }; // 上->右
  64. } else if (direction.equals("D")) { // 下
  65. return new int[] { 0, -1, 2 }; // 下->左
  66. } else if (direction.equals("L")) { // 左
  67. return new int[] { -1, 0, 3 }; // 左->上
  68. } else if (direction.equals("R")) { // 右
  69. return new int[] { 1, 0, 4 }; // 右->下
  70. }
  71. }
  72. if (colour == 0) { // 在白格子
  73. if (direction.equals("U")) { // 上
  74. return new int[] { 0, -1, 2 };// 上->左
  75. } else if (direction.equals("D")) { // 下
  76. return new int[] { 0, 1, 1 }; // 下->右
  77. } else if (direction.equals("L")) { // 左
  78. return new int[] { 1, 0, 4 }; // 左->下
  79. } else if (direction.equals("R")) { // 右
  80. return new int[] { -1, 0, 3 }; // 右->上
  81. }
  82. }
  83. return null;
  84. }
  85. }
点赞(0)
 

9.9 分

1 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论