zoukankan      html  css  js  c++  java
  • 蓝桥杯 兰顿蚂蚁(Bfs)

    历届试题 兰顿蚂蚁  
    时间限制: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
     
    注意方向和增量, 不一样!  兰顿蚂蚁? 多蓝盾 or  兰兆之盾?
    #include <cstdio>
    #include <cstring>
    #include <queue>
    using namespace std;
    const int O = 101;
    int G[O][O];
    char ch[2];
    int n, m, k;
    int N, M;
    int ac[5][2]={0, 0, 0, 1, 1, 0, 0, -1, -1, 0};
    struct matrix
    {
        int x, y, step;
    };
    int deal(int b, char a)   //dir; 
    {
        if(b==0)
        {
            if(a=='U') return 4;
            if(a=='L') return 3;
            if(a=='D') return 2;
            if(a=='R') return 1;
        }
        if(b==1)
        {
            if(a=='U') return 2;
            if(a=='L') return 1;
            if(a=='D') return 4;
            if(a=='R') return 3;
        }
    }
    int add(int b, char a)   //add
    {
        if(b==0)
        {
            if(a=='U') return 3;
            if(a=='L') return 2;
            if(a=='D') return 1;
            if(a=='R') return 4;
        }
        if(b==1)
        {
            if(a=='U') return 1;
            if(a=='L') return 4;
            if(a=='D') return 3;
            if(a=='R') return 2;
        }
    }
    void bfs(int x, int y)   
    {
        matrix r, s, t;
        r.x=x; r.y=y; r.step=0;
        queue<matrix>Q;
        Q.push(r);
        while(!Q.empty())
        {
            if(t.step==k)
            {
                N=t.x; M=t.y;
                return;
            }
            s=Q.front();
            Q.pop();
            int cnt = deal(G[s.x][s.y], ch[0]);
            int inc = add(G[s.x][s.y], ch[0]);
            if(cnt==1) strcpy(ch, "U");
            if(cnt==2) strcpy(ch, "R");
            if(cnt==3) strcpy(ch, "D");
            if(cnt==4) strcpy(ch, "L");
           // printf("%d %c %d ",cnt, ch[0], inc);
            G[s.x][s.y]==1? G[s.x][s.y]=0:G[s.x][s.y]=1;
            t.x=s.x+ac[inc][0];
            t.y=s.y+ac[inc][1];
            t.step=s.step+1;
          //  printf("%d %d
    ", t.x, t.y);
            Q.push(t);
        }
    }
    int main()
    {
        scanf("%d%d", &n, &m);
    
            for(int i = 0; i < n; i++)
                for(int j = 0; j < m; j++)
                    scanf("%d", &G[i][j]);
    
            int a, b;
            scanf("%d%d%s%d", &a, &b, ch, &k);
            bfs(a, b);
            printf("%d %d
    ", N, M);
    
        return 0;
    }
     
  • 相关阅读:
    Mac上的USB存储设备使用痕迹在新版操作系统有所变化
    Beware of the encrypted VM
    A barrier for Mobile Forensics
    Second Space could let suspect play two different roles easily
    Take advantage of Checkra1n to Jailbreak iDevice for App analysis
    Find out "Who" and "Where"
    Where is the clone one and how to extract it?
    Downgrade extraction on phones running Android 7/8/9
    高版本安卓手机的取证未来
    How to extract WeChat chat messages from a smartphone running Android 7.x or above
  • 原文地址:https://www.cnblogs.com/soTired/p/5135269.html
Copyright © 2011-2022 走看看