Facebook
From Sagar, 3 Months ago, written in C++.
Embed
Download Paste or View Raw
Hits: 195
  1. #include <cmath>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. int a[50][50],n,m;
  9. int vis[50][50]={0};
  10.  
  11. void DFS(int xpos,int ypos,int rem_len){
  12.     if(xpos<0 || xpos>=n || ypos<0 || ypos>=m || rem_len==0)
  13.         return;
  14.     vis[xpos][ypos]=1;
  15.     if((xpos!=0) && (a[xpos-1][ypos] ==2 || a[xpos-1][ypos] ==5 || a[xpos-1][ypos] ==6 || a[xpos-1][ypos] ==1))  //up
  16.             DFS(xpos-1, ypos, rem_len-1);
  17.    
  18.     if((xpos!=n-1) && (a[xpos+1][ypos] ==2 || a[xpos+1][ypos] ==4 || a[xpos+1][ypos] ==7 || a[xpos+1][ypos] ==1)) //down
  19.             DFS(xpos+1, ypos, rem_len-1);
  20.  
  21.     if((ypos!=0)&& (a[xpos][ypos-1] ==3 || a[xpos][ypos-1] ==4 || a[xpos][ypos-1] ==5 || a[xpos][ypos-1] ==1))     //left
  22.             DFS(xpos, ypos-1, rem_len-1);
  23.  
  24.     if((ypos!=m-1) && (a[xpos][ypos+1] ==3 || a[xpos][ypos+1] ==6 || a[xpos][ypos+1] ==7 || a[xpos][ypos+1] ==1))   //right
  25.             DFS(xpos, ypos+1, rem_len-1);
  26.  
  27. }
  28.  
  29.  
  30. int main() {
  31.     int t,i,j,k,x,y,l;
  32.     cin>>t;
  33.     while(t--){
  34.        
  35.        cin>>n>>m>>x>>y>>l;
  36.         for(i=0;i<n;i++){
  37.             for(j=0;j<m;j++)
  38.                 cin>>a[i][j];
  39.         }
  40.        
  41.         DFS(x,y,l);
  42.         int count=0;
  43.         for(i=0;i<n;i++){
  44.             for(j=0;j<m;j++){
  45.                 if(vis[i][j]==1){
  46.                      count++;
  47.                     vis[i][j]=0;
  48.                 }
  49.                    
  50.             }
  51.         }
  52.         cout<<count<<endl;
  53.     }
  54.     return 0;
  55. }