蚂蚁移动

47 篇文章 2 订阅

蚂蚁移动 ⁡ \operatorname{蚂蚁移动}

题目链接: ybtoj 20033 ⁡ \operatorname{ybtoj\ 20033} ybtoj 20033

题目

有一根尺子,长度 l l l,在上面有 n n n 只蚂蚁,且没有两只蚂蚁初始位置相同。每只蚂蚁有一个初始方向(左或者右),且它们会爬行,速度都是每秒一个长度单位。当它们碰到另外一个蚂蚁或者尺子的边缘时,它们会立即改变移动的方向(即反向)。

给定尺子的长度,蚂蚁的只数,以及所有蚂蚁初始的位置和方向。要你求第 t t t 秒时每只蚂蚁的位置。

输入

第一行两个整数 l l l t t t

第二行一个整数 N N N,表示蚂蚁的只数。

接下来的每行由两部分组成。第一部分是一个整数,表示该蚂蚁的初始位置。第二部分是一个字母,表示初始方向:D表示向右,L表示向左。两部分中间空格 .

输出

n n n 个整数,表示每只蚂蚁的最终位置。无需按照蚂蚁的原先编号输出,只要按照最终位置坐标递增(非降)的顺序输出坐标即可。

样例输入1

3 5 
1 
1 D

样例输出1

0

样例输入2

5 5 
2 
2 D 
4 L

样例输出2

1 3

样例输入3

8 10 
5 
1 L 
3 L 
4 D 
6 L 
7 D

样例输出3

1 2 4 7 7

数据范围

对于 100 % 100\% 100% 的数据,满足 1 ≤ n < l ≤ 2 × 1 0 5 , 1 ≤ n ≤ 7 × 1 0 4 , 1 ≤ t ≤ 1 0 6 1\leq n<l\leq 2\times10^5,1\leq n\leq 7\times10^4,1\leq t\leq 10^6 1n<l2×105,1n7×104,1t106

数据保证有梯度

思路

这道题是一道结论加模拟。

我们可以看到两个蚂蚁碰到就要掉头这个条件很烦,如果这个条件没有了,我们就可以枚举每一只蚂蚁最后走到哪里。

这时候你就要想:上帝啊!让这个条件消失吧!!!
然后你再去打代码的时候就算忽略了这个条件,也可以 A。

