【三种解法】Not so Mobile UVA - 839_19行代码AC
立志用最少的代碼做最高效的表達
Before being an ubiquous communications gadget, a mobile was just a structure made of strings and wires suspending colourfull things. This kind of mobile is usually found hanging over cradles of small babies.
The figure illustrates a simple mobile. It is just a wire, suspended by a string, with an object on each side. It can also be seen as a kind of lever with the fulcrum on the point where the string ties the wire. From the lever principle we know that to balance a simple mobile the product of the weight of the objects by their distance to the fulcrum must be equal. That is Wl × Dl = Wr × Dr where Dl
is the left distance,
Dr is the right distance, Wl is the left weight and Wr is the right weight.
In a more complex mobile the object may be replaced by a sub-mobile, as shown in the next figure.
In this case it is not so straightforward to check if the mobile is balanced so we need you to write a program that, given a description of a mobile as input, checks whether the mobile is in equilibrium or not.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
The input is composed of several lines, each containing 4 integers separated by a single space.
The 4 integers represent the distances of each object to the fulcrum and their weights, in the format:
Wl Dl Wr Dr
If Wl or Wr is zero then there is a sub-mobile hanging from that end and the following lines define the the sub-mobile. In this case we compute the weight of the sub-mobile as the sum of weights of all its objects, disregarding the weight of the wires and strings. If both Wl and Wr are zero then the following lines define two sub-mobiles: first the left then the right one.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
Write ‘YES’ if the mobile is in equilibrium, write ‘NO’ otherwise.
Sample Input
1
0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2
Sample Output
YES
思路分析
經過觀察很容易發現,本題的輸入類似二叉樹的先序遍歷(即根左右)
以下三種解法為循序漸進的優化過程。
解法一:根據先序遍歷建樹, 然后后序遍歷判斷天平是否平衡
解法二:在建樹的同時判斷天平是否平衡
解法三:隱式建樹,通過遞歸的過程動態判斷天平是否平衡。
解法一:先序建樹、后序判斷
#include<bits/stdc++.h> using namespace std; struct Node {int wl, wr, dl, dr; //value Node *l = NULL, *r = NULL; //left subtree、right subtree };string s; int T; bool readInput(int& wl, int& dl, int &wr, int& dr) { //讀取輸入 if(getline(cin, s) && !s.empty()) {stringstream input(s); //stringstream用法 input >> wl >> dl >> wr >> dr;return true; //成功 }else return false; //失敗 }Node* createTree() { //遞歸建樹(先序) Node* root = NULL;int wl, dl, wr, dr;if(readInput(wl, dl, wr, d2)) { //讀入成功 root = new Node;root->wl = wl; root->wr = wr;root->dl = dl; root->dr = dr;if(wl == 0) root->l = createTree();if(wr == 0) root->r = createTree(); }return root; }int dfs(Node* root) { //后序遍歷并判斷是否平衡if(root == NULL) return -1; //空樹if(root->l != NULL) root->wl = dfs(root->l); //左子樹if(root->r != NULL) root->wr = dfs(root->r); //右子樹if(root->wl == -1 || root->wr == -1) return -1; //非法,剪枝if(root->wl * root->dl == root->wr*root->dr) return root->wl+root->wr; //平衡,返回左右重量和else return -1; //非法標志 }int main() {scanf("%d ", &T); //兩個空格可吸收換行for(int i = 0; i < T; i++) {Node* bt = createTree();printf("%s%s\n", dfs(bt)!=-1 ? "YES" : "NO", i!=T-1?"\n":"");if(i != T-1) getchar(); //空行吸收 } return 0; }解法二:建樹的同時判斷
#include<bits/stdc++.h> using namespace std; struct Node{int Wl, Dl, Wr, Dr; //數據域Node* left = nullptr, *right = nullptr; //左右孩子指針 };int DFS(Node*&root, bool&f) {root = new Node();scanf("%d%d%d%d", &root->Wl, &root->Dl, &root->Wr, &root->Dr);if(root->Wl == 0) //Wl是0root->Wl = DFS(root->left, f); //遞歸建立左子天平if(root->Wr == 0) root->Wr = DFS(root->right, f);if(root->Dl*root->Wl != root->Dr*root->Wr) //力矩不等f = false;return root->Wl + root->Wr; }int main() {int n; scanf("%d", &n);for(int i = 0; i < n; i++) {Node *root = nullptr;bool f = true;DFS(root, f);printf("%s%s\n", i>0?"\n":"", f?"YES":"NO");}return 0; }解法三:隱式建樹
#include<iostream> using namespace std;bool solve(int &W) { //此處W類似靜態變量 int W1, D1, W2, D2;bool b1 = true, b2 = true;cin >> W1 >> D1 >> W2 >> D2;if(!W1) b1 = solve(W1);if(!W2) b2 = solve(W2);W = W1 + W2;return b1 && b2 && (W1*D1 == W2*D2); }int main() {int T, W; cin >> T;while(T--) {cout << solve(W) ? "YES\n" : "NO\n";if(T) putchar('\n');}return 0; }擇苦而安,擇做而樂,虛擬終究不比真實精彩之萬一
總結
以上是生活随笔為你收集整理的【三种解法】Not so Mobile UVA - 839_19行代码AC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python Thread 函数_Pyt
- 下一篇: 【最新合集】编译原理习题(含答案)_1