Advertisement
Ahmed_Negm

Untitled

Jun 28th, 2024
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define ll long long
  5. #define nl "\n"
  6.  
  7. void files(){
  8.     ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
  9.     #ifndef ONLINE_JUDGE
  10.         freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  11.     #endif
  12. }
  13.  
  14.  
  15. void solve(){
  16.     int n,m; cin>>n>>m;
  17.  
  18.     vector<vector<int>> grid(n, vector<int>(m));
  19.     for(int i=0;i<n;i++){
  20.         for(int j=0;j<m;j++){
  21.             char c; cin>>c;
  22.             if(c=='#') grid[i][j] = 0;
  23.             else if(c=='P') grid[i][j] = -1;
  24.             else if(c=='.') grid[i][j] = 1;
  25.             else grid[i][j] = c-'0';
  26.         }
  27.     }
  28.  
  29.     queue<pair<int,int>> q;
  30.     // map<pair<int,int>,bool> vis;
  31.     // set<pair<int,int>> vis;
  32.     bool vis[n][m] = {false};
  33.  
  34.     for(int i=0;i<n;i++){
  35.         for(int j=0;j<m;j++){
  36.             if(grid[i][j]==-1){
  37.                 q.push({i,j});
  38.                 vis[i][j] = true;
  39.             }
  40.         }
  41.     }
  42.  
  43.     while(!q.empty()){
  44.         int x = q.front().first, y = q.front().second;
  45.         q.pop();
  46.  
  47.         int nx = x,ny = y;
  48.         if(grid[x][y] == -1) ny = y+1;
  49.         else ny = y+grid[x][y];
  50.         if(ny<m){
  51.             if(grid[nx][ny]>0 && !vis[nx][ny]){
  52.                 vis[nx][ny] = true;
  53.                 q.push({nx,ny});
  54.             }
  55.             nx = x+(grid[x][y]==-1?1:grid[x][y]);
  56.             if(nx<n and grid[nx][ny]>0 and !vis[nx][ny]){
  57.                 vis[nx][ny] = true;
  58.                 q.push({nx,ny});
  59.             }
  60.             nx = x-(grid[x][y]==-1?1:grid[x][y]);
  61.             if(nx>=0 and grid[nx][ny]>0 and !vis[nx][ny]){
  62.                 vis[nx][ny] = true;
  63.                 q.push({nx,ny});
  64.             }
  65.         }
  66.     }
  67.  
  68.     for(int i=0;i<n;i++){
  69.         for(int j=0;j<m;j++){
  70.             if(vis[i][j]) grid[i][j] = -1;
  71.             vis[i][j] = false;
  72.         }
  73.     }
  74.  
  75.     for(int i=0;i<n;i++){
  76.         if(grid[i][0]>0){
  77.             q.push({i,0});
  78.             vis[i][0] = true;
  79.         }
  80.     }
  81.  
  82.     int ans = 0;
  83.     while(!q.empty()){
  84.         int sz = q.size();
  85.         while(sz--){
  86.             int x = q.front().first, y = q.front().second;
  87.             q.pop();
  88.  
  89.             if(y==m-1){
  90.                 cout<<ans<<nl;
  91.                 return;
  92.             }
  93.             int nx = x,ny = y+grid[x][y];
  94.             if(ny<m){
  95.                 if(grid[nx][ny]>0 and !vis[nx][ny]){
  96.                     vis[nx][ny] = true;
  97.                     q.push({nx,ny});
  98.                 }
  99.                 nx = x+grid[x][y];
  100.                 if(nx<n and grid[nx][ny]>0 and !vis[nx][ny]){
  101.                     vis[nx][ny] = true;
  102.                     q.push({nx,ny});
  103.                 }
  104.                 nx = x-grid[x][y];
  105.                 if(nx>=0 and grid[nx][ny]>0 and !vis[nx][ny]){
  106.                     vis[nx][ny] = true;
  107.                     q.push({nx,ny});
  108.                 }
  109.             }
  110.         }
  111.         ans++;
  112.     }
  113.  
  114.     cout<<-1<<nl;
  115.  
  116.  
  117. }
  118.  
  119. int main(){
  120.     files();
  121.     int t = 1;
  122.     // cin>>t;
  123.     while(t--) solve();
  124.  
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement