兰顿蚂蚁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 stub
		for (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);  //所在位置输出
	}
}

 

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值