Advertisement
Guest User

Untitled

a guest
Jan 28th, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <vector>
  5. #include <string>
  6. #include <set>
  7. #include <queue>
  8. #include <utility>
  9. #include <iomanip>
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <numeric>
  13. #include <cmath>
  14. #include <stack>
  15. #include <map>
  16. #include <deque>
  17. #include <sstream>
  18. using namespace std;
  19. template <class T>
  20. istream& operator>>(istream& stream, vector<T>& v){
  21. for(T& x : v){
  22. stream >> x;
  23. }
  24. return stream;
  25. }
  26. #define int long long
  27. typedef vector<int> vi;
  28. typedef vector<pair<int, int>> vii;
  29. typedef long long ll;
  30. typedef string str;
  31. typedef long double ld;
  32. typedef vector <vector <int>> vvi;
  33. #define pi M_PI
  34. #define all(x) (x).begin(), (x).end()
  35. #define rall(x) (x).rbegin(), (x).rend()
  36. #define pb push_back
  37. #define re return
  38. #define fr(x) for(int i = 0; i <(x); i++)
  39. const int inf = 1000000000 + 7;
  40. int n,m;
  41. char mas[10000][10000];
  42. int was[10000][10000];
  43. int ans = 0;
  44. void bfs(int x, int y, map<int, vector<pair<int,int>>> portals){
  45. was[x][y] = 1;
  46. int dx[4] = {1, -1, 0, 0};
  47. int dy[4] = {0, 0, -1, 1};
  48. queue<pair<int,int>> q;
  49. q.push({x,y});
  50. while(!q.empty()){
  51. auto p = q.front();
  52. q.pop();
  53. if(mas[p.first][p.second] == 'X'){
  54. cout << was[p.first][p.second] + 1<< endl;
  55. return;
  56. }
  57. if(isdigit(mas[p.first][p.second])){
  58. for(auto x : portals[mas[p.first][p.second]- '0']){
  59. if(!was[x.first][x.second]){
  60. q.push({x.first, x.second});
  61. was[x.first][x.second] = was[p.first][p.second] + 1;
  62. }
  63. }
  64. }
  65. for(int k = 0 ; k < 4; k++)
  66. {
  67. int a = p.first + dx[k];
  68. int b = p.second + dy[k];
  69. if(a >= 0 && a < n && b >=0 && b < m)
  70. if(!was[a][b] && mas[a][b] != '#'){
  71. q.push({a,b});
  72. was[a][b] = was[p.first][p.second] + 1;
  73. }
  74. }
  75. }
  76. }
  77. signed main() {
  78. cin >> m >> n;
  79. map<int, vector<pair<int,int>>> portals;
  80. string s;
  81. getline(cin, s);
  82. for(int i = 0; i < n; i++){
  83. getline(cin, s);
  84. for(int j = 0; j < m; j++){
  85. mas[i][j] = s[j];
  86. if(( mas[i][j] >= '0')&&( mas[i][j] <= '9'))
  87. portals[mas[i][j] - '0'].pb({i,j});
  88. }
  89. }
  90. bfs(0,0,portals);
  91.  
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement