Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS 0
- #include <iostream>
- #include <math.h>
- using namespace std;
- int n, m, a[1000][1000], b[1000][1000], c[1000][1000];
- void horse ( int i , int j ){
- if (i + 1 < n && j + 2 < m) {
- if (c[i + 1][j + 2] == a[i + 1][j + 2] || a[i + 1][j + 2] + c[i][j] > c[i + 1][j + 2]){
- c[i + 1][j + 2] = a[i + 1][j + 2] + c[i][j];
- horse(i + 1, j + 2);
- }
- b[i + 1][j + 2] = 1;
- }
- if (i + 2 < n && j + 1 < m) {
- if (c[i + 2][j + 1] == a[i + 2][j + 1] || a[i + 2][j + 1] + c[i][j] > c[i + 2][j + 1]){
- c[i + 2][j + 1] = a[i + 2][j + 1] + c[i][j];
- horse(i + 2, j + 1);
- }
- b[i + 2][j + 1] = 1;
- }
- if (i - 1 >= 0 && j + 2 < m) {
- if (c[i - 1][j + 2] == a[i - 1][j + 2] || a[i - 1][j + 2] + c[i][j] > c[i - 1][j + 2]){
- c[i - 1][j + 2] = a[i - 1][j + 2] + c[i][j];
- horse(i - 1, j + 2);
- }
- b[i - 1][j + 2] = 1;
- }
- if (i + 2 < n && j - 1 >= 0) {
- if (c[i + 2][j - 1] == a[i + 2][j - 1] || a[i + 2][j - 1] + c[i][j] > c[i + 2][j - 1]){
- c[i + 2][j - 1] = a[i + 2][j - 1] + c[i][j];
- horse(i + 2, j - 1);
- }
- b[i + 2][j - 1] = 1;
- }
- }
- int main() {
- freopen("input.txt", "r", stdin);
- freopen("output.txt", "w", stdout);
- cin >> n >> m;
- for (int i = 0; i < n; i++){
- for (int j = 0; j < m; j++){
- cin >> a[i][j];
- c[i][j] = a[i][j];
- b[i][j] = 0;
- }
- }
- if (n > 2 || m > 2){
- horse(0 , 0);
- }
- if ( b[n-1][m-1] == 1 )
- cout << c[n-1][m-1];
- else
- cout << "-";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement