Advertisement
Guest User

dodging lasers

a guest
Nov 30th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.52 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N=3000;
  5. int dp[2][N][N];
  6. bool r[2][N],c[2][N];
  7.  
  8. int main()
  9. {
  10.     ios_base::sync_with_stdio(false);
  11.     cin.tie(NULL);
  12.  
  13.     int n,m,k;
  14.     cin>>n>>m>>k;
  15.  
  16.     int i,j,mx=1e9;
  17.     for(i=0;i<n;i++)
  18.     {
  19.         r[0][i]=r[1][i]=false;
  20.         for(j=0;j<m;j++)
  21.         {
  22.             c[0][j]=c[1][j]=false;
  23.             dp[0][i][j]=dp[1][i][j]=mx;
  24.         }
  25.     }
  26.  
  27.     for(i=0;i<k;i++)
  28.     {
  29.         int u,v; char s;
  30.         cin>>u>>v>>s;
  31.         u--; v--; int ty=0;
  32.         if(s=='C') ty=1;
  33.         if(ty==0)
  34.         {
  35.             r[0][u]=true;
  36.             c[1][v]=true;
  37.         }
  38.         else
  39.         {
  40.             r[1][u]=true;
  41.             c[0][v]=true;
  42.         }
  43.     }
  44.  
  45.     dp[0][0][0]=0;
  46.     queue<int> q;
  47.     q.push(0); q.push(0); q.push(0);
  48.     vector<pair<int,int>> d;
  49.     d.push_back({0,-1}); d.push_back({-1,0}); d.push_back({1,0}); d.push_back({0,1});
  50.     while(!q.empty())
  51.     {
  52.         int pc=q.front(); q.pop();
  53.         int px=q.front(); q.pop();
  54.         int py=q.front(); q.pop();
  55.         for(auto di:d)
  56.         {
  57.             int tx=px+di.first,ty=py+di.second,cx=pc^1;
  58.             if(tx>=0 && tx<n && ty>=0 && ty<m && !c[cx][ty] && !r[cx][tx] && dp[cx][tx][ty]==mx)
  59.             {
  60.                 q.push(cx); q.push(tx); q.push(ty);
  61.                 dp[cx][tx][ty]=dp[pc][px][py]+1;
  62.             }
  63.         }
  64.     }
  65.  
  66.     int ans=min(dp[0][n-1][m-1],dp[1][n-1][m-1]);
  67.     if(ans==mx) ans=-1;
  68.     cout<<ans<<"\n";
  69.     return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement