Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <climits>
  4.  
  5. using namespace std;
  6. int b[1000][1000];
  7. int c[1000][1000];//costul optim pentru traseul care incepe de acolo
  8. int n;
  9. int m;
  10.  
  11. ifstream fin("date.in");
  12.  
  13. class Point{
  14. public:
  15.     int x, y;
  16.     Pointe(int x = 0, int y = 0) {}
  17.     void setLocation(int x_, int y_)
  18.     {
  19.         x = x_;
  20.         y = y_;
  21.     }
  22. };
  23.  
  24. void citire()
  25. {
  26.     fin >> n;
  27.     fin >> m;
  28.  
  29.     for (int i = 0; i < n; ++i)
  30.     {
  31.         for (int j = 0; j < m; ++j)
  32.             fin >> b[i][j];
  33.     }
  34. }
  35.  
  36. int get(int l, int col)
  37. {
  38.     if (l < 0 || l >= n)
  39.         return INT_MIN;
  40.     if (col < 0 || col >= m)
  41.         return INT_MIN;
  42.     return c[l][col];
  43. }
  44.  
  45. int max(int a, int b)
  46. {
  47.     return a > b ? a : b;
  48. }
  49.  
  50. int main()
  51. {
  52.     citire();
  53.     for (int i = 0; i < n; ++i)
  54.         c[i][m - 1] = b[i][m - 1];
  55.  
  56.     for (int col = m - 2; col >= 0; --col)
  57.     {
  58.         for (int lin = 0; lin < n; ++lin)
  59.         {
  60.             c[lin][col] = b[lin][col] + max(get(lin - 1, col + 1), get(lin + 1, col + 1));
  61.             c[lin][col] = max(c[lin][col], b[lin][col] + get(lin, col + 1));
  62.         }
  63.     }
  64.  
  65. //    for(int i = 0; i < n; i++)
  66. //    {
  67. //        for(int j = 0; j < m; j++)
  68. //            cout << c[i][j] << " ";
  69. //        cout << endl;
  70. //    }
  71.  
  72.     bool unic = true;
  73.     int maxVal = -1;
  74.     Point p;
  75.     for (int l = 0; l < n; ++l)
  76.     {
  77.         if (c[l][0] > maxVal)
  78.         {
  79.             maxVal = c[l][0];
  80.             p.x = l;
  81.         }
  82.         else
  83.             if (c[l][0] == maxVal)
  84.                 unic = false;
  85.     }
  86.     cout << maxVal << endl;
  87.     cout << p.x + 1 << " " << p.y + 1<< endl;
  88.     for (int col = 0; col < m - 1; ++col)
  89.     {
  90.         int ne = get(p.x - 1, col + 1);
  91.         int e = get(p.x, col + 1);
  92.         int se = get(p.x + 1, col + 1);
  93.  
  94.         if (ne == e || ne == se || e == se)
  95.         {
  96.             unic = false;
  97.         }
  98.         if (ne > e)
  99.         {
  100.             p.setLocation(p.x - 1, col + 1);
  101.         }
  102.         else if (e > se)
  103.         {
  104.             p.setLocation(p.x, col + 1);
  105.         }
  106.         else
  107.         {
  108.             p.setLocation(p.x + 1, col + 1);
  109.         }
  110.         cout << p.x + 1 << " " << p.y + 1 << endl;
  111.     }
  112.  
  113.     if (unic)
  114.     {
  115.         cout << "traseu unic";
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement