Advertisement
Guest User

P43164 | Treasures in a map (5)

a guest
Jan 17th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <map>
  5.  
  6. using namespace std;
  7.  
  8. const vector<int> DI = {1,0,-1,0};
  9. const vector<int> DJ = {0,1,0,-1};
  10. typedef vector<vector<char>> GRAPH;
  11. typedef pair<pair<int,int>,int> VL;
  12.  
  13. bool in_range(int i, int j, int n, int m) {
  14. return 0 <= i and 0 <= j and i < n and j < m;
  15. }
  16.  
  17. int main() {
  18. int n, m; cin >> n >> m;
  19. GRAPH g(n,vector<char>(m));
  20. for (int i = 0; i < n; ++i) {
  21. for (int j = 0; j < m; ++j) {
  22. cin >> g[i][j];
  23. }
  24. }
  25. int counter = 0;
  26. int a, b; cin >> a >> b; --a; --b;
  27. map<int,int> s;
  28. VL aux; aux.first.first = a; aux.first.second = b; aux.second = 0; bool r = in_range(a,b,n,m);
  29. queue<VL> q; q.push(aux);
  30. while (not q.empty() and r) {
  31. int ui = q.front().first.first; int uj = q.front().first.second;
  32. int d = q.front().second;
  33. q.pop();
  34. if (g[ui][uj] != 'v' and g[ui][uj] != 'X') {
  35. if (g[ui][uj] == 't') {
  36. if (s.find(d) == s.end()) {
  37. s.insert(make_pair(d,1));
  38. } else {
  39. map<int,int>::iterator it = s.find(d);
  40. it -> second = it->second+1;
  41. }
  42. ++counter;
  43. }
  44. g[ui][uj] = 'v';
  45. for (int i = 0; i < 4; ++i) {
  46. VL v;
  47. v.first.first = ui + DI[i];
  48. v.first.second = uj + DJ[i];
  49. if (in_range(v.first.first, v.first.second, n, m)) {
  50. v.second = d + 1;
  51. q.push(v);
  52. }
  53. }
  54. }
  55. }
  56.  
  57. map<int,int>::const_iterator it = s.end();
  58. if (counter < 2) cout << "we cannot reach two or more treasures" << endl;
  59. else {
  60. --it;
  61. if (it -> second >= 2) cout << "second maximum distance: " << it -> first << endl;
  62. else {
  63. --it;
  64. cout << "second maximum distance: " << it -> first << endl;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement