Advertisement
MathQ_

Untitled

Nov 26th, 2020
151
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 <map>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. class Vertex {
  9.     int x, y;
  10.  
  11. public:
  12.     Vertex();
  13.     Vertex(int _x, int _y);
  14.     pair<int, int> operator+ (Vertex& u);
  15.     friend bool operator==(const Vertex& v, const Vertex& u);
  16.     friend bool operator<(const Vertex& v, const Vertex& u);
  17.     friend istream& operator>>(istream& in, Vertex& v);
  18.     friend ostream& operator<<(ostream& in, const Vertex& v);
  19. };
  20.  
  21. Vertex::Vertex() {
  22.     x = 0;
  23.     y = 0;
  24. }
  25.  
  26. Vertex::Vertex(int _x, int _y) {
  27.     x = _x;
  28.     y = _y;
  29. }
  30.  
  31. bool operator==(const Vertex& v, const Vertex& u) {
  32.     return v.x == u.x && v.y == u.y;
  33. }
  34.  
  35. bool operator<(const Vertex& v, const Vertex& u) {
  36.     if (v.x == u.x) {
  37.         return v.y < u.y;
  38.     }
  39.     else {
  40.         return v.x < u.x;
  41.     }
  42. }
  43.  
  44. pair<int, int> Vertex::operator+ (Vertex& u) {
  45.     pair<int, int> result;
  46.     result.first = u.x - this->x;
  47.     result.second = u.y - this->y;
  48.     return result;
  49. }
  50.  
  51. istream& operator>>(istream& in, Vertex& v) {
  52.     in >> v.x >> v.y;
  53.     return in;
  54. }
  55.  
  56. ostream& operator<<(ostream& out, const Vertex& v) {
  57.     out << v.x << " " << v.y;
  58.     return out;
  59. }
  60.  
  61. int height, width;
  62. const int MAX = 50;
  63. char field[MAX][MAX];
  64. map<Vertex, vector<Vertex>> graph;
  65. map<Vertex, map<Vertex, Vertex>> shortest_way;
  66.  
  67. bool check(int x, int y) {
  68.     if ((1 <= x && x <= width) && (1 <= y && y <= height)) {
  69.         return field[x][y] == '1';
  70.     }
  71.     else {
  72.         return false;
  73.     }
  74. }
  75.  
  76. void input() {
  77.     cin >> height >> width;
  78.     for (int i = 1; i <= height; ++i) {
  79.         for (int j = 1; j <= width; ++j) {
  80.             cin >> field[i][j];
  81.         }
  82.     }
  83. }
  84.  
  85. void create_graph() {
  86.     for (int i = 1; i <= height; ++i) {
  87.         for (int j = 1; j <= width; ++j) {
  88.             if (field[i][j] == '1') {
  89.                 if (check(i + 1, j)) {
  90.                     graph[Vertex(i, j)].push_back(Vertex(i + 1, j));
  91.                 }
  92.                 if (check(i, j + 1)) {
  93.                     graph[Vertex(i, j)].push_back(Vertex(i, j + 1));
  94.                 }
  95.                 if (check(i, j - 1)) {
  96.                     graph[Vertex(i, j)].push_back(Vertex(i, j - 1));
  97.                 }
  98.                 if (check(i - 1, j)) {
  99.                     graph[Vertex(i, j)].push_back(Vertex(i - 1, j));
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
  105.  
  106. void bfs(Vertex st) {
  107.     queue<Vertex> q;
  108.     map<Vertex, bool> used;
  109.     used[st] = true;
  110.  
  111.     for (auto u : graph[st]) {
  112.         shortest_way[st][u] = u;
  113.         q.push(u);
  114.         used[u] = true;
  115.     }
  116.  
  117.     while (!q.empty()) {
  118.         Vertex v = q.front();
  119.         q.pop();
  120.  
  121.         for (auto u : graph[v]) {
  122.             if (!used[u]) {
  123.                 q.push(u);
  124.                 shortest_way[st][u] = shortest_way[st][v];
  125.                 used[u] = true;
  126.             }
  127.         }
  128.     }
  129. }
  130.  
  131. int main() {
  132.     int type;
  133.     cin >> type;
  134.     if (type == 1) {
  135.         input();
  136.         create_graph();
  137.         for (auto vertex : graph) {
  138.             bfs(vertex.first);
  139.         }
  140.     }
  141.     if (type == 2) {
  142.         Vertex v, u;
  143.         cin >> v >> u;
  144.         Vertex next = shortest_way[v][u];
  145.         pair<int, int> result = next + v;
  146.         cout << result.first << " " << result.second;
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement