ErnestGalbrun

Untitled

Dec 15th, 2021 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <compare>
  5. #include <absl/time/time.h>
  6. #include <absl/time/clock.h>
  7. #include <sstream>
  8. #include <map>
  9. #include <regex>
  10. #include <absl/algorithm/container.h>
  11. #include <absl/strings/str_split.h>
  12. #include <absl/strings/str_cat.h>
  13.  
  14. using namespace std;
  15.  
  16. struct Point {
  17.     int x;
  18.     int y;
  19.  
  20.     auto operator<=>(const Point &) const = default;
  21.  
  22.     auto operator+(const Point &b) const {
  23.         return Point{.x = x + b.x, .y = y + b.y};
  24.     }
  25.  
  26.     void operator+=(const Point &b) {
  27.         x += b.x;
  28.         y += b.y;
  29.     }
  30.  
  31.     struct Hash {
  32.         size_t operator()(const Point &p) const {
  33.             size_t rowHash = std::hash<int>()(p.x);
  34.             size_t colHash = std::hash<int>()(p.y) << 1;
  35.             return rowHash ^ colHash;
  36.         }
  37.     };
  38. };
  39.  
  40. vector<Point> neighbors(const Point &p, int R, int C) {
  41.     vector<Point> ans;
  42.     for (const Point &d: vector<Point>{{.x=-1, .y=0},
  43.                                        {.x=1, .y=0},
  44.                                        {.x=0, .y=-1},
  45.                                        {.x=0, .y=1}}) {
  46.         Point n = p + d;
  47.         if (n.x >= 0 && n.x < R && n.y >= 0 && n.y < C) {
  48.             ans.push_back(n);
  49.         }
  50.     }
  51.     return ans;
  52. }
  53.  
  54. int Get(const vector<vector<int>> &A, int i, int j, int R, int C) {
  55.     int ans = (A[i % R][j % C] + (i / R) + (j / C));
  56.     while (ans > 9) ans -= 9;
  57.     return ans;
  58. }
  59.  
  60.  
  61. int main() {
  62.     auto start = absl::Now();
  63.     vector<vector<int>> A;
  64.     for (std::string line; std::getline(std::cin, line);) {
  65.         A.push_back(vector<int>());
  66.         for (auto c: line) {
  67.             A.back().push_back(c - '0');
  68.         }
  69.     }
  70.  
  71.     int R = A.size();
  72.     int C = A[0].size();
  73.  
  74.     int RR = R * 5;
  75.     int CC = C * 5;
  76.  
  77.     vector<vector<int>> AA(RR, vector<int>(CC));
  78.  
  79.     int dist[RR][CC];
  80.     std::priority_queue<pair<int, Point>, vector<pair<int, Point>>, std::greater<pair<int, Point>>> to_visit;
  81.     bool visited[RR][CC];
  82.  
  83.     for (int i = 0; i < RR; ++i) {
  84.         for (int j = 0; j < CC; ++j) {
  85.             AA[i][j] = Get(A, i, j, R, C);
  86.             dist[i][j] = 1e9;
  87.             visited[i][j] = false;
  88.         }
  89.     }
  90.  
  91.     dist[0][0] = 0;
  92.     to_visit.push({0,{.x = 0, .y = 0}});
  93.     while (!to_visit.empty()) {
  94.         auto p = to_visit.top().second;
  95.         to_visit.pop();
  96.         if (visited[p.x][p.y]) continue;
  97.         visited[p.x][p.y] = true;
  98.         auto N = neighbors(p, RR, CC);
  99.         for (auto n: N) {
  100.             dist[n.x][n.y] = min(dist[n.x][n.y], dist[p.x][p.y] + AA[n.x][n.y]);
  101.             to_visit.push({dist[n.x][n.y],n});
  102.         }
  103.     }
  104.  
  105.     cout << dist[RR - 1][CC - 1] << endl;
  106.     cout << absl::Now() - start << endl;
  107.  
  108.     return 0;
  109. }
Add Comment
Please, Sign In to add comment