前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >历届试题 兰顿蚂蚁

历届试题 兰顿蚂蚁

作者头像
AI那点小事
发布2020-04-20 18:15:28
5950
发布2020-04-20 18:15:28
举报
文章被收录于专栏:AI那点小事AI那点小事

问题描述

这里写图片描述
这里写图片描述

兰顿蚂蚁,是于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

代码语言:javascript
复制
import java.util.Scanner;

/*
 * Cells是细胞自动机类
 */

class Cells{
    private int M;
    private int N;
    private int row;
    private int col;
    private int[][] cells;
    private StringBuffer head = new StringBuffer();
    private int count;

    //构造方法
    public Cells() {
        super();
        Scanner in = new Scanner(System.in);
        this.M = in.nextInt();
        this.N = in.nextInt();
        cells = new int[M][N];
        for ( int i = 0 ; i < M ; i++)
            for ( int j = 0 ; j < N ; j++)
                cells[i][j] = in.nextInt();
        this.row = in.nextInt();
        this.col = in.nextInt();
        String direction = in.next();
        this.head.append(direction);
        this.count = in.nextInt();

        in.close();
    }

    public void setRow(int row) {
        this.row = row;
    }

    public void setCol(int col) {
        this.col = col;
    }

    //上方向的移动
    public void MoveUp(){
        if (this.cells[row][col] == 0){
            this.cells[row][col] = 1;
            if( col == 0 ){
                this.setCol(this.N-1);
            }else{
                this.setCol(this.col-1);
            }
            head.replace(0, head.length(), "L");
        }else{
            cells[row][col] = 0;
            if( col == this.N-1 ){
                this.setCol(0);
            }else{
                this.setCol(this.col+1);
            }
            head.replace(0, head.length(), "R");
        }
    }

    //下方向的移动
    public void MoveDown(){
        if (this.cells[row][col] == 0){
            cells[row][col] = 1;
            if( col == this.N-1 ){
                this.setCol(0);
            }else{
                this.setCol(this.col+1);
            }
            head.replace(0, head.length(), "R");
        }else{
            cells[row][col] = 0;
            if( col == 0 ){
                this.setCol(this.N-1);
            }else{
                this.setCol(this.col-1);
            }
            head.replace(0, head.length(), "L");
        }
    }

    //左方向的移动
    public void MoveLeft(){
        if (this.cells[row][col] == 0){
            this.cells[row][col] = 1;
            if( row == this.M - 1 ){
                this.setRow(0);
            }else{
                this.setRow(this.row+1);
            }
            this.head.replace(0, head.length(), "D");
        }else{
            this.cells[row][col] = 0;
            if( row == 0 ){
                this.setRow(this.M-1);
            }else{
                this.setRow(this.row-1);
            }
            head.replace(0, head.length(), "U");
        }
    }

    //右方向的移动
    public void MoveRight(){
        if (this.cells[row][col] == 0){
            cells[row][col] = 1;
            if( row ==  0 ){
                this.setRow(this.M-1);
            }else{
                this.setRow(this.row-1);
            }
            head.replace(0, head.length(), "U");
        }else{
            cells[row][col] = 0;
            if( row ==  M-1 ){
                this.setRow(0);
            }else{
                this.setRow(this.row+1);
            }
            head.replace(0, head.length(), "D");
        }
    }

    //兰顿蚂蚁的移动
    public void Move(){
        for ( int i = this.count ; i > 0 ; i--){
            if("U".equals(this.head.toString())){
                MoveUp();
            }else if("D".equals(this.head.toString())){
                MoveDown();
            }else if("L".equals(this.head.toString())){
                MoveLeft();
            }else if("R".equals(this.head.toString())){
                MoveRight();
            }
        }
    }

    public void Print_Location(){
        System.out.print(this.row+" "+this.col);
    }
}

public class Main {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Cells myCells = new Cells();
        myCells.Move();
        myCells.Print_Location();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档