Quick actions

cmd+k|ctrl+k

Navigation

Languages

CF_516_B

Snippet info

Language

Cpp

Visibility

public

Author

karthik.pasupulatei

Created

2021-06-29T15:50:59.266206Z

Updated

2021-06-29T15:52:13.916575Z

#include "bits/stdc++.h" 
#define graph vector<vector<char>>
using ll = long long ; 
using namespace std ; 

vector<pair<int,int>> moves = {{0,1},{0,-1},{1,0},{-1,0}} ;

bool valid(int i , int j , int n ,int m ){
     if (i < 1 || j < 1 || i > n || j> m)return false ; 
     return true ; 
}
int bfs(int sx , int sy , graph g ,int x ,int y ,int n , int m ){
     queue<vector<int>> q  ;
     q.push({sx ,sy , 0, 0 }) ;
     
     g[sx][sy] = '*' ; 
     int retVal = 0 ; 
     while(!q.empty()){
          vector<int> P  = q.front() ; 
          q.pop() ; 
          retVal++ ; 
          for(auto &move : moves){
               int cx  = move.first + P[0] , cy = move.second + P[1] ;
               if(valid(cx , cy , n , m) &&  g[cx][cy] !='*'){
                    vector<int> temp{cx,cy, P[2] , P[3]} ; 
                    if(abs(move.second)){
                         if(move.second > 0 )temp[3]++ ; 
                         else temp[2]++ ; 
                    }
                    if(temp[2] <=x && temp[3] <= y)
                         {
                              q.push(temp) ; 
                              g[cx][cy] = '*' ; 
                         }
               }
          }
          
     }
     return retVal ; 
}
int main (){
     
     ll n  , m ; cin >> n >> m ; 
     ll sx , sy ; cin >> sx >> sy  ; 
     ll x ,y  ; cin >> x >> y  ; 
     
     graph g(n+1 , vector(m+1 , '.')) ; 
     for(int i = 1 ; i <=n ; i++ ){
          for(int j = 1 ; j <=m ; j++ ){
               cin>> g[i][j] ; 
          }
     }
     cout << bfs(sx,sy,g,x,y,n,m) <<"\n" ; 
     return  0; 
}
INFO