sacgajcvs

Untitled

Mar 20th, 2022
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. string ltrim(const string &);
  6. string rtrim(const string &);
  7. vector<string> split(const string &);
  8.  
  9.  
  10. /*
  11. * Complete the 'minimumDistance' function below.
  12. *
  13. * The function is expected to return an INTEGER.
  14. * The function accepts 2D_INTEGER_ARRAY area as parameter.
  15. */
  16.  
  17. int minimumDistance(vector<vector<int>> area) {
  18. int n = area.size();
  19. int m = area[0].size();
  20. vector<vector<int>> mat(n, vector<int>(m, -1));
  21. queue<vector<int>> que;
  22. vector<int> X = {0, 0, -1, 1};
  23. vector<int> Y = {1, -1, 0, 0};
  24. auto check = [&] (int x, int y) {
  25. return (x >= 0 && x < n && y >= 0 && y < m && area[x][y] != 0);
  26. };
  27. mat[0][0] = 0;
  28. que.push({0, 0});
  29. while(!que.empty()) {
  30. vector<int> cur = que.front();
  31. que.pop();
  32. int x = cur[0], y = cur[1];
  33. if(area[x][y] == 9) {
  34. return mat[x][y];
  35. }
  36. for(int i = 0; i < 4; i++) {
  37. int p = x + X[i], q = y + Y[i];
  38. if(check(p, q) && mat[p][q] == -1) {
  39. mat[p][q] = mat[x][y] + 1;
  40. que.push({p, q});
  41. }
  42. }
  43. }
  44. return -1;
  45. }
  46. int main()
  47. {
  48. ofstream fout(getenv("OUTPUT_PATH"));
  49.  
  50. string area_rows_temp;
  51. getline(cin, area_rows_temp);
  52.  
  53. int area_rows = stoi(ltrim(rtrim(area_rows_temp)));
  54.  
  55. string area_columns_temp;
  56. getline(cin, area_columns_temp);
  57.  
  58. int area_columns = stoi(ltrim(rtrim(area_columns_temp)));
  59.  
  60. vector<vector<int>> area(area_rows);
  61.  
  62. for (int i = 0; i < area_rows; i++) {
  63. area[i].resize(area_columns);
  64.  
  65. string area_row_temp_temp;
  66. getline(cin, area_row_temp_temp);
  67.  
  68. vector<string> area_row_temp = split(rtrim(area_row_temp_temp));
  69.  
  70. for (int j = 0; j < area_columns; j++) {
  71. int area_row_item = stoi(area_row_temp[j]);
  72.  
  73. area[i][j] = area_row_item;
  74. }
  75. }
  76.  
  77. int result = minimumDistance(area);
  78.  
  79. fout << result << "\n";
  80.  
  81. fout.close();
  82.  
  83. return 0;
  84. }
  85.  
  86. string ltrim(const string &str) {
  87. string s(str);
  88.  
  89. s.erase(
  90. s.begin(),
  91. find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
  92. );
  93.  
  94. return s;
  95. }
  96.  
  97. string rtrim(const string &str) {
  98. string s(str);
  99.  
  100. s.erase(
  101. find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
  102. s.end()
  103. );
  104.  
  105. return s;
  106. }
  107.  
  108. vector<string> split(const string &str) {
  109. vector<string> tokens;
  110.  
  111. string::size_type start = 0;
  112. string::size_type end = 0;
  113.  
  114. while ((end = str.find(" ", start)) != string::npos) {
  115. tokens.push_back(str.substr(start, end - start));
  116.  
  117. start = end + 1;
  118. }
  119.  
  120. tokens.push_back(str.substr(start));
  121.  
  122. return tokens;
  123. }
  124.  
Advertisement
Add Comment
Please, Sign In to add comment