JosepRivaille

P35971: F009A. Recorrent matrius

Jun 2nd, 2015
927
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. typedef vector< vector<int> > Matriu;
  7.  
  8. int suma_linia(const Matriu& mat, int of, int oc, int df, int dc) {
  9.     int suma = 0;
  10.     int baixar = 2;
  11.     int dreta = 2;
  12.     if (of == df && oc < dc) dreta = 1;
  13.     else if (of == df && oc > dc) dreta = 0;
  14.     else if (oc == dc && of < df) baixar = 1;
  15.     else if (oc == dc && of > df) baixar = 0;
  16.    
  17.     if (dreta == 1) {
  18.         while (oc < dc) {
  19.             ++oc;
  20.             suma += mat[of][oc];
  21.         }
  22.     }
  23.     else if (dreta == 0) {
  24.         while (oc > dc) {
  25.             --oc;
  26.             suma += mat[of][oc];
  27.         }
  28.     }
  29.     else if (baixar == 1) {
  30.         while (of < df) {
  31.             ++of;
  32.             suma += mat[of][oc];
  33.         }
  34.     }
  35.     else if (baixar == 0) {
  36.         while (of > df) {
  37.             --of;
  38.             suma += mat[of][oc];
  39.         }
  40.     }
  41.    
  42.    
  43.     return suma;
  44. }
  45.  
  46. void read_matrix(Matriu& M) {
  47.     int f = M.size();
  48.     int c = M[0].size();
  49.     for (int i = 0; i < f; ++i) {
  50.         for (int j = 0; j < c; ++j) {
  51.             cin >> M[i][j];
  52.         }  
  53.     }  
  54. }
  55.  
  56. int main() {
  57.     int f, c;
  58.     cin >> f >> c;
  59.     Matriu M(f, vector<int> (c));
  60.     read_matrix(M);
  61.     int x, y, suma = 0;
  62.     bool primer = true;
  63.     int of, oc;
  64.     while (cin >> x >> y) {
  65.         if (primer) {
  66.             suma = M[x][y];
  67.             primer = false;
  68.         }
  69.         else {
  70.             suma += suma_linia(M, of, oc, x, y);
  71.         }
  72.         of = x;
  73.         oc = y;
  74.     }
  75.     cout << "suma = " << suma << endl;
  76. }
  77.  
  78. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment