本文主要是介绍兰顿蚂蚁java,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
思路:
首先要确定蚂蚁的“坐标”,“头所在方位”,和所在格子的"颜色",然后根据其移动规则进行移动,根据所要移动的步数确定循环的次数哦b( ̄▽ ̄)d
import java.util.*;public class Main {static int[][] num;static String direction;public static void main(String[] args) {Scanner reader = new Scanner(System.in);int m = reader.nextInt();// 行数int n = reader.nextInt();// 列数num = new int[m][n];for (int i = 0; i < m; i++) {for (int j = 0; j < n; j++) {num[i][j] = reader.nextInt();// 给矩阵赋值}}int x = reader.nextInt(); //横坐标int y = reader.nextInt(); //纵坐标direction = reader.next(); //初始方向int step = reader.nextInt();//需要走的步骤remove(x, y, step); //进行移动}private static void remove(int x, int y, int step) {// TODO Auto-generated method stubfor (int i = 0; i < step; i++) {if (num[x][y] == 1) {// 黑格switch (direction) {case "U":direction = "R";//重设方向num[x][y] = 0; //变为白格y += 1; //位置移动break;case "D":direction = "L";num[x][y] = 0;y -= 1;break;case "L":direction = "U";num[x][y] = 0;x -= 1;break;case "R":direction = "D";num[x][y] = 0;x += 1;break;}} else {// 白格switch (direction) {case "U":direction = "L";//重设方向num[x][y] = 1; //变为黑格y -= 1; //移动位置break;case "D":direction = "R";num[x][y] = 1;y += 1;break;case "L":direction = "D";num[x][y] = 1;x += 1;break;case "R":direction = "U";num[x][y] = 1;x -= 1;break;}}}System.out.println(x + " " + y); //所在位置输出}
}
这篇关于兰顿蚂蚁java的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!