zoukankan      html  css  js  c++  java
  • 蓝桥杯 历届试题 PREV-33 兰顿蚂蚁

    历届试题 兰顿蚂蚁  
    时间限制: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
     
    示例代码:
     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 
     5 public class Main {
     6     static int[][] arr;
     7     public static void main(String[] args) throws IOException {
     8         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     9         String[] str = br.readLine().split(" ");
    10         int m = Integer.parseInt(str[0]);
    11         int n = Integer.parseInt(str[1]);
    12         
    13         arr = new int[m][n];
    14         
    15         for(int i = 0; i < m; i++){
    16             str = br.readLine().split(" ");
    17             for(int j = 0; j < n; j++){
    18                 arr[i][j] = Integer.parseInt(str[j]);
    19             }
    20         }
    21         
    22         str = br.readLine().split(" ");
    23         int x = Integer.parseInt(str[0]);
    24         int y = Integer.parseInt(str[1]);
    25         char s = str[2].charAt(0);
    26         int k = Integer.parseInt(str[3]);
    27         
    28         antGo(x,y,s,k);
    29         
    30     }
    31     
    32     private static void antGo(int x, int y, char s, int k) {
    33         if( k == 0 ){
    34             System.out.println(x+" "+y);
    35         }else{
    36             k --;
    37             if(arr[x][y] == 0){
    38                 arr[x][y] = 1;
    39                 if(s == 'U'){
    40                     antGo(x , y - 1 , 'L' , k);
    41                 }else if(s == 'D'){
    42                     antGo(x , y + 1 , 'R' , k);
    43                 }else if(s == 'L'){
    44                     antGo(x + 1, y , 'D' , k);
    45                 }else{
    46                     antGo(x - 1 , y , 'U' , k);
    47                 }
    48             }else{
    49                 arr[x][y] = 0;
    50                 if(s == 'U'){
    51                     antGo(x , y + 1 , 'R' , k);
    52                 }else if(s == 'D'){
    53                     antGo(x , y - 1 , 'L' , k);
    54                 }else if(s == 'L'){
    55                     antGo(x - 1, y , 'U' , k);
    56                 }else{
    57                     antGo(x + 1 , y , 'D' , k);
    58                 }
    59             }
    60         }
    61     }
    62 }
  • 相关阅读:
    Intellij Idea非常6的10个姿势!
    java 集合
    oracle sql调优
    springboot
    整理的java jdk1.8跟mybatiseplus
    java8中lambda的用法(map转list,list转map等等
    遍历数组中的元素(含es6方法)
    云计算初探
    转FMS小工具用法
    Flex内存消耗
  • 原文地址:https://www.cnblogs.com/cao-lei/p/6628061.html
Copyright © 2011-2022 走看看