历届试题 兰顿蚂蚁

http://lx.lanqiao.org/problem.page?gpid=T125

 历届试题 兰顿蚂蚁  
时间限制: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
 
 
分析:
直接模拟路径,和蚂蚁的坐标。
 
AC代码:
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <string.h>
 5 #include <string>
 6 #include <math.h>
 7 #include <stdlib.h>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <map>
12 #include <list>
13 #include <iomanip>
14 #include <vector>
15 #pragma comment(linker, "/STACK:1024000000,1024000000")
16 #pragma warning(disable:4786)
17 
18 using namespace std;
19 
20 const int INF = 0x3f3f3f3f;
21 const int Max = 10000 + 10;
22 const double eps = 1e-8;
23 const double PI = acos(-1.0);
24 int a[110][110];
25 
26 void run(int x , int y , char str , int k)
27 {
28     if(k == 0)
29         printf("%d %d
", x , y);
30     else
31     {
32         k --;
33         if(a[x][y] == 0)
34         {
35             a[x][y] = 1;
36             if(str == 'U')
37                 run(x , y - 1 , 'L' , k);
38             else if(str == 'D')
39                 run(x , y + 1 , 'R' , k);
40             else if(str == 'L')
41                 run(x + 1, y , 'D' , k);
42             else
43                 run(x - 1 , y , 'U' , k);
44         }
45         else
46         {
47             a[x][y] = 0;
48             if(str == 'U')
49                 run(x , y + 1 , 'R' , k);
50             else if(str == 'D')
51                 run(x , y - 1 , 'L' , k);
52             else if(str == 'L')
53                 run(x - 1, y , 'U' , k);
54             else
55                 run(x + 1 , y , 'D' , k);
56         }
57     }
58 }
59 
60 int main()
61 {
62     int i , j , n , m , x , y , k;
63     char s;
64     scanf("%d%d",&n,&m);
65     for(i = 0;i < n;i ++)
66         for(j = 0;j < m;j ++)
67             scanf("%d",&a[i][j]);
68     scanf("%d %d %c %d", &x , &y , &s , &k);
69     run(x , y , s , k);
70     return 0;
71 }
View Code
原文地址:https://www.cnblogs.com/jeff-wgc/p/4450887.html