解题思路:就是控制蚂蚁转方向和更新方块
注意事项:
参考代码:
import java.util.Scanner; public class 兰顿蚂蚁 { private static int fangxiang; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); //因为蚂蚁是不会一直转圈的,只要有四个URDL就能保证蚂蚁的四个方向切换 //--就是向左转,++就是向右转 char[] farr = {'U', 'R', 'D', 'L', 'U', 'R', 'D', 'L', 'U', 'R', 'D', 'L', 'U', 'R', 'D', 'L'}; int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = sc.nextInt(); } } int x = sc.nextInt(); int y = sc.nextInt(); char s = sc.next().charAt(0); for (int i = 8; i < 13; i++) { if (s == farr[i]) { fangxiang = i; } } int k = sc.nextInt(); int temp = 0; while (temp < k) { if (arr[x][y] == 1) { fangxiang++; arr[x][y] = 0; } else if (arr[x][y] == 0) { fangxiang--; arr[x][y] = 1; } if (farr[fangxiang] == 'U') x--; if (farr[fangxiang] == 'R') y++; if (farr[fangxiang] == 'D') x++; if (farr[fangxiang] == 'L') y--; temp++; } System.out.println(x + " " + y); } }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题6.8 (C++代码)浏览:682 |
printf基础练习2 (C语言代码)浏览:768 |
C语言程序设计教程(第三版)课后习题7.4 (C++代码)(和输入10个数排序没有区别,直接sort即可)浏览:2098 |
C语言程序设计教程(第三版)课后习题10.7 (C语言代码)浏览:528 |
【绝对值排序】 (C语言代码)浏览:717 |
最长单词 (C语言代码)浏览:1380 |
蛇行矩阵 (C语言代码)浏览:536 |
简单的a+b (C语言代码)浏览:546 |
字符串输入输出函数 (C语言代码)浏览:2495 |
C语言程序设计教程(第三版)课后习题12.2 (C语言代码)浏览:768 |