所以没了。(bushi

好了好了,正常讲:

虽然上面祈祷这个东西没用,但我们是真的可以通过一些方法得出那个条件等于不存在。

我们来看一下两个蚂蚁碰到时的不同情况:
在这里插入图片描述
这,是一根木棍。
每一个格子,代表一个单位的长度。

第一种

在这里插入图片描述
上一步的时候,两只蚂蚁是这样。
在这里插入图片描述
无视碰撞,就会变成这样。
在这里插入图片描述
这个是看碰撞的。

可以发现,在把每一个蚂蚁看成一样的时候,这两个其实是一样的。
(其实就是方向跟换了一下,又因为位置都一样, 所以可以视为不碰撞)

第二种

在这里插入图片描述
上一步的时候,两只蚂蚁是这样。
在这里插入图片描述
无视碰撞,就会变成这样。
在这里插入图片描述
这个是看碰撞的。

可以发现又是一样。
(因为两个蚂蚁走到了两个点中间然后又折了回来,所以算碰撞就是两个人的方向变了,位置没变)
(而当不算碰撞的时候蚂蚁走到了对方的位置,方向又没变,而原来两只蚂蚁方向就不一样,所以就跟算碰撞一样)

所以我们就可以直接算啦~

代码

#include<cstdio>
#include<algorithm>

using namespace std;

int l, t, n, a[70001], nowt;
char way;

int main() {
//	freopen("mravi.in", "r", stdin);
//	freopen("mravi.out", "w", stdout);
	
	scanf("%d %d\n%d", &l, &t, &n);
	
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		way = getchar();
		while (way != 'D' && way != 'L') way = getchar();
		
		if (way == 'D') {//向右走
			nowt = t;
			if (t <= l - a[i]) {//不会掉头
				a[i] = a[i] + t;
				continue;
			}
			nowt -= l - a[i];
			a[i] = l;
			if (nowt / l % 2 == 1) a[i] = 0;
			nowt %= l;
			if (a[i] == 0) a[i] += nowt;
				else a[i] -= nowt;
		}
		else {//向左走
			nowt = t;
			if (t <= a[i]) {//不会掉头
				a[i] = a[i] - t;
				continue;
			}
			nowt -= a[i];
			a[i] = 0;
			if (nowt / l % 2 == 1) a[i] = l;
			nowt %= l;
			if (a[i] == 0) a[i] += nowt;
				else a[i] -= nowt;
		}
	}
	
	sort(a + 1, a + n + 1);
	
	for (int i = 1; i <= n; i++) printf("%d ", a[i]);
	
	fclose(stdin);
	fclose(stdout);

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Python 实现蚁群算法的蚂蚁移动动画过程的示例代码: ```python import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation # 定义蚂蚁的数量、城市数量和迭代次数 num_ants = 10 num_cities = 20 num_iterations = 100 # 定义城市的坐标 cities = np.random.rand(num_cities, 2) # 计算城市之间的距离 distances = np.zeros((num_cities, num_cities)) for i in range(num_cities): for j in range(num_cities): distances[i, j] = np.sqrt((cities[i, 0] - cities[j, 0]) ** 2 + (cities[i, 1] - cities[j, 1]) ** 2) # 初始化信息素矩阵 pheromones = np.ones((num_cities, num_cities)) # 定义蚂蚁类 class Ant: def __init__(self, start_city): self.current_city = start_city self.visited_cities = [start_city] self.path_length = 0 def choose_next_city(self): # 计算当前城市与其他未访问城市之间的信息素和距离的乘积 unvisited_cities = [city for city in range(num_cities) if city not in self.visited_cities] product = np.zeros(len(unvisited_cities)) for i, city in enumerate(unvisited_cities): product[i] = pheromones[self.current_city, city] ** 0.5 * (1 / distances[self.current_city, city]) # 根据概率选择下一个城市 probabilities = product / np.sum(product) next_city = np.random.choice(unvisited_cities, p=probabilities) # 更新路径长度和已访问城市列表 self.path_length += distances[self.current_city, next_city] self.current_city = next_city self.visited_cities.append(next_city) # 定义蚂蚁群类 class AntColony: def __init__(self): self.ants = [Ant(start_city=np.random.randint(num_cities)) for i in range(num_ants)] def update_pheromones(self): # 计算每只蚂蚁留下的信息素 delta_pheromones = np.zeros((num_cities, num_cities)) for ant in self.ants: for i in range(len(ant.visited_cities) - 1): current_city = ant.visited_cities[i] next_city = ant.visited_cities[i+1] delta_pheromones[current_city, next_city] += 1 / ant.path_length # 更新信息素矩阵 pheromones *= 0.5 pheromones += delta_pheromones def run(self): # 迭代指定次数 for i in range(num_iterations): # 每只蚂蚁移动一步 for ant in self.ants: ant.choose_next_city() # 更新信息素 self.update_pheromones() # 重置所有蚂蚁的状态 for ant in self.ants: ant.current_city = np.random.randint(num_cities) ant.visited_cities = [ant.current_city] ant.path_length = 0 def get_best_path(self): # 找到最短路径 best_ant = self.ants[0] for ant in self.ants: if ant.path_length < best_ant.path_length: best_ant = ant return best_ant.visited_cities, best_ant.path_length # 定义动画函数 def animate(i): ax.clear() ax.set_xlim(0, 1) ax.set_ylim(0, 1) ax.set_title('Iteration {}'.format(i)) # 运行蚂蚁群算法 colony = AntColony() colony.run() # 绘制城市 for j in range(num_cities): ax.plot(cities[j, 0], cities[j, 1], 'bo') # 绘制路径 path, path_length = colony.get_best_path() for j in range(len(path) - 1): ax.plot([cities[path[j], 0], cities[path[j+1], 0]], [cities[path[j], 1], cities[path[j+1], 1]], 'r-') # 显示路径长度 ax.text(0.1, 0.9, 'Path length: {:.2f}'.format(path_length), transform=ax.transAxes) # 创建动画 fig, ax = plt.subplots() ani = animation.FuncAnimation(fig, animate, frames=num_iterations, interval=500) plt.show() ``` 在运行该代码时,会生成一个动画,展示蚂蚁在城市之间移动的过程,并在每次迭代后绘制最佳路径。在动画的右上角,会显示当前迭代次数和最佳路径的长度

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值