战争游戏拉资源兵种自动选择策略

54a13639ec6e4be7b6d6ee2e220c7b0f.png

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct TroopItem {
	int id;
	int type;          //兵种
	int level;		   //等级
	int load;		   //装载量
	int force;		   //战力
	int own_num;	   //拥有士兵数
	int select_num;	   //选择的数量
};

bool compareTroopByLevel(const TroopItem& t1, const TroopItem& t2) {
	return t1.load > t2.load;
}

//参数:资源地 load 上限,出兵数量上限,拥有的士兵种类列表
vector<TroopItem> QuickSelectTroopList(int res_max, int march_size_max, vector<TroopItem> own_troop_list) {
	//返回的选择士兵列表(士兵类型,数量)
	vector<TroopItem> selected_troop_list;

	// 按照士兵装载值从高到低排序
	sort(own_troop_list.begin(), own_troop_list.end(), compareTroopByLevel);

	for (auto& troop : own_troop_list) {
		//达到出兵上限或已经没有资源可拉
		if (res_max == 0 || march_size_max == 0) {
			break;
		}

		int max_possible_num = 0;			//初始化最大派兵数
		int selected_num = 0;				//初始化实际派兵数量

		max_possible_num = min(res_max / troop.load, march_size_max);	//最大派兵数
		selected_num = min(max_possible_num, troop.own_num);			//实际派兵数量
		res_max -= selected_num * troop.load;							//更新剩下资源数量
		troop.own_num -= selected_num;									//更新拥有士兵数量
		march_size_max -= selected_num;									//更新最大派兵数
		
		//(res_max/troop.load)有余数 且 当前兵种还没用完 且 不超过最大派兵数量的情况下
		if(march_size_max > 0 && res_max % troop.load != 0 && troop.own_num != 0){
			troop.own_num--;
			selected_num++;
			res_max = 0;
		}
		
		//将信息加入返回列表
		if (selected_num > 0) {
			TroopItem selected_troop = troop;
			selected_troop.select_num = selected_num;
			selected_troop_list.push_back(selected_troop);
		}
	}
	
	if(res_max == 0){
		cout<<"资源可被拉完!!"<<endl;
	}else{
		cout<<"可派兵数量或者拥有士兵数量不足,资源不可被拉完!!"<<endl;
	}

	return selected_troop_list;
}

int main() {
//测试数据
//id type 等级 装载量  战力  拥有士兵数	 选择该兵数
	vector<TroopItem> own_troop_list = {
		{1, 1, 4, 7, 70, 15, 0},
		{2, 2, 3, 6, 60, 12, 0},
		{3, 3, 2, 5, 50, 10, 0},
		{4, 4, 1, 4, 40, 5, 0}
	};

	int res_max = 200;
	int march_size_max = 40;

	vector<TroopItem> selected_troop_list = QuickSelectTroopList(res_max, march_size_max, own_troop_list);

	// 输出选择的士兵列表
	for (const auto& troop : selected_troop_list) {
		cout << "Troop ID: " << troop.id << ", Selected Num: " << troop.select_num << endl;
	}

	return 0;
}

9b7570f01ae84de58ca1bc3da20e5df0.png

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_xian_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值