Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <string>
- #include <set>
- #include <queue>
- #include <utility>
- #include <iomanip>
- #include <cstdio>
- #include <cstdlib>
- #include <numeric>
- #include <cmath>
- #include <stack>
- #include <map>
- #include <deque>
- #include <sstream>
- using namespace std;
- template <class T>
- istream& operator>>(istream& stream, vector<T>& v){
- for(T& x : v){
- stream >> x;
- }
- return stream;
- }
- #define int long long
- typedef vector<int> vi;
- typedef vector<pair<int, int>> vii;
- typedef long long ll;
- typedef string str;
- typedef long double ld;
- typedef vector <vector <int>> vvi;
- #define pi M_PI
- #define all(x) (x).begin(), (x).end()
- #define rall(x) (x).rbegin(), (x).rend()
- #define pb push_back
- #define re return
- #define fr(x) for(int i = 0; i <(x); i++)
- const int inf = 1000000000 + 7;
- int n,m;
- char mas[10000][10000];
- int was[10000][10000];
- int ans = 0;
- void bfs(int x, int y, map<int, vector<pair<int,int>>> portals){
- was[x][y] = 1;
- int dx[4] = {1, -1, 0, 0};
- int dy[4] = {0, 0, -1, 1};
- queue<pair<int,int>> q;
- q.push({x,y});
- while(!q.empty()){
- auto p = q.front();
- q.pop();
- if(mas[p.first][p.second] == 'X'){
- cout << was[p.first][p.second] + 1<< endl;
- return;
- }
- if(isdigit(mas[p.first][p.second])){
- for(auto x : portals[mas[p.first][p.second]- '0']){
- if(!was[x.first][x.second]){
- q.push({x.first, x.second});
- was[x.first][x.second] = was[p.first][p.second] + 1;
- }
- }
- }
- for(int k = 0 ; k < 4; k++)
- {
- int a = p.first + dx[k];
- int b = p.second + dy[k];
- if(a >= 0 && a < n && b >=0 && b < m)
- if(!was[a][b] && mas[a][b] != '#'){
- q.push({a,b});
- was[a][b] = was[p.first][p.second] + 1;
- }
- }
- }
- }
- signed main() {
- cin >> m >> n;
- map<int, vector<pair<int,int>>> portals;
- string s;
- getline(cin, s);
- for(int i = 0; i < n; i++){
- getline(cin, s);
- for(int j = 0; j < m; j++){
- mas[i][j] = s[j];
- if(( mas[i][j] >= '0')&&( mas[i][j] <= '9'))
- portals[mas[i][j] - '0'].pb({i,j});
- }
- }
- bfs(0,0,portals);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement