【题目】
2100: 兰顿蚂蚁
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 94 Solved: 62
[Submit][Status][Web Board]
Description
兰顿蚂蚁,是于1986年,由克里斯·兰顿提出来的,属于细胞自动机的一种。
平面上的正方形格子被填上黑色或白色。在其中一格正方形内有一只“蚂蚁”。
蚂蚁的头部朝向为:上下左右其中一方。
蚂蚁的移动规则十分简单:
若蚂蚁在黑格,右转90度,将该格改为白格,并向前移一格;
若蚂蚁在白格,左转90度,将该格改为黑格,并向前移一格。
规则虽然简单,蚂蚁的行为却十分复杂。刚刚开始时留下的路线都会有接近对称,像是会重复,但不论起始状态如何,蚂蚁经过漫长的混乱活动后,会开辟出一条规则的“高速公路”。
蚂蚁的路线是很难事先预测的。
你的任务是根据初始状态,用计算机模拟兰顿蚂蚁在第n步行走后所处的位置。
Input
输入数据的第一行是 m n 两个整数(3 < m, n < 100),表示正方形格子的行数和列数。
接下来是 m 行数据。
每行数据为 n 个被空格分开的数字。0 表示白格,1 表示黑格。
接下来是一行数据:x y s k, 其中x y为整数,表示蚂蚁所在行号和列号(行号从上到下增长,列号从左到右增长,都是从0开始编号)。s 是一个大写字母,表示蚂蚁头的朝向,我们约定:上下左右分别用:UDLR表示。k 表示蚂蚁走的步数。
Output
输出数据为两个空格分开的整数 p q, 分别表示蚂蚁在k步后,所处格子的行号和列号。
Sample Input
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
Sample Output
1 3
【题解】
简单模拟。
【代码】
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
main()
{
int m,n,x,y,k; char s;
int a[105][105];
char dir[5];
dir[0]='U';dir[1]='R';dir[2]='D';dir[3]='L';
while(~scanf("%d%d",&m,&n))
{
go(i,0,m-1)
go(j,0,n-1)
scanf("%d",&a[i][j]);
scanf("%d %d %c %d",&x,&y,&s,&k);
int t;
go(i,0,3) if(s==dir[i]) t=i;
go(i,1,k)
{
if(a[x][y]) t=(t+1)%4,a[x][y]=0;
else t=(t+3)%4,a[x][y]=1;
s=dir[t];
if(s=='U') x--;
else if(s=='D') x++;
else if(s=='L') y--;
else y++;
}
printf("%d %d\n",x,y);
}
}
【题目】
2096: 数字游戏
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 201 Solved: 58
[Submit][Status][Web Board]
Description
栋栋正在和同学们玩一个数字游戏。
游戏的规则是这样的:栋栋和同学们一共n个人围坐在一圈。栋栋首先说出数字1。接下来,坐在栋栋左手边的同学要说下一个数字2。再下面的一个同学要从上一个同学说的数字往下数两个数说出来,也就是说4。下一个同学要往下数三个数,说7。依次类推。
为了使数字不至于太大,栋栋和同学们约定,当在心中数到 k-1 时,下一个数字从0开始数。例如,当k=13时,栋栋和同学们报出的前几个数依次为:
1, 2, 4, 7, 11, 3, 9, 3, 11, 7。
游戏进行了一会儿,栋栋想知道,到目前为止,他所有说出的数字的总和是多少。
Input
输入的第一行包含三个整数 n,k,T,其中 n 和 k 的意义如上面所述,T 表示到目前为止栋栋一共说出的数字个数。
Output
输出一行,包含一个整数,表示栋栋说出所有数的和。
Sample Input
3 13 3
Sample Output
17
【题解】
找规律,推公式。
【代码】
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <map>
#include <list>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#define mem(a) memset(a,0,sizeof(a))
#define go(i,a,b) for(int i=a;i<=b;i++)
#define og(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=1000005;
const int inf=0x3f3f3f3f;
typedef long long ll;
typedef unsigned long long ull;
main()
{
ll n,k,t;
while(~scanf("%lld%lld%lld",&n,&k,&t))
{
ll sum=0,x=1,y=1;
go(i,1,t)
{
sum+=x;
x=(x+(2*y+n-1)*n/2)%k;
y+=n;
}
printf("%lld\n",sum);
}
}
【代码】