Advertisement
Guest User

Untitled

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