Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2020
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.74 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define X ios_base::sync_with_stdio(false); cin.tie(NULL);
  4. #define FIXED_FLOAT(x) std::fixed <<std::setprecision(2)<<(x)
  5.  
  6.  
  7. void __print(int x) {cerr << x;}
  8. void __print(long x) {cerr << x;}
  9. void __print(long long x) {cerr << x;}
  10. void __print(unsigned x) {cerr << x;}
  11. void __print(unsigned long x) {cerr << x;}
  12. void __print(unsigned long long x) {cerr << x;}
  13. void __print(float x) {cerr << x;}
  14. void __print(double x) {cerr << x;}
  15. void __print(long double x) {cerr << x;}
  16. void __print(char x) {cerr << '\'' << x << '\'';}
  17. void __print(const char *x) {cerr << '\"' << x << '\"';}
  18. void __print(const string &x) {cerr << '\"' << x << '\"';}
  19. void __print(bool x) {cerr << (x ? "true" : "false");}
  20.  
  21. template<typename T, typename V>
  22. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  23. template<typename T>
  24. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  25. void _print() {cerr << "]\n";}
  26. template <typename T, typename... V>
  27. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  28. #ifndef ONLINE_JUDGE
  29. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  30. #else
  31. #define debug(x...)
  32. #endif
  33.  
  34.  
  35. // long long p = 1e9+7;
  36.  
  37. typedef long long ll;
  38. typedef pair<ll,ll> pl;
  39. typedef vector<int> VI;
  40. typedef vector<pair<ll,ll>> VP;
  41. typedef vector<ll> VL;
  42. typedef vector<VL> VVL;
  43. typedef vector<bool> VB;
  44.  
  45. // typedef pair<ll, ll> PL;
  46. typedef unordered_map<ll, ll> UMP;
  47. #define FOR(i,b,init) for(i=init;i<b;i++)
  48. #define pb push_back
  49. #define fi first
  50. #define se second
  51. #define mp make_pair
  52.  
  53. // typedef  unordered_set<ll>;
  54.  
  55.  
  56. // void printa(VI &x,ll n){
  57. //     ll i;
  58. //     FOR(i, n){
  59. //         cout<<x[i]<<" ";
  60. //     }
  61. //     cout<<endl;
  62. // }
  63.  
  64.  
  65.  
  66. /////GLOABLS VARS
  67. ll MOD = 1e9+7;
  68. ll gmx = 1e6+7;
  69.  
  70. VL fact(gmx, 1);
  71. //////FUNCTIONS
  72. ll powp(ll val, ll deg)
  73. {
  74.     // debug(val, deg);
  75.     if (!deg)
  76.         return 1;
  77.     if (deg & 1)
  78.         return (powp(val, deg - 1) * val) % MOD;
  79.     ll res = powp(val, deg >> 1);
  80.     // debug(res);
  81.     return (res * res) % MOD;
  82. }
  83. ll mx=1e5+7;
  84. vector<VL> adj;
  85. VL sub;
  86. ll n;
  87.  
  88. void dfs(ll r, ll parent){
  89.     sub[r]=1;
  90.     // debug(r,parent);
  91.     for(auto k: adj[r]){
  92.         if(k==parent){continue;}
  93.         dfs(k, r);
  94.         sub[r]+=sub[k];
  95.     }
  96.     return;
  97. }
  98.  
  99. //It is not easy but it can be fun, if you think!!!
  100.  
  101. //
  102. // ll mx = 2*1e5+7;
  103. // vector<vector<pl>> adj;
  104.  
  105.  
  106. int main(){
  107.       ios::sync_with_stdio(0);
  108.       cin.tie(0);
  109.     // #ifndef ONLINE_JUDGE
  110.     //     freopen("input.txt", "r", stdin);
  111.     //     freopen("output.txt", "w", stdout);
  112.     //     #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  113.     // #endif
  114.     ll n,i,j,m;
  115.     cin>>n>>m;
  116.     pl st,end;
  117.     cin>>st.fi>>st.se>>end.fi>>end.se;
  118.     st.fi--;st.se--;end.fi--;end.se--;
  119.     vector<string>l(n);
  120.     FOR(i,n,0){
  121.         cin>>l[i];
  122.     }
  123.     queue<pl> q, waitq;
  124.     q.push(st);
  125.     vector<VL> vis(n, vector<ll>(m,1e18));
  126.     vis[st.fi][st.se]=0;
  127.     VL dir{1,0,-1};
  128.     // VL magic
  129.     bool flag=false;
  130.     ll mag=0;
  131.     while((q.size()>0) || (waitq.size()>0)){
  132.         if(q.size()==0){
  133.             while(waitq.size()>0){
  134.                 pl el=waitq.front(); waitq.pop();
  135.                 if(vis[el.fi][el.se]>mag){
  136.                     // vis[el.fi][el.se]=vis[el.fi][el.se]-1e18;
  137.                     q.push(el);
  138.                     // mag++;
  139.  
  140.                 }
  141.             }
  142.             continue;
  143.         }
  144.         pl el=q.front();
  145.         q.pop();
  146.         mag = max(mag, vis[el.fi][el.se]);
  147.         if(el==end){
  148.             break;
  149.         }
  150.         for(auto x:dir){
  151.             for(auto y:dir){
  152.                 if(abs(x)==abs(y)){
  153.                     continue;
  154.                 }
  155.                 ll nbx = x+el.fi, nby = y+el.se;
  156.                 if((nbx>=n) || (nbx<0) || (nby>=m) || (nby<0)){
  157.                     continue;
  158.                 }
  159.                 if((vis[nbx][nby]>vis[el.fi][el.se]) && l[nbx][nby]=='.'){
  160.                     vis[nbx][nby] = vis[el.fi][el.se];
  161.                     q.push(mp(nbx,nby));
  162.                 }
  163.             }
  164.         }
  165.         for(i=max((ll)0, el.fi-2); i<min(n, el.fi+3);i++){
  166.             for(j=max((ll)0, el.se-2); j<min(m, el.se+3);j++){
  167.                 if((vis[i][j]>vis[el.fi][el.se]+1) && (l[i][j]=='.')){
  168.                     vis[i][j] = 1+vis[el.fi][el.se];
  169.                     waitq.push(mp(i,j));
  170.                 }
  171.             }  
  172.         }
  173.  
  174.     }
  175.     // debug(vis);
  176.     if(vis[end.fi][end.se]==1e18){
  177.         cout<<-1;
  178.     }
  179.     else{
  180.         cout<<vis[end.fi][end.se];
  181.     }
  182.     // cout.flush();
  183.  
  184.  
  185.  
  186.    
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement