跳到正文

题解

题解:P12776 [POI 2018/2019 R1] 小机器人 Robby the little robot

Ymy1201约 3 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 3 分钟
题目
P12776

版本

  1. v1当前

题目:P12776 [POI 2018/2019 R1] 小机器人 Robby the little robot

题目大意‌: 模拟一个机器人在二维平面上按特定规律移动,统计在时间 tt 内经过目标点 (a,b)(a,b) 的次数。

算法思路

1. ‌移动规则‌:

  • 方向循环:上 →\to 右 →\to 下 →\to 左
  • 步长序列:按数组 dd 循环取值
  • 时间消耗:移动距离 +1+1(含转向时间)

2‌. 优化策略‌:

  • 周期性检测:当轨迹出现循环时直接计算剩余时间贡献
  • 精确判断:检查移动线段是否穿过目标点

完整代码(复制者CE)

C++
#include<bit/stdc++.h>
using namespace std;
long long n,t,t2,d[100001],a,b,x,y,c,z,ans,ans2,e,sum;
int mian() {
//	freopen("a.in","r",stdin);
	scanf("%lld%lld",&n,&t);
	t2=t;
	for(int i=1; i<=n; i++)scanf("%lld",d+i);
	scanf("%lld%lld",&a,&b);
	if(a==0&&b==0)ans2=1;
	while(t>0) {
		z++;
		if(z>n) {
			z=1;
			if(x==0&&y==0&&e==0) {
				ans*=t/(t2-t)+1;
				t%=t2-t;
			}
		}
		if(e==0) {
			long long v=x;
			x+=d[z];
			t-=abs(x-v);
			if(t<0)x+=t;
			if(x>=b&&y==a&&v<b)ans++;
		} else if(e==1) {
			long long v=y;
			y+=d[z];
			t-=abs(y-v);
			if(t<0)y+=t;
			if(x==b&&y>=a&&v<a)ans++;
		} else if(e==2) {
			long long v=x;
			x-=d[z];
			t-=abs(v-x);
			if(t<0)x-=t;
			if(x<=b&&y==a&&v>b)ans++;
		} else {
			long long v=y;
			y-=d[z];
			t-=abs(v-y);
			if(t<0)y-=t;
			if(x==b&&y<=a&&v>a)ans++;
		}
		(e+=1)%=4;
		t--;
		if(++sum>1e9)return printf("%lld",ans+ans2),0;
	}
	printf("%lld",ans+ans2);
	return 0;
}

代码特点说明

1. ‌周期性优化‌: 检测完整移动周期,避免重复计算 2. 精确碰撞检测‌: 通过移动前后坐标判断是否穿过目标点 3. ‌边界处理‌: 正确处理时间不足时的坐标回退 4. ‌安全控制‌: sumsum 计数器防止极端情况下的无限循环

球管理大大大大大大大大过(qwp)

原文