Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <queue>
- #include <set>
- #include <map>
- #define ll long long int
- using namespace std;
- int n, m, k;
- const int inf = 10005;
- pair<int, int> nul = {-1, -1};
- void bfs(pair<int,int> x, vector<pair<int,int>>& g, map<pair<int,int>,int>& path) {
- queue<pair<int,int>> q;
- map<pair<int, int>,int> used;
- for(int i = 0; i < n * m; i++) {
- used[g[i]];
- used[g[i]] = false;
- }
- map<pair<int,int>, pair<int,int>> parent;
- for(int i = 0; i < n * m; i++) {
- parent[g[i]];
- parent[g[i]] = nul;
- }
- q.push(x);
- used[x] = true;
- path[x] = 0;
- while(!q.empty()) {
- pair<int,int> v = q.front(); q.pop();
- int dx[4] = {-1, 0, 1, 0};
- int dy[4] = {0, 1, 0, -1};
- for(int i = 0; i < 4; i++) {
- pair<int,int> s = {v.first + dx[i], v.second + dy[i]};
- if(path.find(s) != path.end()) {
- if(!used[s] && path[s] > path[v] + 1) {
- path[s] = path[v] + 1;
- q.push(s);
- used[s] = true;
- parent[s] = v;
- }
- }
- }
- }
- }
- int main() {
- cin >> n >> m >> k;
- vector<pair<int, int>> init;
- for (int i = 0; i < k; i++) {
- int x, y;
- cin >> x >> y;
- init.push_back({x, y});
- }
- vector<pair<int, int>> g;
- for(int i = 0; i < n; i++) {
- for(int j = 0; j < m; j++) {
- g.push_back({i, j});
- }
- }
- map<pair<int,int>,int> path;
- for(int i = 0; i < n * m; i++) {
- path[g[i]];
- path[g[i]] = inf;
- }
- for(int i = 0; i < k; i++) {
- bfs(init[i], g, path);
- }
- int ans = 0;
- for(int i = 0; i < n * m; i++) {
- if(path[g[i]] > ans) ans = path[g[i]];
- }
- cout << ans;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement