Uva--10123(回溯,强剪枝)

2014-07-19 23:49:21

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 lineImpossible. 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

思路:这题可以用离线算法的角度来考虑,先把所有要放的packages读进来,然后逆过来考虑:一开始天平为空,然后逐个放package。
需要剪枝的地方有2处:(1)先把坐标在-1.5到1.5之间的packages放上天平(因为这些packages不会影响当前天平的平衡)(剪枝)
(2)把packages分成两份,左支点左边 and 右支点右边,然后按照力矩升序进行排序(后面有用)。这样在递归过程中可以在优先递归左边,把左边放到不能放为止,
这时排序的好处就体现了:如果放完第k个就不能再放了,那么k之后的也不能在放了,不用考虑(剪枝)。然后递归右边,过程类似前面。
  1 /*************************************************************************
  2     > File Name: Uva10123.cpp
  3     > Author: Nature
  4     > Mail: 564374850@qq.com
  5     > Created Time: Sat 19 Jul 2014 08:51:10 PM CST
  6  ************************************************************************/
  7 
  8 #include<iostream>
  9 #include<cmath>
 10 #include<algorithm>
 11 #include<cstring>
 12 #include<cstdio>
 13 #include<fstream>
 14 using namespace std;
 15 
 16 int cnt,cnt1,cnt2,cnt3,n;
 17 double L,W,tra[25][2];
 18 
 19 struct pac{
 20     double w,p;
 21     double ltor,rtor;
 22 };
 23 pac pa1[25],pa2[25],pa3[25];
 24 
 25 bool Dfs(int cur,double ll,double rr,int cl,int cr){
 26     if(cur >= cnt)
 27         return true;
 28     if(cl < cnt1){
 29         if(ll + pa1[cl].ltor <= 0 && rr + pa1[cl].rtor <= 0){
 30             tra[cur][0] = pa1[cl].p;
 31             tra[cur][1] = pa1[cl].w;
 32             if(Dfs(cur + 1,ll + pa1[cl].ltor,rr + pa1[cl].rtor,cl + 1,cr))
 33                 return true;
 34         }
 35     }
 36     if(cr < cnt2){
 37         if(ll + pa2[cr].ltor <= 0 && rr + pa2[cr].rtor <= 0){
 38             tra[cur][0] = pa2[cr].p;
 39             tra[cur][1] = pa2[cr].w;
 40             if(Dfs(cur + 1,ll + pa2[cr].ltor,rr + pa2[cr].rtor,cl,cr + 1))
 41                 return true;
 42         }
 43     }
 44     return false;
 45 }
 46 
 47 void Init(){
 48     cnt = n;
 49     cnt1 = 0;
 50     cnt2 = 0;
 51     cnt3 = 0;
 52     memset(pa1,0,sizeof(pa1));
 53     memset(pa2,0,sizeof(pa2));
 54     memset(pa3,0,sizeof(pa3));
 55     memset(tra,0,sizeof(tra));
 56 }
 57 
 58 bool cmp1(pac a,pac b){
 59     return fabs(a.ltor) < fabs(b.ltor);
 60 }
 61 
 62 bool cmp2(pac a,pac b){
 63     return fabs(a.rtor) < fabs(b.rtor);
 64 }
 65 
 66 int main(){
 67     int Case = 0;
 68     double LL,RR,tp,tw;
 69     while(scanf("%lf%lf%d",&L,&W,&n) == 3){
 70        if(!L && !W && !n) break;
 71        Init();
 72        LL = -1.5 * W;
 73        RR = -1.5 * W;
 74        cnt = n;
 75        for(int i = 0; i < n; ++i){
 76            scanf("%lf%lf",&tp,&tw);
 77            if(tp >= -1.5 && tp <= 1.5){
 78                pa3[cnt3++].p = tp;
 79                pa3[cnt3++].w = tw;
 80                LL += tw * (-1.5 - tp);
 81                RR += tw * (tp - 1.5);
 82                   --cnt;
 83            }
 84            else if(tp < -1.5){
 85                pa1[cnt1++].p = tp;
 86                pa1[cnt1++].w = tw;
 87                pa1[cnt1++].ltor = tw * (-1.5 - tp);
 88                pa1[cnt1++].rtor = tw * (tp - 1.5);
 89            }
 90            else{
 91                pa2[cnt2++].p = tp;
 92                pa2[cnt2++].w = tw;
 93                pa2[cnt2++].ltor = tw * (-1.5 - tp);
 94                pa2[cnt2++].rtor = tw * (tp - 1.5);
 95            }
 96        }
 97        sort(pa1,pa1 + cnt1,cmp1);
 98        sort(pa2,pa2 + cnt2,cmp2);
 99        printf("Case %d:\n",++Case);
100        if(!Dfs(0,LL,RR,0,0)) // 在线转离线:倒过来考虑,逐个放上天平
101           printf("Impossible\n");
102        else{
103            for(int i = cnt - 1; i >= 0; --i)
104                printf("%.0lf %.0lf\n",tra[i][0],tra[i][1]);
105            for(int i = 0; i < cnt3; ++i)
106                printf("%.0lf %.0lf\n",pa3[i].p,pa3[i].w);
107        }
108     }
109     return 0;
110 }

 

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3855672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值