Advertisement
Guest User

Untitled

a guest
May 21st, 2019
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <string>
  5. #include <sstream>
  6. #include <queue>
  7. #include <ext/pb_ds/hash_policy.hpp>
  8. #include <ext/pb_ds/assoc_container.hpp>
  9. #include <ext/pb_ds/tree_policy.hpp>
  10.  
  11. using namespace std;
  12. typedef long long ll;
  13.  
  14. namespace pbds{
  15. using namespace __gnu_pbds;
  16.  
  17. template <class T, class cmp = std::less<T>>
  18. using set = tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
  19.  
  20. template <class key, class value, class cmp = std::less<key>>
  21. using map = tree<key, value, cmp, rb_tree_tag, tree_order_statistics_node_update>;
  22. }
  23.  
  24. int n, m, k;
  25. const int inf = 10005;
  26.  
  27. template <class VVP, class QP>
  28. void bfs(int n, int m, VVP& g, QP& q) {
  29. vector<vector<char>> used(n, vector<char>(m));
  30. while(!q.empty()) {
  31. pair<int,int> v = q.front(); q.pop();
  32. int x = v.first, y = v.second;
  33. if (used[x][y]) continue;
  34. used[x][y] = 1;
  35. int dx[4] = {-1, 0, 1, 0};
  36. int dy[4] = {0, 1, 0, -1};
  37. for(int i = 0; i < 4; i++) {
  38. int nx = x + dx[i], ny = y + dy[i];
  39. if (nx < 0 || nx >= n || ny < 0 || ny >= m || g[nx][ny] <= g[x][y] + 1) continue;
  40. g[nx][ny] = 1 + g[x][y];
  41. q.push({nx, ny});
  42. }
  43. }
  44. }
  45.  
  46. int main() {
  47. cin >> n >> m >> k;
  48. vector<vector<int>> g(n, vector<int>(m, inf));
  49. queue<pair<int, int>> q;
  50. for (int i = 0; i < k; i++) {
  51. int x, y;
  52. cin >> x >> y;
  53. q.push({x, y});
  54. g[x][y] = 0;
  55. }
  56. bfs(n, m, g, q);
  57. int ans = 0;
  58. for(int i = 0; i < n; i++)
  59. for (int j = 0; j < m; ++j)
  60. if(g[i][j] > ans)
  61. ans = g[i][j];
  62.  
  63. cout << ans;
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement