Guest User

hyinia

a guest
Nov 9th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4. vector<vector<char> >g;
  5.  
  6. vector<vector<int> > d;
  7. vector<vector<pair<int,int> > >  t;
  8. int n,m;
  9. int res1=-1,res2=-1;
  10. int dx[] = {-1, -1, -1,  0, 1, 1,  1,  0};
  11. int dy[] = {-1,  0,  1,  1, 1, 0, -1, -1};
  12. void add(int x, int y, int dist, queue<pair<int,int> >&q){
  13. if(isalpha(g[x][y])){
  14.                 for(int h=0; h<t[g[x][y]-'A'].size(); h++){
  15.                 pair<int,int> newto=t[g[x][y]-'A'][h];
  16.  
  17.                     q.push(newto);
  18.                   //cout<<newto.first<<newto.second<<' '<<to.first<<to.second<<endl;
  19.                   //cout<<d[to.first][to.second]<<endl;
  20.  
  21.                     d[newto.first][newto.second]=dist+1;
  22.                   //  cout<<d[newto.first][newto.second]<<' ';
  23.                 }
  24.          }
  25.  
  26.     else{
  27.         d[x][y]=dist+1;
  28.         q.push({x,y});
  29.     }
  30.  
  31.  
  32.  
  33. }
  34. int bfs(int x, int y){
  35.     queue<pair<int,int> > q;
  36.     q.push({x,y});
  37.     while(!q.empty()){
  38.  
  39.         pair<int,int> to=q.front();
  40.       /*  if(to.first==2 && to.second==0){
  41.             cout<<"=="<<d[to.first][to.second]<<endl;
  42.  
  43.         }*/
  44.  
  45.        //cout<<to.first<<' '<<to.second<<endl;
  46.        //cout<<used[0][6]<<' '<<endl;
  47.  
  48.         if(g[to.first][to.second]=='!'){
  49.             res1=d[to.first][to.second];
  50.         }
  51.         if(g[to.first][to.second]=='$'){
  52.             res2=d[to.first][to.second];
  53.         }
  54.         q.pop();
  55.  
  56.  
  57.         for(int i=0; i<8; i++){
  58.             pair<int,int> cop=make_pair(to.first+dx[i], to.second+dy[i]);
  59.                 if(cop.first>=0 && cop.first<n && cop.second>=0 && cop.second<m && g[cop.first][cop.second]!='#' && d[cop.first][cop.second]==0){
  60.                     add(cop.first,cop.second,d[cop.first][cop.second],q);
  61.  
  62.             /*if(cop.first==2 && cop.second==0){
  63.             cout<<"=="<<d[cop.first][cop.second]<<endl;*/
  64.  
  65.  
  66.             }
  67.         }
  68.  
  69.         }
  70.     if(res1==-1 || res2==-1) return -1;
  71.     return(max(res1,res2));
  72. }
  73. int main(){
  74.     ios_base::sync_with_stdio(false);
  75.     cin.tie(0);
  76.     cin>>n>>m;
  77.      t.resize(30,vector<pair<int,int> >());
  78.     g.resize(n,vector<char>(m));
  79.     d.resize(n,vector<int> (m));
  80.  
  81.     char a;
  82.     pair<int,int> kate,jelo,krep;
  83.     string str;
  84.     for(int i=0; i<n; i++){
  85.         cin>>str;
  86.         for(int j=0; j<str.size(); j++){
  87.             g[i][j]=str[j];
  88.             if(str[j]=='$') kate={i,j};
  89.             else if(str[j]=='!') jelo={i,j};
  90.             else if(str[j]=='*'){ krep={i,j};
  91.             g[i][j]='#';
  92.             }
  93.             else if(isalpha(str[j]))
  94.                 t[g[i][j]-'A'].emplace_back(i,j);
  95.         }
  96.     }
  97.  
  98.  
  99.         vector<int>ans;
  100.     for(int i=0; i<8; i++){
  101.         pair<int,int> v=make_pair(krep.first+dx[i],krep.second+dy[i]);
  102.         if(v.first>=0 && v.first<n && v.second>=0 && v.second<m && g[v.first][v.second]!='#'){
  103.             ans.pb(bfs(v.first,v.second));
  104.              d.assign(n,vector<int>(m,0));
  105.         }
  106.     }
  107.     int mn=ans[0];
  108.     for(int i=1; i<ans.size(); i++){
  109.         if(mn>ans[i])
  110.             mn=ans[i];
  111.     }
  112.     if(mn==-1 )
  113.         cout<<"Impossible";
  114.     else cout<<++mn;
  115.  
  116.  
  117.  
  118.     return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment