首先,讲一下思路:蚂蚁碰头后掉头,可以当作蚂蚁可以直接”穿过“对方,即蚂蚁碰头对蚂蚁运动没有影响。然后可以转换为每一只蚂蚁从初始位置直接到离开木杆的场景。
求最短时间:以木杆中心为基准,左边的蚂蚁往左走,右边的蚂蚁往右走,这样子,左右两边的最短时间就是所求的最短时间。
求最长时间:以木杆中心为基准,左边的蚂蚁往右走,右边的蚂蚁往左走,这样子,时间就会最长
然后贴一下代码,写得不好大家轻喷哈 [可爱]:
#include
using namespace std;
int main(){
int gan_length = 27; //杆的长度
int ant_num = 5; //蚂蚁数量
int position[5] = {3, 7, 11, 17, 23}; //蚂蚁初始位置
int speed = 1; //蚂蚁的速度
int max_time = 0; //最长时间
int min_time = 27; //最短时间 初始化为最大值,避免出现一只蚂蚁的情况
int temp_max = 0;
int temp_min = 0;
for(int i=0; i
temp_max = 0;
temp_min = 0;
if (position[i] < gan_length / 2) //中点左边
{
temp_max = (gan_length - position[i])