Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <vector>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. const int ARGUMENTS_COUNT = 2;
  9.  
  10. struct Node
  11. {
  12. int price = 0;
  13. pair<int, int> next;
  14. };
  15.  
  16. void ReadField(ifstream& file, vector<vector<Node>>& field, size_t& sizeX, size_t& sizeY);
  17.  
  18. void CheckLastRow(vector<vector<Node>>& field, int sizeX, int sizeY);
  19. void CheckLastColl(vector<vector<Node>>& field, int sizeX, int sizeY);
  20. void CheckColls(vector<vector<Node>>& field, int sizeX, int sizeY);
  21.  
  22. void PrintMatrix(vector<vector<Node>>& field, int sizeX, int sizeY);
  23. void PrintSumAndWay(vector<vector<Node>> const& field, int sizeX, int sizeY);
  24.  
  25. int main(int argc, char* argv[])
  26. {
  27. if (argc != ARGUMENTS_COUNT)
  28. {
  29. return 0;
  30. }
  31.  
  32. ifstream file(argv[1]);
  33.  
  34. size_t sizeX = 0;
  35. size_t sizeY = 0;
  36. vector<vector<Node>> field;
  37.  
  38. ReadField(file, field, sizeX, sizeY);
  39.  
  40. CheckLastRow(field, sizeX, sizeY);
  41. CheckLastColl(field, sizeX, sizeY);
  42. CheckColls(field, sizeX, sizeY);
  43.  
  44. PrintSumAndWay(field, sizeX, sizeY);
  45.  
  46. return 0;
  47. }
  48.  
  49. void PrintMatrix(vector<vector<Node>>& field, int sizeX, int sizeY)
  50. {
  51. for (int y = 0; y < sizeY; y++)
  52. {
  53. for (int x = 0; x < sizeX; x++)
  54. {
  55. cout << field[y][x].price << "(" << field[y][x].next.first << ", " << field[y][x].next.second << ") ";
  56. }
  57. cout << endl;
  58. }
  59. }
  60.  
  61. void PrintSumAndWay(vector<vector<Node>> const&field, int sizeX, int sizeY)
  62. {
  63. cout << field[0][0].price << endl;
  64.  
  65. pair<int, int> startPair = pair<int, int>(0, 0);
  66. while (startPair.first != sizeY - 1 || startPair.second != sizeX - 1)
  67. {
  68. cout << "(" << startPair.first + 1 << ", " << startPair.second + 1 << ")" << endl;
  69. startPair = field[startPair.first][startPair.second].next;
  70. }
  71.  
  72. cout << "(" << sizeY << ", " << sizeX << ")" << endl;
  73. }
  74.  
  75. void CheckLastRow(vector<vector<Node>>& field, int sizeX, int sizeY)
  76. {
  77. int x = sizeX - 2;
  78. int y = sizeY - 1;
  79.  
  80. while (x >= 0)
  81. {
  82. field[y][x].price = field[y][x].price + field[y][x + 1].price;
  83. field[y][x].next = pair<int, int>(y, x + 1);
  84. x--;
  85. }
  86. }
  87.  
  88. void CheckLastColl(vector<vector<Node>>& field, int sizeX, int sizeY)
  89. {
  90. int x = sizeX - 1;
  91. int y = sizeY - 2;
  92.  
  93. while (y >= 0)
  94. {
  95. field[y][x].price = field[y][x].price + field[y + 1][x].price;
  96. field[y][x].next = pair<int, int>(y + 1, x);
  97. y--;
  98. }
  99. }
  100.  
  101. void CheckColls(vector<vector<Node>>& field, int sizeX, int sizeY)
  102. {
  103. int x = sizeX - 2;
  104. int y = sizeY - 2;
  105.  
  106. while (y >= 0)
  107. {
  108. while (x >= 0)
  109. {
  110. if (field[y][x].price + field[y][x + 1].price < field[y][x].price + field[y + 1][x].price)
  111. {
  112. field[y][x].price += field[y][x + 1].price;
  113. field[y][x].next = pair<int, int>(y, x + 1);
  114. }
  115. else
  116. {
  117. field[y][x].price += field[y + 1][x].price;
  118. field[y][x].next = pair<int, int>(y + 1, x);
  119. }
  120. x--;
  121. }
  122. x = sizeX - 2;
  123. y--;
  124. }
  125. }
  126.  
  127. void ReadField(ifstream& file, vector<vector<Node>>& field, size_t& sizeX, size_t& sizeY)
  128. {
  129. file >> sizeY;
  130. file >> sizeX;
  131.  
  132. for (size_t i = 0; i < sizeY; i++)
  133. {
  134. auto vec = new vector<Node>();
  135. field.push_back(*vec);
  136. for (size_t j = 0; j < sizeX; j++)
  137. {
  138. int tmpPrice = 0;
  139. auto node = new Node();
  140. field[i].push_back(*node);
  141. file >> tmpPrice;
  142. field[i][j].price = tmpPrice;
  143. field[i][j].next = pair<int, int>(i, j);
  144. }
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement