No Tipping +回溯+物理+贪心

本文介绍了一个关于保持杠杆平衡的问题,并提出了一种结合物理知识、回溯算法和剪枝策略的解决方案。通过合理安排物体移除顺序,确保杠杆始终保持平衡状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

解决方案:做这道题,要用到物理的知识+回溯+剪枝。题目意思是,在一块长板上放着很多块物体,在1.5和-1.5处有支点,从板上抽出物块,使长板不倾斜,求拿出物品的顺序。如果直接暴力回溯,那肯定超时了。所以,要制定一个策略。首先确定,放在-1.5和1.5之间的只会使木板更稳定;然后,若支点为1.5为情况1,-1.5为情况2 。则当wl1>wr1,或wr2>wl2时,都不会倾斜。所以可制定以下策略:

1)把拿出的操作换成放回;

2)在-1.5和1.5之间的肯定先放;

3)可把力矩分成左和右两类,然后分别对这两类的力矩的绝对值重小到大排序;

4)放置的规则为,小力矩的先放,如果左边放不了,放右边,都放不了则可肯定结果是“Impossible”

5) 最后按照放回的顺序逆序输出就可以了。



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int len,n;
int weight;
struct a_banlance{
   int position;
   int weight;
}B[3][100],rec[100];
int cnt[3],ok,pace[100];
double wr1,wl1,wr2,wl2;
bool cmp(a_banlance a,a_banlance b){

   return abs(a.position*a.weight)<abs(b.position*b.weight);
}
void dfs(int cur,int dir,int bo){

  if(cur>=n){
    ok=1;
    return ;
  }
  int yes=0;
  for(int i=pace[dir];i<cnt[dir];i++){
    if(dir==0){
        if(wr2<(wl2+(-3-B[dir][i].position)*B[dir][i].weight)){
            pace[dir]=i;
            break;
        }
        wl2+=(-3-B[dir][i].position)*B[dir][i].weight;
        wl1+=(3-B[dir][i].position)*B[dir][i].weight;
    }
    else {
        if((wr1+(-3+B[dir][i].position)*B[dir][i].weight)>wl1)
        {
            pace[dir]=i;
            break;
        }
        wr1+=(-3+B[dir][i].position)*B[dir][i].weight;
        wr2+=(3+B[dir][i].position)*B[dir][i].weight;
    }
    yes=1;
    pace[dir]=i+1;
    rec[cur++]=B[dir][i];
  }
  if(!yes&&!bo){
    ok=-1;
    return ;
  }
  dfs(cur,2-dir,yes);
  if(ok)
    return ;
}
int main(){
    int p,w,k=0;
    while(~scanf("%d%d%d",&len,&weight,&n)&&(len+weight+n)){
       memset(cnt,0,sizeof(cnt));
       memset(pace,0,sizeof(pace));
       memset(rec,0,sizeof(rec));
       memset(B,0,sizeof(B));
       wl1=wr2=(len+3)*(len+3)*weight/(4.0*len);
       wl2=wr1=(len-3)*(len-3)*weight/(4.0*len);
        for(int i=0;i<n;i++){
            scanf("%d%d",&p,&w);
           p*=2;
        if(p>3){
            B[2][cnt[2]].position=p;
            B[2][cnt[2]].weight=w;
            cnt[2]++;
        }
        else if(p<-3){
            B[0][cnt[0]].position=p;
            B[0][cnt[0]].weight=w;
            cnt[0]++;
        }
        else {
            B[1][cnt[1]].position=p;
            B[1][cnt[1]].weight=w;
            cnt[1]++;
        }
        }
    for(int i=0;i<3;i++)
        sort(B[i],B[i]+cnt[i],cmp);
    memcpy(rec,B[1],sizeof(B[1]));
    for(int i=0;i<cnt[1];i++){
        wl1+=(3-B[1][i].position)*B[1][i].weight;
        wr2+=(3+B[1][i].position)*B[1][i].weight;
    }
    ok=0;
    dfs(cnt[1],0,1);
    cout<<"Case "<<++k<<":\n";
    if(ok==1){
            for(int i=n-1;i>=0;i--){
                printf("%d %d\n",rec[i].position/2,rec[i].weight);
            }

    }
    else printf("Impossible\n");
    }
    return 0;
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值