历届试题 兰顿蚂蚁

时间限制:1.0s 内存限制:256.0MB

提交此题

问题描述

兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种。

平面上的正方形格子被填上黑色或白色。在其中一格正方形内有一只“蚂蚁”。

  蚂蚁的头部朝向为:上下左右其中一方。

蚂蚁的移动规则十分简单:

  若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格;

  若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格。

规则虽然简单,蚂蚁的行为却十分复杂。刚刚开始时留下的路线都会有接近对称,像是会重复,但不论起始状态如何,蚂蚁经过漫长的混乱活动后,会开辟出一条规则的“高速公路”。

蚂蚁的路线是很难事先预测的。

你的任务是根据初始状态,用计算机模拟兰顿蚂蚁在第n步行走后所处的位置。

输入格式

  输入数据的第一行是 m n 两个整数(3 < m, n < 100),表示正方形格子的行数和列数。

  接下来是 m 行数据。

  每行数据为 n 个被空格分开的数字。0 表示白格,1 表示黑格。

接下来是一行数据:x y s k, 其中x y为整数,表示蚂蚁所在行号和列号(行号从上到下增长,列号从左到右增长,都是从0开始编号)。s 是一个大写字母,表示蚂蚁头的朝向,我们约定:上下左右分别用:UDLR表示。k 表示蚂蚁走的步数。

输出格式

  输出数据为两个空格分开的整数 p q, 分别表示蚂蚁在k步后,所处格子的行号和列号。

样例输入

5 6

0 0 0 0 0 0

0 0 0 0 0 0

0 0 1 0 0 0

0 0 0 0 0 0

0 0 0 0 0 0

2 3 L 5

样例输出

1 3

样例输入

3 3

0 0 0

1 1 1

1 1 1

1 1 U 6

样例输出

0 0

import java.util.Scanner;

public class 兰顿蚂蚁 {
public static void main(String[] args) {
//获取数据
Scanner input=new Scanner(System.in);
int row=input.nextInt();//获取行
int column=input.nextInt(); //获取列 int arr[][]=new int[row][column];//获取数组内容
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
arr[i][j]=input.nextInt();
}
}
int r=input.nextInt(); //蚂蚁行
int c=input.nextInt(); //蚂蚁列
String temp=input.next();//方向
int t=0;
if(temp.equals("U")){
t=0;
}else if(temp.equals("R")){
t=1;
}else if(temp.equals("D")){
t=2;
}else{
t=3;
}
Ant ant=new Ant(r,c,t);
int step=input.nextInt();//步数 //用for代表走的步数
for (int i = 0; i < step; i++) {
//行走方法
Go(ant,arr);
}
//输出蚂蚁的位置
System.out.println(ant.r+" "+ant.c);
} //写一个类,包含蚂蚁的位置,方向
public static void Go(Ant ant,int arr[][]) {
// TODO Auto-generated method stub
if(arr[ant.r][ant.c]==1){
ant.aspect=(ant.aspect+1)%4; //方向改变
arr[ant.r][ant.c]=0; //格子颜色改变
Run(ant); //蚂蚁行走
}else{
ant.aspect=(ant.aspect+3)%4;
arr[ant.r][ant.c]=1;
Run(ant);
} } private static void Run(Ant ant) {
if(ant.aspect==0){
ant.r--;
}else if(ant.aspect==1){
ant.c++;
}else if(ant.aspect==2){
ant.r++;
}else{
ant.c--;
}
} }
class Ant{
public int aspect; //方向
public int r=0; //蚂蚁的行
public int c=0; //列
public Ant(int r,int c,int aspect) {
this.aspect=aspect;
this.r=r;
this.c=c;
} }

最新文章

  1. 在PHP中使用CURL
  2. java实现调用ORACLE中的游标和包
  3. 生产环境下一定要开启mysqlbinlog
  4. 个人对maven pom.xml文件的理解
  5. 在iPhoneApp中加载PDF
  6. poj 3278 Catch That Cow bfs
  7. 如何安装Orchard
  8. icon button样式(类似windows桌面图标)
  9. python学习笔记 函数
  10. python 生成18年写过的博客词云
  11. RedHat Linux关闭seLinux命令
  12. 从Joda-Time反观Java语言利弊
  13. 古堡算式|2012年蓝桥杯B组题解析第二题-fishers
  14. Oracle ORA-00911: 无效字符
  15. 【转】android系统常用URI
  16. CSS动画简介
  17. 两条命令在Linux主机之间建立信任关系
  18. webpack4 优化记录
  19. .NetCore简介
  20. PHP - 输出缓存 - 关于ob系列函数和flush函数

热门文章

  1. 记一次 spinor flash 读速度优化
  2. 你应该知道的Python3.6、3.7、3.8新特性
  3. LiteAI四大技术&quot;杀手锏&quot;,解锁物联网智能设备AI开发难关
  4. JS理论:编码习惯
  5. 常用header头
  6. IDEA快捷键(windows)
  7. java 生成随机字符串
  8. 什么是 Nginx?
  9. mysqldump 参数--single-transaction
  10. 14.1 Go数据结构