Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- struct cur
- {
- bool f;
- int max = 0;
- int val;
- };
- void curr(cur* a, cur* b)
- {
- if (a->f == true)
- {
- if (b->val > a->max)
- {
- a->val = a->val - a->max;
- a->max = b->val;
- a->val = a->val + b->val;
- }
- }
- else
- {
- a->max = b->val;
- a->val = a->val + b->val;
- a->f = true;
- }
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- int n, m;
- cin >> n >> m;
- cur arr[10][10];
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> arr[i][j].val;
- arr[i][j].f = false;
- }
- }
- if (m == 0 || n == 0)
- {
- cout << "-";
- }
- else
- {
- arr[0][0].f = true;
- for (int i = 0; i < m; i++)
- {
- int t = i;
- int j = 0;
- while (t >= 0 && j < n)
- {
- if (j - 2 >= 0 && t - 1 >= 0 && arr[j - 2][t - 1].f)
- {
- curr(&arr[j][t], &arr[j - 2][t - 1]);
- }
- if (j - 2 >= 0 && t + 1 < m && arr[j - 2][t + 1].f)
- {
- curr(&arr[j][t], &arr[j - 2][t + 1]);
- }
- if (j - 1 >= 0 && t - 2 >= 0 && arr[j - 1][t - 2].f)
- {
- curr(&arr[j][t], &arr[j - 1][t - 2]);
- }
- if (j + 1 < n && t - 2 >= 0 && (arr[(j + 1)][(t - 2)].f))
- {
- curr(&arr[j][t], &arr[j + 1][t - 2]);
- }
- j++;
- t--;
- }
- }
- for (int i = 1; i < n; i++)
- {
- int t = m - 1;
- int j = i;
- while (t >= 0 && j < n)
- {
- if (j - 2 >= 0 && t - 1 >= 0 && arr[j - 2][t - 1].f)
- {
- curr(&arr[j][t], &arr[j - 2][t - 1]);
- }
- if (j - 2 >= 0 && t + 1 < m && arr[j - 2][t + 1].f)
- {
- curr(&arr[j][t], &arr[j - 2][t + 1]);
- }
- if (j - 1 >= 0 && t - 2 >= 0 && arr[j - 1][t - 2].f)
- {
- curr(&arr[j][t], &arr[j - 1][t - 2]);
- }
- if (j + 1 < n && t - 2 >= 0 && (arr[(j + 1)][(t - 2)].f))
- {
- curr(&arr[j][t], &arr[j + 1][t - 2]);
- }
- j++;
- t--;
- }
- }
- if (arr[n - 1][m - 1].f == true)
- {
- cout << arr[n - 1][m - 1].val;
- }
- else
- {
- cout << "-";
- }
- }
- cin >> m;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement