Advertisement
fibernalia

P35971 - F009A. Recorrent matrius

Dec 9th, 2018
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. /* FIB-UPC | PRO1 | Jutge.org | Llistes del control C3 | Llista P10 | P35971 F009A. Recorrent matrius */
  2.  
  3. #include <iostream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. typedef vector<vector<int> > Matriu;
  8.  
  9. int suma_linia(const Matriu& mat, int of, int oc, int df, int dc) {
  10.     int suma = 0;
  11.     if (of == df) {
  12.         if (oc < dc) {
  13.             while (oc < dc) {
  14.                 ++oc;
  15.                 suma += mat[of][oc];
  16.             }
  17.         }
  18.         else {
  19.             while (oc > dc) {
  20.                 --oc;
  21.                 suma += mat[of][oc];
  22.             }
  23.         }
  24.     }
  25.     else {
  26.         if (of < df) {
  27.             while (of < df) {
  28.                 ++of;
  29.                 suma += mat[of][oc];
  30.             }
  31.         }
  32.         else {
  33.             while (of > df) {
  34.                 --of;
  35.                 suma += mat[of][oc];
  36.             }
  37.         }
  38.     }
  39.     return suma;
  40. }
  41.  
  42. int main() {
  43.     int f, c;
  44.     cin >> f >> c;
  45.     Matriu m(f, vector<int> (c));
  46.     for (int f = 0; f < m.size(); ++f) for (int c = 0; c < m[0].size(); ++c) cin >> m[f][c];
  47.     cin >> f >> c;
  48.     int df, dc;
  49.     int suma = m[f][c];
  50.     while (cin >> df >> dc) {
  51.         suma += suma_linia(m, f, c, df, dc);
  52.         f = df;
  53.         c = dc;
  54.     }
  55.     cout << "suma = " << suma << endl;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement