Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* FIB-UPC | PRO1 | Jutge.org | Llistes del control C3 | Llista P10 | P35971 F009A. Recorrent matrius */
- #include <iostream>
- #include <vector>
- using namespace std;
- typedef vector<vector<int> > Matriu;
- int suma_linia(const Matriu& mat, int of, int oc, int df, int dc) {
- int suma = 0;
- if (of == df) {
- if (oc < dc) {
- while (oc < dc) {
- ++oc;
- suma += mat[of][oc];
- }
- }
- else {
- while (oc > dc) {
- --oc;
- suma += mat[of][oc];
- }
- }
- }
- else {
- if (of < df) {
- while (of < df) {
- ++of;
- suma += mat[of][oc];
- }
- }
- else {
- while (of > df) {
- --of;
- suma += mat[of][oc];
- }
- }
- }
- return suma;
- }
- int main() {
- int f, c;
- cin >> f >> c;
- Matriu m(f, vector<int> (c));
- for (int f = 0; f < m.size(); ++f) for (int c = 0; c < m[0].size(); ++c) cin >> m[f][c];
- cin >> f >> c;
- int df, dc;
- int suma = m[f][c];
- while (cin >> df >> dc) {
- suma += suma_linia(m, f, c, df, dc);
- f = df;
- c = dc;
- }
- cout << "suma = " << suma << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement