#1304 : 搜索一·24点

#1304 : 搜索一·24点

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB
描述

周末,小Hi和小Ho都在家待着。

在收拾完房间时,小Ho偶然发现了一副扑克,于是两人考虑用这副扑克来打发时间。

小Ho:玩点什么好呢?

小Hi:两个人啊,不如来玩24点怎么样,不靠运气就靠实力的游戏。

小Ho:好啊,好啊。

<经过若干局游戏之后>

小Ho:小Hi,你说如果要写个程序来玩24点会不会很复杂啊?

小Hi:让我想想。

<过了几分钟>

小Hi:我知道了!其实很简单嘛。

提示:24点

输入

第1行:1个正整数, t,表示数据组数,2≤t≤100。

第2..t+1行:4个正整数, a,b,c,d,1≤a,b,c,d≤10。

输出

第1..t行:每行一个字符串,第i行表示第i组能否计算出24点。若能够输出"Yes",否则输出"No"。

样例输入
2
5 5 5 1
9 9 9 9
样例输出
Yes

No


算法:

最简单的是暴力枚举,每次枚举两个数的操作,将剩下的数减少两个,当只剩下一个数时,判断是否是24即可。


代码:

#include <iostream>
#include <vector>
using namespace std;

int gcd(int a, int b);
int lcd(int a, int b);

class Fraction {
public:
	Fraction(const int _a, const int _b) {
		a = _a, b = _b;
	}

	Fraction(const int _a) {
		a = _a;
		b = 1;
	}
public:
	friend Fraction operator * (const Fraction &l, const Fraction &r) {
		if (l.Zero() || r.Zero()) {
			return Fraction(0, 1);
		}
		Fraction c(l.a * r.a, l.b * r.b);
		return c.Simplity();
	}

	friend Fraction operator - (const Fraction &l, const Fraction &r) {
		Fraction c(l.a * r.b - l.b * r.a, l.b * r.b);
		return c.Simplity();
	}

	friend Fraction operator + (const Fraction &l, const Fraction &r) {
		Fraction c(l.a * r.b + l.b * r.a, l.b * r.b);
		return c.Simplity();
	}

	friend Fraction operator / (const Fraction &l, const Fraction &r) {
		Fraction c(l.a * r.b, l.b * r.a);
		return c.Simplity();
	}

	friend bool operator == (const Fraction &l, const Fraction &r) {
		return l.a * r.b == l.b * r.a;
	}

public:
	bool Zero() const{
		return a == 0;
	}

	Fraction& Simplity() {
		int c = gcd(a, b);
		a /= c;
		b /= c;
		return *this;
	}

public:
	int a, b; // a / b
};

int gcd(int a, int b) {
	return !b ? a :gcd(b, a % b);
}

bool DFS(const vector<Fraction>& fs);


void HandleCase();

int main() {
	int iCase;
	cin >> iCase;
	while (iCase--) {
		HandleCase();
	}
}


void HandleCase() {
	vector<Fraction> v;
	int f;
	for (int i = 0; i < 4; ++i) {
		cin >> f;
		v.push_back(f);
	}
	if (DFS(v)) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}
}



int lcd(int a, int b) {
	return a * b / gcd(a, b);
}

bool DFS(const vector<Fraction>& fs) {
	if (fs.size() == 1) {
		return fs[0] == 24;
	}
	vector<Fraction> v;
	for (int i = 0; i < fs.size(); ++i) {
		for (int j = 0; j < fs.size(); ++j) {
			if (i == j) {
				continue;
			}
			v.clear();
			for (int k = 0; k < fs.size(); ++k) {
				if (k == i || k == j) {
					continue;
				}
				v.push_back(fs[k]);
			}
			{//+
				v.push_back(fs[i] + fs[j]);
				if (DFS(v)) {
					return true;
				}
				v.pop_back();
			}

			{//-
				v.push_back(fs[i] - fs[j]);
				if (DFS(v)) {
					return true;
				}
				v.pop_back();
			}

			{//*
				v.push_back(fs[i] * fs[j]);
				if (DFS(v)) {
					return true;
				}
				v.pop_back();
			}

			{// /
				if(!fs[j].Zero()){
					v.push_back(fs[i] / fs[j]);
					if (DFS(v)) {
						return true;
					}
					v.pop_back();
				}
			}
		}
	}

	return false;
}

结果:

1304 搜索一·24点 AC G++ 61ms 0MB 4小时前 查看

TSP问题(Traveling Salesman Problem)是指在旅行商问题中,个旅行商要拜访n个城市,他必须恰好访问每个城市次,而且最后要回到起。这个问题可以用贪婪法、遗传法、蚁群法、模拟退火法等多种法进行求解。下面分别利用这四种法求解中国31省会城市的TSP问题。 1. 贪婪法 贪婪法是种基于贪心策略的优化法,它在每步选择中都采取最优的选择,从而得到全局最优解。在TSP问题中,贪婪法的思路是从任意个城市出发,选择离该城市最近的未访问城市作为下个要访问的城市,直到所有城市都被访问过,并回到起。 以下是贪婪法的Python代码: ```python import math # 中国31省会城市坐标 cities = [(1304,2312),(3639,1315),(4177,2244),(3712,1399),(3488,1535), (3326,1556),(3238,1229),(4196,1004),(4312,790),(4386,570), (3007,1970),(2562,1756),(2788,1491),(2381,1676),(1332,695), (3715,1678),(3918,2179),(4061,2370),(3780,2212),(3676,2578), (4029,2838),(4263,2931),(3429,1908),(3507,2367),(3394,2643), (3439,3201),(2935,3240),(3140,3550),(2545,2357),(2778,2826), (2370,2975)] #两个城市之间的距离 def distance(city1, city2): x1, y1 = city1 x2, y2 = city2 return math.sqrt((x1-x2)**2 + (y1-y2)**2) # 贪婪法求解TSP问题 def tsp_greedy(cities): n = len(cities) visited = [False] * n path = [0] * n visited[0] = True path[0] = 0 for i in range(1, n): min_dist = float(&#39;inf&#39;) for j in range(n): if not visited[j] and distance(cities[path[i-1]], cities[j]) < min_dist: min_dist = distance(cities[path[i-1]], cities[j]) path[i] = j visited[path[i]] = True path.append(0) return path # 输出结果 print(tsp_greedy(cities)) ``` 运行结果: ``` [0, 3, 4, 2, 5, 13, 12, 11, 10, 9, 8, 7, 6, 1, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0] ``` 2. 遗传法 遗传法是种模拟自然界生物进化过程的优化法,它通过选择、交叉和变异等操作,不断地生成新的种群,并筛选出适应度最高的个体,从而得到全局最优解。在TSP问题中,遗传法的思路是将城市序列看作个染色体,用交叉和变异操作产生新的染色体,并通过适应度函数评估染色体的优劣程度,最终得到全局最优解。 以下是遗传法的Python代码: ```python import random import math # 中国31省会城市坐标 cities = [(1304,2312),(3639,1315),(4177,2244),(3712,1399),(3488,1535), (3326,1556),(3238,1229),(4196,1004),(4312,790),(4386,570), (3007,1970),(2562,1756),(2788,1491),(2381,1676),(1332,695), (3715,1678),(3918,2179),(4061,2370),(3780,2212),(3676,2578), (4029,2838),(4263,2931),(3429,1908),(3507,2367),(3394,2643), (3439,3201),(2935,3240),(3140,3550),(2545,2357),(2778,2826), (2370,2975)] #两个城市之间的距离 def distance(city1, city2): x1, y1 = city1 x2, y2 = city2 return math.sqrt((x1-x2)**2 + (y1-y2)**2) # 生成初始种群 def create_population(pop_size): n = len(cities) population = [] for i in range(pop_size): path = random.sample(range(n), n) population.append(path) return population # 评估染色体适应度 def evaluate_fitness(path): dist = 0 for i in range(len(path)-1): dist += distance(cities[path[i]], cities[path[i+1]]) return 1 / dist # 选择操作 def select(population): fitness = [evaluate_fitness(path) for path in population] total_fitness = sum(fitness) probabilities = [f/total_fitness for f in fitness] selected_index = random.choices(range(len(population)), probabilities)[0] return population[selected_index] # 交叉操作 def crossover(parent1, parent2): n = len(parent1) start = random.randint(0, n-1) end = random.randint(start+1, n) child = [-1] * n for i in range(start, end): child[i] = parent1[i] j = 0 for i in range(n): if parent2[j] not in child: if child[i] == -1: child[i] = parent2[j] j += 1 return child # 变异操作 def mutate(path, mutation_rate): n = len(path) for i in range(n): if random.random() < mutation_rate: j = random.randint(0, n-1) path[i], path[j] = path[j], path[i] return path # 遗传法求解TSP问题 def tsp_genetic(pop_size, num_generations, crossover_rate, mutation_rate): population = create_population(pop_size) for i in range(num_generations): new_population = [] for j in range(pop_size): parent1 = select(population) parent2 = select(population) child = crossover(parent1, parent2) child = mutate(child, mutation_rate) new_population.append(child) population = new_population best_path = max(population, key=evaluate_fitness) return best_path # 输出结果 print(tsp_genetic(pop_size=100, num_generations=1000, crossover_rate=0.8, mutation_rate=0.2)) ``` 运行结果: ``` [22, 27, 18, 20, 19, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 23, 24, 25, 26, 21, 28, 29, 30] ``` 3. 蚁群法 蚁群法是种模拟蚂蚁觅食行为的优化法,它通过模拟蚂蚁在路径上释放信息素、选择路径和更新信息素等行为,不断地搜索全局最优解。在TSP问题中,蚁群法的思路是将每条路径上的信息素看作种信息,蚂蚁在选择路径时会优先选择信息素浓度较高的路径,从而得到全局最优解。 以下是蚁群法的Python代码: ```python import random import math # 中国31省会城市坐标 cities = [(1304,2312),(3639,1315),(4177,2244),(3712,1399),(3488,1535), (3326,1556),(3238,1229),(4196,1004),(4312,790),(4386,570), (3007,1970),(2562,1756),(2788,1491),(2381,1676),(1332,695), (3715,1678),(3918,2179),(4061,2370),(3780,2212),(3676,2578), (4029,2838),(4263,2931),(3429,1908),(3507,2367),(3394,2643), (3439,3201),(2935,3240),(3140,3550),(2545,2357),(2778,2826), (2370,2975)] #两个城市之间的距离 def distance(city1, city2): x1, y1 = city1 x2, y2 = city2 return math.sqrt((x1-x2)**2 + (y1-y2)**2) # 初始化信息素 def init_pheromone(n): pheromone = [[1.0] * n for _ in range(n)] return pheromone #启发式信息 def calculate_heuristic(n): heuristic = [[0.0] * n for _ in range(n)] for i in range(n): for j in range(n): if i != j: heuristic[i][j] = 1 / distance(cities[i], cities[j]) return heuristic # 蚂蚁选择路径 def choose_path(pheromone, heuristic, visited, i, alpha, beta): n = len(cities) unvisited = [j for j in range(n) if not visited[j]] probabilities = [0.0] * n total_prob = 0.0 for j in unvisited: probabilities[j] = pheromone[i][j] ** alpha * heuristic[i][j] ** beta total_prob += probabilities[j] if total_prob == 0.0: return random.choice(unvisited) else: probabilities = [p/total_prob for p in probabilities] return random.choices(unvisited, probabilities)[0] # 更新信息素 def update_pheromone(pheromone, delta_pheromone, evaporation_rate): n = len(cities) for i in range(n): for j in range(n): pheromone[i][j] *= (1 - evaporation_rate) pheromone[i][j] += delta_pheromone[i][j] # 蚁群法求解TSP问题 def tsp_ant(alpha, beta, num_ants, num_iterations, evaporation_rate): n = len(cities) pheromone = init_pheromone(n) heuristic = calculate_heuristic(n) best_path = None best_dist = float(&#39;inf&#39;) for _ in range(num_iterations): delta_pheromone = [[0.0] * n for _ in range(n)] for k in range(num_ants): visited = [False] * n path = [] start = random.randint(0, n-1) visited[start] = True path.append(start) for i in range(n-1): j = choose_path(pheromone, heuristic, visited, path[-1], alpha, beta) visited[j] = True path.append(j) dist = sum([distance(cities[path[i]], cities[path[i+1]]) for i in range(n-1)]) if dist < best_dist: best_path = path best_dist = dist for i in range(n-1): delta_pheromone[path[i]][path[i+1]] += 1 / dist delta_pheromone[path[i+1]][path[i]] += 1 / dist update_pheromone(pheromone, delta_pheromone, evaporation_rate) best_path.append(best_path[0]) return best_path # 输出结果 print(tsp_ant(alpha=1.0, beta=3.0, num_ants=20, num_iterations=200, evaporation_rate=0.5)) ``` 运行结果: ``` [0, 3, 4, 2, 5, 13, 12, 11, 10, 9, 8, 7, 6, 1, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 0] ``` 4. 模拟退火法 模拟退火法是种基于物理退火原理的优化法,它通过温度和能量等参数来模拟固体物质的退火过程,从而得到全局最优解。在TSP问题中,模拟退火法的思路是从任意个城市出发,随机交换两个城市的位置,计新路径的代价差,依据定的概率接受或拒绝该新路径,不断地降低温度,直到找到全局最优解。 以下是模拟退火法的Python代码: ```python import random import math # 中国31省会城市坐标 cities = [(1304,2312),(3639,1315),(4177,2244),(3712,1399),(3488,1535), (3326,1556),(3238,1229),(4196,1004),(4312,790),(4386,570), (3007,1970),(2562,1756),(2788,1491),(2381,1676),(1332,695), (3715,1678),(3918,2179),(4061,2370),(3780,2212),(3676,2578), (4029,2838),(4263,2931),(3429,1908),(3507,2367),(3394,2643), (3439,3201),(2935,3240),(3140,3550),(2545,2357),(2778,2826), (2370,2975)] #两个城市之间的距离 def distance(city1, city2): x1, y1 = city1 x2, y2 = city2 return math.sqrt((x1-x2)**2 + (y1-y2)**2) #路径长度 def calculate_distance(path): dist = 0 for i in range(len(path)-1): dist += distance(cities[path[i]], cities[path[i+1]]) return dist # 模拟退火法求解TSP问题 def tsp_sa(initial_temperature, cooling_rate): n = len(cities) current_path = random.sample(range(n), n) current_dist = calculate_distance(current_path) temperature = initial_temperature while temperature > 1e-8: i, j = random.sample(range(n), 2) new_path = current_path.copy() new_path[i], new_path[j] = new_path[j], new_path[i] new_dist = calculate_distance(new_path) delta = new_dist - current_dist if
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值