Trilogy(CodeNation) Online Assessement

Question -> you are given a odd integer N which represent the size of the grid and there is a factory which produce toxic gas at middle of the grid which is (n/2,n/2) and there is another interger B which tells us the impact of the toxic gas like what it's spread capacity if B=1 then it covers all adjecent elements (diagonally also). and if B=2 then it covers all cells which are at distance 2 from the middle(diagonally also). its nothing but just form a square of size (2B+1) inside the grid and we can not go through this toxic cells. We have to return the numbers of unique ways from (0,0) to (N-1,N-1)

Constraint -> 1<N<1e5
0<B<N/2-1

sample Case 1 -> N = 3
B = 0
output -> 2
sample Case 2-> N = 7
B = 1
output ->74

Sample Case 3-> N=9
B=2
output -> 130

I try this Brute force during OA
#include <bits/stdc++.h>
using namespace std;
#define int long long
vector i_moves = {-1,-1,-1,0,1,1,1,0};
vector j_moves = {-1,0,1,1,1,0,-1,-1};
void solve()
{
int n,k;
cin>>n>>k;
vector <vector> grid(n,vector(n,1));
queue <pair<int,int>> q;
q.push({n/2,n/2});
grid[n/2][n/2] = 0;
while(k>0&&!q.empty())
{
int size = q.size();
while(size--)
{
int curri = q.front().first;
int currj = q.front().second;
q.pop();
for(int i=0;i<8;i++)
{
int newi = curri + i_moves[i];
int newj = currj + j_moves[i];
if(newi<0||newj<0||newi>=n||newj>=n) continue;
if(grid[newi][newj]==0) continue;
grid[newi][newj] = 0;
q.push({newi,newj});
}
}
k-=1;
}
vector <vector> dp(n,vector(n,0));
for(int i=0;i<n;i++)
{
if(grid[0][i]==0) break;
dp[0][i] = 1;
}
for(int i=1;i<n;i++)
{
if(grid[i][0]==1) dp[i][0] = 1;
for(int j=1;j<n;j++)
{
if(grid[i][j]==0) continue;
dp[i][j] = dp[i-1][j] + dp[i][j-1];
}

}
cout<<dp[n-1][n-1]<<endl;

}
int32_t main() {
// Your code goes hxcx ere;
int t;
t = 1;
while(t--)
{
solve();
}

return 0;

}

Comments (3)