Advertisement
shchuko

Turtle

May 17th, 2019
495
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. int analyse(int **iData, int **oData, int _high, int _width, int x, int y) {
  7.  
  8.     int returnValue = 0;
  9.  
  10.     if (oData[x][y] == -1) {
  11.         int scoreX = -1, scoreY = -1;
  12.  
  13.         if (x != 0) {
  14.             scoreX = analyse(iData, oData, _high, _width, x - 1, y);
  15.         }
  16.  
  17.         if (y != _width - 1) {
  18.             scoreY = analyse(iData, oData, _high, _width, x, y + 1);
  19.         }
  20.  
  21.         if ( scoreX == -1 && scoreY == -1 ) {
  22.             returnValue = 0;
  23.         } else {
  24.             if (scoreX > scoreY) {
  25.                 returnValue = scoreX;
  26.             } else {
  27.                 returnValue = scoreY;
  28.             }
  29.         }
  30.  
  31.         oData[x][y] = iData[x][y] + returnValue;
  32.     }
  33.  
  34.     return oData[x][y];
  35. }
  36.  
  37. int main()
  38. {
  39.     ifstream fin("turtle.in");
  40.  
  41.     int high = 0, width = 0;
  42.     fin >> high >> width;
  43.  
  44.     int **a = new int*[high];
  45.     int **scores = new int*[high];
  46.  
  47.     for (int i = 0; i < high; i++) {
  48.         a[i] = new int[width];
  49.         scores[i] = new int[high];
  50.  
  51.         for (int j = 0; j < width; j++) {
  52.             fin >> a[i][j];
  53.             scores[i][j] = -1;
  54.         }
  55.     }
  56.  
  57.     ofstream fout("turtle.out");
  58.     fout << analyse(a, scores, high, width, high - 1, 0);
  59.  
  60.     for (int i = 0; i < high; i++) {
  61.         delete[] a[i];
  62.         delete[] scores[i];
  63.     }
  64.  
  65.     delete[] a;
  66.     delete[] scores;
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement