Algorithm


problem Link : https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1981 

This in not “another brick in the wall”, it’s just a matter of adding numbers. Suppose you have a wall with the shape of a triangle, like the one shown below. The wall has 9 rows and row i has exactly i bricks, considering that top row is the first one and bottom row is the ninth. Some bricks are labeled with a number and other ones are blank. Notice that labeled bricks appear only on odd rows and they occupy odd positions within the row. The problem you must solve is finding a suitable number for each blank brick taking into account one simple rule: the number of a brick is obtained by adding the numbers of the two bricks below it. Obviously, this rule does not apply to the ninth row. Numbers are supposed to be integers. Input The first line of the input contains an integer N, indicating the number of test cases. This line is followed by the lines corresponding to the test cases. Each test case is described in five lines. These five lines correspond to odd rows of the wall, from top to bottom, as described above. Line i contains the numbers corresponding to odd bricks on row i of the wall (that is, non blank bricks), enumerated from left to right and separated with a single space. It is supposed that each test case is correct, that is, there exists a solution to the problem that the case describes. Output For each test case, the output should consist of nine lines describing the numbers of all bricks of the wall. So, line i should contain the numbers corresponding to the i bricks on row i of the wall, enumerated from left to right and separated by a single space. Note: Here we have an example with two test cases. The first one corresponds to the wall depicted above.

Sample Input 2 255 54 67 10 18 13 3 3 5 2 2 1 2 1 1 256 64 64 16 16 16 4 4 4 4 1 1 1 1 1

Sample Output 255 121 134 54 67 67 23 31 36 31 10 13 18 18 13 5 5 8 10 8 5 3 2 3 5 5 3 2 2 1 1 2 3 2 1 1 2 0 1 0 2 1 1 0 1 256 128 128 64 64 64 32 32 32 32 16 16 16 16 16 8 8 8 8 8 8 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1

Code Examples

#1 Code Example with C Programming

Code - C Programming

#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
using namespace std;

int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        vector < vector<int> > res(9,vector<int>());
        int cur;
        int prev;
        for(int i=0;i < 9;i+=2){
            scanf("%d",&prev);
            res[i].push_back(prev);
            for(int j=0;j i+It i/2;j++){
                scanf("%d",&cur);
                // solve for previous missing item
                // Target = Prev + Cur + 2*Missing
                int targetIndex = res[i].size()-1;
                int target = res[i-2][targetIndex];
                int missing = (target - prev - cur)/2;
                res[i].push_back(missing);
                res[i].push_back(cur);
                // solve missing row
                res[i-1].push_back(missing+prev);
                res[i-1].push_back(missing+cur);
                prev = cur;
            }
        }
        for(auto vec : res){
            for(auto& i : vec)
                cout << i   i+It (&i != &vec.back() ? " " : "\n");
        }
    }
}

Copy The Code & Try With Live Editor

Input

x
+
cmd
2
255
54 67
10 18 13
3 3 5 2
2 1 2 1 1
256
64 64
16 16 16
4 4 4 4
1 1 1 1 1

Output

x
+
cmd
255
121 134
54 67 67
23 31 36 31
10 13 18 18 13
5 5 8 10 8 5
3 2 3 5 5 3 2
2 1 1 2 3 2 1 1
2 0 1 0 2 1 1 0 1
256
128 128
64 64 64
32 32 32 32
16 16 16 16 16
8 8 8 8 8 8
4 4 4 4 4 4 4
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1 1
Advertisements

Demonstration


UVA Online Judge solution - 11040-Add_bricks_in_the_wall - UVA Online Judge solution in C,C++,java

Previous
UVA Online Judge solution - 11039-Building designing - UVA Online Judge solution in C,C++,java
Next
UVA Online Judge solution - 11053-Flavius Josephus Reloaded - UVA Online Judge solution in C,C++,java