Advertisement
Guest User

Untitled

a guest
May 21st, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <queue>
  5. #include <set>
  6. #include <map>
  7.  
  8. #define ll long long int
  9. using namespace std;
  10. int n, m, k;
  11. const int inf = 10005;
  12. pair<int, int> nul = {-1, -1};
  13.  
  14. void bfs(pair<int,int> x,  vector<pair<int,int>>& g, map<pair<int,int>,int>& path) {
  15.     queue<pair<int,int>> q;
  16.     map<pair<int, int>,int> used;
  17.     for(int i = 0; i < n * m; i++) {
  18.         used[g[i]];
  19.         used[g[i]] = false;
  20.     }
  21.     map<pair<int,int>, pair<int,int>> parent;
  22.     for(int i = 0; i < n * m; i++) {
  23.         parent[g[i]];
  24.         parent[g[i]] = nul;
  25.     }
  26.  
  27.     q.push(x);
  28.     used[x] = true;
  29.     path[x] = 0;
  30.     while(!q.empty()) {
  31.         pair<int,int> v = q.front(); q.pop();
  32.         int dx[4] = {-1, 0, 1, 0};
  33.         int dy[4] = {0, 1, 0, -1};
  34.         for(int i = 0; i < 4; i++) {
  35.             pair<int,int> s = {v.first + dx[i], v.second + dy[i]};
  36.             if(path.find(s) != path.end()) {
  37.                 if(!used[s] && path[s] > path[v] + 1) {
  38.                     path[s] = path[v] + 1;
  39.                     q.push(s);
  40.                     used[s] = true;
  41.                     parent[s] = v;
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. int main() {
  49.     cin >> n >> m >> k;
  50.     vector<pair<int, int>> init;
  51.     for (int i = 0; i < k; i++) {
  52.         int x, y;
  53.         cin >> x >> y;
  54.         init.push_back({x, y});
  55.     }
  56.  
  57.     vector<pair<int, int>> g;
  58.     for(int i = 0; i < n; i++) {
  59.         for(int j = 0; j < m; j++) {
  60.             g.push_back({i, j});
  61.         }
  62.     }
  63.  
  64.     map<pair<int,int>,int> path;
  65.     for(int i = 0; i < n * m; i++) {
  66.         path[g[i]];
  67.         path[g[i]] = inf;
  68.     }
  69.  
  70.     for(int i = 0; i < k; i++) {
  71.         bfs(init[i], g, path);
  72.     }
  73.  
  74.     int ans = 0;
  75.     for(int i = 0; i < n * m; i++) {
  76.         if(path[g[i]] > ans) ans = path[g[i]];
  77.     }
  78.  
  79.     cout << ans;
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement