UVA 10603 Fill (隐式图遍历)

本文介绍了一个经典的计算机科学问题——如何使用三个容器精确测量特定数量的水,并尽量减少倒水次数。文章通过广度优先搜索算法解决此问题,寻找最优解。

 

There are three jugs with a volume of a, b and c liters. (a, b, and c are positive integers not greater than 200). The first and the second jug are initially empty, while the third

is completely filled with water. It is allowed to pour water from one jug into another until either the first one is empty or the second one is full. This operation can be performed zero, one or more times.

 

You are to write a program that computes the least total amount of water that needs to be poured; so that at least one of the jugs contains exactly d liters of water (d is a positive integer not greater than 200). If it is not possible to measure d liters this way your program should find a smaller amount of water d' < d which is closest to d and for which d' liters could be produced. When d' is found, your program should compute the least total amount of poured water needed to produce d' liters in at least one of the jugs.

 

Input

The first line of input contains the number of test cases. In the next T lines, T test cases follow. Each test case is given in one line of input containing four space separated integers - a, b, c and d.

 
Output

The output consists of two integers separated by a single space. The first integer equals the least total amount (the sum of all waters you pour from one jug to another) of poured water. The second integer equals d, if d liters of water could be produced by such transformations, or equals the closest smaller value d' that your program has found.

 

Sample Input

Sample Output

2

2 3 4 2

96 97 199 62

2 2

9859 62


题意:给定三个容器。第三个容器中装满了水。给定一个指定量的水。要求用3个容器倒出这样的水。最少要转移几升水。

如果倒不出来,在在指定量的水-1继续查询。

思路:状态记录的广搜。开一个二维数组存下前两杯水的状态(因为前两杯水确定了第三杯水就确定了所以只需要开二维数组。然后进行广搜,把能倒出的水量进行标记(标记的值为最小转移的水量)。最后在从指定水量往下找到。如果找到一个有标记过的水量即该水量是可以倒出的最大水量。

UVA上改数据了,重写一发!

代码:

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;

const int N = 205;

int s, n, m, vis[N][N];

struct State {
	int a, b, val;
	State() {}
	State(int a, int b, int val) {
		this->a = a;
		this->b = b;
		this->val = val;
	}
};

void add(int a, int b, int val, queue<State> &Q) {
	if (vis[a][b] > val) {
		vis[a][b] = val;
		Q.push(State(a, b, val));
	}
}

const int INF = 0x3f3f3f3f;

void solve() {
	memset(vis, INF, sizeof(vis));
	queue<State> Q;
	Q.push(State(0, 0, 0));
	vis[0][0] = 0;
	while (!Q.empty()) {
		State u = Q.front();
		Q.pop();
		int us = s - u.a - u.b;
		int ns, na, nb, val;
		ns = max(0, us - (n - u.a));
		val = u.val + us - ns;
		na = us - ns + u.a;
		nb = u.b;
		add(na, nb, val, Q);
		ns = max(0, us - (m - u.b));
		val = u.val + us - ns;
		na = u.a;
		nb = us - ns + u.b;
		add(na, nb, val, Q);
		na = max(0, u.a - (m - u.b));
		val = u.val + u.a - na;
		nb = u.a - na + u.b;
		add(na, nb, val, Q);
		na = max(0, u.a - (s - us));
		val = u.val + u.a - na;
		nb = u.b;
		add(na, nb, val, Q);
		nb = max(0, u.b - (s - us));
		val = u.val + u.b - nb;
		na = u.a;
		add(na, nb, val, Q);
		nb = max(0, u.b - (n - u.a));
		val = u.val + u.b - nb;
		na = u.b - nb + u.a;
		add(na, nb, val, Q);
	}
}

int t, d;

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d%d%d", &n, &m, &s, &d);
		solve();
		int i;
		int ans;
		for (i = d; i >= 0; i--) {
			ans = INF;
			for (int j = 0; j <= s - i; j++) {
				int k = s - i - j;
				ans = min(ans, vis[i][j]);
				ans = min(ans, vis[i][k]);
				ans = min(ans, vis[j][k]);
				ans = min(ans, vis[j][i]);
				ans = min(ans, vis[k][i]);
				ans = min(ans, vis[k][j]);
			}
			if (ans != INF) break;
		}
		printf("%d %d\n", ans, i);
	}
	return 0;
}


内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值