Saleh127

Light OJ 1175 / BFS

Mar 28th, 2022
917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.90 KB | None | 0 0
  1. /***
  2.  created: 2022-03-28-15.35.18
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. vector<pair<ll,ll>>ff;
  13. char a[300][300];
  14. ll r,c,x,y;
  15. ll dx[]= {1,-1,0,0};
  16. ll dy[]= {0,0,1,-1};
  17. ll disf[205][205];
  18. ll disj[205][205];
  19. bool v[205][205];
  20.  
  21. bool isvalid(pair<ll,ll>u)
  22. {
  23.     if(u.first<0 || u.first>=r || u.second<0 || u.second>=c || v[u.first][u.second] || a[u.first][u.second]!='.') return 0;
  24.  
  25.     return 1;
  26. }
  27.  
  28.  
  29. void bfs1()
  30. {
  31.     queue<pair<ll,ll>>q;
  32.  
  33.     for(ll i=0; i<r+5; i++)
  34.     {
  35.         for(ll j=0; j<c+5; j++)
  36.         {
  37.             v[i][j]=0;
  38.             disf[i][j]=INT_MAX;
  39.         }
  40.     }
  41.  
  42.     for(auto d:ff)
  43.     {
  44.         disf[d.first][d.second]=0;
  45.         v[d.first][d.second]=1;
  46.         q.push(d);
  47.     }
  48.  
  49.     pair<ll,ll>u,s;
  50.  
  51.     while(!q.empty())
  52.     {
  53.         u=q.front();
  54.         q.pop();
  55.  
  56.         for(ll i=0; i<4; i++)
  57.         {
  58.             s.first=u.first+dx[i];
  59.             s.second=u.second+dy[i];
  60.  
  61.             if(isvalid(s))
  62.             {
  63.                 v[s.first][s.second]=1;
  64.                 disf[s.first][s.second]=disf[u.first][u.second]+1;
  65.                 q.push(s);
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71.  
  72. ll bfs2(ll x,ll y)
  73. {
  74.     queue<pair<ll,ll>>q;
  75.  
  76.     for(ll i=0; i<r+5; i++)
  77.     {
  78.         for(ll j=0; j<c+5; j++)
  79.         {
  80.             v[i][j]=0;
  81.             disj[i][j]=INT_MAX;
  82.         }
  83.     }
  84.  
  85.     disj[x][y]=0;
  86.     v[x][y]=1;
  87.     q.push({x,y});
  88.  
  89.     pair<ll,ll>u,s;
  90.  
  91.     while(!q.empty())
  92.     {
  93.         u=q.front();
  94.         q.pop();
  95.  
  96.         if(u.first==0 || u.first==r-1 || u.second==0 || u.second==c-1)
  97.         {
  98.             return disj[u.first][u.second];
  99.         }
  100.  
  101.         for(ll i=0; i<4; i++)
  102.         {
  103.             s.first=u.first+dx[i];
  104.             s.second=u.second+dy[i];
  105.  
  106.             if(isvalid(s) && disf[s.first][s.second]>disj[u.first][u.second]+1)
  107.             {
  108.                 v[s.first][s.second]=1;
  109.                 disj[s.first][s.second]=disj[u.first][u.second]+1;
  110.                 q.push(s);
  111.             }
  112.         }
  113.     }
  114.  
  115.     return -1;
  116. }
  117.  
  118. int main()
  119. {
  120.     ios_base::sync_with_stdio(0);
  121.     cin.tie(0);
  122.     cout.tie(0);
  123.  
  124.  
  125.  
  126.     test
  127.     {
  128.         cin>>r>>c;
  129.  
  130.         for(ll i=0; i<r; i++)
  131.         {
  132.             for(ll j=0; j<c; j++)
  133.             {
  134.                 cin>>a[i][j];
  135.                 if(a[i][j]=='J')
  136.                 {
  137.                     x=i;
  138.                     y=j;
  139.                 }
  140.                 else if(a[i][j]=='F')
  141.                 {
  142.                     ff.push_back({i,j});
  143.                 }
  144.             }
  145.         }
  146.  
  147.         bfs1();
  148.  
  149.         ll l=bfs2(x,y);
  150.  
  151.         cout<<"Case "<<cs<<": ";
  152.  
  153.         if(l==-1) cout<<"IMPOSSIBLE"<<nl;
  154.         else cout<<l+1<<nl;
  155.  
  156.         ff.clear();
  157.  
  158.     }
  159.  
  160.     get_lost_idiot;
  161. }
  162.  
  163.  
Advertisement
Add Comment
Please, Sign In to add comment