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.
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.
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 0Possible 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重量M的木板上放n个木块,下n行表示木块放的位置和木块的重量,开始木板是处于平衡的,每次拿下一个木块,直到木块全部被取下,期间要求木板不发生偏移,如果可以完全取下,就输出去下木块的顺序(情况不唯一),不能完全取下就输出Impossible。
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<algorithm> using namespace std; struct block { int p, w; }; block L[20], R[20], M[20]; int fL, fR, cL, cR, move; int cmp1(block a, block b) { return (a.p + 3) * a.w > (b.p + 3) * b.w; //排序坐标-1.5左边的木块 } int cmp2(block a, block b) { return (a.p - 3) * a.w < (b.p - 3) * b.w; //排序坐标1.5右边的木块 } int check(block *b, int &cnt, int C) { //检查放上b(L/R)边的编号为cnt的木块后,b(L/R)边是否平衡 if (cnt >= C) return 0; int temp1, temp2; temp1 = (b[cnt].p + 3) * b[cnt].w; //放上该木块对左右两边的影响 temp2 = (b[cnt].p - 3) * b[cnt].w; if (fL + temp1 >= 0 && fR + temp2 <= 0) { //判断放上该木块是否会导致不平衡 fL += temp1; fR += temp2; M[move++] = b[cnt]; cnt++; return 1; } return 0; } int put(int c1, int c2) { //逆向思维:题目要求一个一个拿,保持平衡;这里采用逆向思维,在保持平衡的基础上,从零个开始一个一个放,最后逆序输出 do { while (check(L, c1, cL)); while (check(R, c2, cR)); } while (check(L, c1, cL)); if (c1 == cL && c2 == cR) { return 1; } return 0; } int main() { int l, w, n, Case = 1; while (scanf("%d %d %d", &l, &w, &n) == 3, (l || w || n)) { l *= 2; //乘以2,避免小数点 fL = 3 * w; //左侧木板的重量是相对于右侧支点的,将右侧支点左侧的所有木板看成质点,坐标为0,所以力臂为3,F = (0 + 3) * w fR = -fL; //右边重量记为负,便于计算 cL = cR = 0; move = 0; for (int i = 0; i < n; i++) { int pos, we; scanf("%d %d", &pos, &we); pos *= 2; if (pos >= -3 && pos <= 3) { //存在于中部的木块使得木板更稳固 fL += (pos + 3) * we; // 左边的重量是相对右边的支点的,所以要加3 fR += (pos - 3) * we; // 右边的重量是相对左边的支点的,所以要减3 M[move].p = pos; M[move++].w = we; } else if (pos > 3) { R[cR].p = pos; R[cR++].w = we; } else { L[cL].p = pos; L[cL++].w = we; } } sort(L, L + cL, cmp1); sort(R, R + cR, cmp2); int flag = put(0, 0); printf("Case %d:\n", Case++); if (!flag) printf("Impossible\n"); else { while (--move >= 0) { printf("%d %d\n", M[move].p / 2, M[move].w); } } } return 0; }
原文地址:http://blog.csdn.net/llx523113241/article/details/43637669