Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <compare>
- #include <absl/time/time.h>
- #include <absl/time/clock.h>
- #include <sstream>
- #include <map>
- #include <regex>
- #include <absl/algorithm/container.h>
- #include <absl/strings/str_split.h>
- #include <absl/strings/str_cat.h>
- using namespace std;
- struct Point {
- int x;
- int y;
- auto operator<=>(const Point &) const = default;
- auto operator+(const Point &b) const {
- return Point{.x = x + b.x, .y = y + b.y};
- }
- void operator+=(const Point &b) {
- x += b.x;
- y += b.y;
- }
- struct Hash {
- size_t operator()(const Point &p) const {
- size_t rowHash = std::hash<int>()(p.x);
- size_t colHash = std::hash<int>()(p.y) << 1;
- return rowHash ^ colHash;
- }
- };
- };
- vector<Point> neighbors(const Point &p, int R, int C) {
- vector<Point> ans;
- for (const Point &d: vector<Point>{{.x=-1, .y=0},
- {.x=1, .y=0},
- {.x=0, .y=-1},
- {.x=0, .y=1}}) {
- Point n = p + d;
- if (n.x >= 0 && n.x < R && n.y >= 0 && n.y < C) {
- ans.push_back(n);
- }
- }
- return ans;
- }
- int Get(const vector<vector<int>> &A, int i, int j, int R, int C) {
- int ans = (A[i % R][j % C] + (i / R) + (j / C));
- while (ans > 9) ans -= 9;
- return ans;
- }
- int main() {
- auto start = absl::Now();
- vector<vector<int>> A;
- for (std::string line; std::getline(std::cin, line);) {
- A.push_back(vector<int>());
- for (auto c: line) {
- A.back().push_back(c - '0');
- }
- }
- int R = A.size();
- int C = A[0].size();
- int RR = R * 5;
- int CC = C * 5;
- vector<vector<int>> AA(RR, vector<int>(CC));
- int dist[RR][CC];
- std::priority_queue<pair<int, Point>, vector<pair<int, Point>>, std::greater<pair<int, Point>>> to_visit;
- bool visited[RR][CC];
- for (int i = 0; i < RR; ++i) {
- for (int j = 0; j < CC; ++j) {
- AA[i][j] = Get(A, i, j, R, C);
- dist[i][j] = 1e9;
- visited[i][j] = false;
- }
- }
- dist[0][0] = 0;
- to_visit.push({0,{.x = 0, .y = 0}});
- while (!to_visit.empty()) {
- auto p = to_visit.top().second;
- to_visit.pop();
- if (visited[p.x][p.y]) continue;
- visited[p.x][p.y] = true;
- auto N = neighbors(p, RR, CC);
- for (auto n: N) {
- dist[n.x][n.y] = min(dist[n.x][n.y], dist[p.x][p.y] + AA[n.x][n.y]);
- to_visit.push({dist[n.x][n.y],n});
- }
- }
- cout << dist[RR - 1][CC - 1] << endl;
- cout << absl::Now() - start << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment