UVA 10123 No Tipping (物理+贪心+DFS剪枝)

平衡木板问题求解
本文介绍了一个关于平衡木板的问题及其解决方案。该问题要求在移动木板上的物品时保持木板平衡,避免倾斜。通过将物品按其力矩排序,并采用贪心策略逐一移除物品来解决这一挑战。

Problem A - No Tipping

As Archimedes famously observed, if you put an object on a lever arm, it will exert a twisting force around the lever's fulcrum. This twisting is called torque and is equal to the object's weight multiplied by its distance from the fulcrum (the angle of the lever also comes in, but that does not concern us here). If the object is to the left of the fulcrum, the direction of the torque is counterclockwise; if the object is to the right, the direction is clockwise. To compute the torque around a support, simply sum all the torques of the individual objects on the lever.

The challenge is to keep the lever balanced while adjusting the objects on it. Assume you have a straight, evenly weighted board, 20 meters long and weighing three kilograms. The middle of the board is the center of mass, and we will call that position 0. So the possible positions on the board range from -10 (the left end) to +10 (the right end). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. On the board are six packages, at positions -8, -4, -3, 2, 5 and 8, having weights of 4, 10, 10, 4, 7 and 8 kilograms, respectively as in the picture below.

Your job is to remove the packages one at a time in such a way that the board rests on both supports without tipping. The board would tip if the net torque around the left fulcrum (resulting from the weights of the packages and the board itself) were counterclockwise or if the net torque around the right fulcrum were clockwise. A possible solution to this problem is: first remove the package at position -4, then the package at 8, then -8, then 5, then -3 and finally 2.

You are to write a program which solves problems like the one described above. The input contains multiple cases. Each case starts with three integers: the length of the board (in meters, at least 3), the weight of the board (in kilograms) and n the number of packages on the board (n <= 20). The board is supported at positions -1.5 and +1.5 by two equal fulcrums, both two meters tall and standing on a flat floor. The following n lines contain two integers each: the position of a package on board (in meters measured from the center, negative means to the left) and the weight of the package (in kilograms). A line containing three 0's ends the input. For each case you are to output the number of the case in the format shown below and then n lines each containing 2 integers, the position of a package and its weight, in an order in which the packages can be removed without causing the board to tip. If there is no solution for a case, output a single line Impossible. There is no solution if in the initial configuration the board is not balanced.

Sample input

20 3 6
-8 4
-4 10
-3 10
2 4
5 7
8 8
20 3 15
1 10 
8 5
-6 8
5 9
-8 4
8 10
-3 10
-4 5
2 9
-2 2
3 3
-3 2
5 1
-6 1
2 5
30 10 2
-8 100
9 91
0 0 0 

Possible Output for sample input

Case 1:
-4 10
8 8
-8 4
5 7
-3 10
2 4
Case 2:
1 10 
8 5
-6 8
5 9
-8 4
8 10
-3 10
-4 5
2 9
-2 2
3 3
-3 2
5 1
-6 1
2 5
Case 3:
Impossible

题意:给定一块木板长度l,重量w,和上面放了n个木块,下面nn木块信息每个木块放置位置重量。现在已知木板两个支点为-1.5和1.5位置。要求出一个把木块拿下来的顺序。保证木板一直是平衡的。输出这个顺序,如果做不到就输出Impossible。。

思路:题目中有两个支点根据物理学,可以证明,当左支点左边的力距大于左支点右边的力距时,和右支点右力距大于右支点左力距时,会失去平衡。还有如果木块是放在-1.5 到 1.5之间,那么木块只会使木板更平衡。所以我们可以用贪心的思想。把中间的木块最后拿掉。

接着我们把左边的木块和右边的木块分成两堆。进行力距从小到大的排序。然后把思路反过来想,可以转换成,把木块一个个放上去,仍然保持平衡。这时候。我们从力距小的开始放,可以使得木板最不可能失去平衡。然后之前分成两堆是因为。木块一般是交替放置的。这样左边往上不符合,就放右边,反之,当右边不符合,就放左边。这样贪心可极大减少时间。

直到木块全部放置完毕就结束。

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;

struct M {
    int l;
    int w;
    double ll;
    double lz;
    double rr;
    double rz;
} ml[35], mr[35];

bool cmpl(M a, M b) {
    return a.ll < b.ll;
}
bool cmpr(M a, M b) {
    return a.rr < b.rr;
}
int numm;
int numl, numr;
int l, w, n;
double ll, lz, rr, rz;
int a, b;
int judge;
int out[35][2];

void dfs(double ll, double lz, double rr, double rz, int num, int numll, int numrr) {
    if (judge)
	return;
    if (ll > lz || rr > rz)
	return;
    if (num == n) {
	judge = 1;
	return;
    }
    out[num][0] = ml[numll].l;
    out[num][1] = ml[numll].w;
    if (numll != numl)
	dfs(ll + ml[numll].ll, lz, rr, rz + ml[numll].rz, num + 1, numll + 1, numrr);
    if (judge)
	return;
    out[num][0] = mr[numrr].l;
    out[num][1] = mr[numrr].w;
    if (numrr != numr)
	dfs(ll, lz + mr[numrr].lz, rr + mr[numrr].rr, rz, num + 1, numll, numrr + 1);
}
int main()
{
    int t = 1;
    while (scanf("%d%d%d", &l, &w, &n) != EOF && l && w && n) {
	memset(ml, 0, sizeof(ml));
	memset(mr, 0, sizeof(mr));
	memset(out, 0, sizeof(out));
	numl = numr = numm = 0;
	judge = 0;
	ll = rr = (l - 3) * (l - 3) * w / (4.0 * l);
	lz = rz = (l + 3) * (l + 3) * w / (4.0 * l);
	l *= 2;
	for (int i = 0; i < n; i ++) {
	    scanf("%d%d", &a, &b);
	    a *= 2;
	    if (abs(a) <= 3) {
		out[numm][0] = a / 2;
		out[numm++][1] = b;
		if (a < 0) {
		    lz += ((3 - abs(a)) * b) * 1.0;
		    rz += ((3 + abs(a)) * b) * 1.0;
		}
		else {
		    lz += ((3 + abs(a)) * b) * 1.0;
		    rz += ((3 - abs(a)) * b) * 1.0;
		}
	    }
	    else {
		if (a < 0) {
		    ml[numl].l = a / 2;
		    ml[numl].w = b;
		    ml[numl].ll = ((abs(a) - 3) * b) * 1.0;
		    ml[numl ++].rz = ((abs(a) + 3) * b) * 1.0;
		}
		if (a > 0) {
		    mr[numr].l = a / 2;
		    mr[numr].w = b;
		    mr[numr].rr = ((abs(a) - 3) * b) * 1.0;
		    mr[numr ++].lz = ((abs(a) + 3) * b) * 1.0;
		}
	    }
	}
	sort(ml, ml + numl, cmpl);
	sort(mr, mr + numr, cmpr);
	dfs(ll, lz, rr, rz, numm, 0, 0);
	printf("Case %d:\n", t ++);
	if (judge) {
	    for (int i = n - 1; i >= 0; i --)
		printf("%d %d\n", out[i][0], out[i][1]);
	}
	else
	    printf("Impossible\n");
    }
    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、付费专栏及课程。

余额充值