lelouche29

Optimized_Endoscope_Samsung

Sep 24th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class que{
  5.     int *row;
  6.     int *col;
  7.     int *len;
  8.     int front,rear;
  9.     public:
  10.         que();
  11.         que(int r,int c){
  12.             row= new int[r*c];
  13.             col= new int[r*c];
  14.             len= new int[r*c];
  15.             front=rear=0;
  16.         }
  17.  
  18.         void push(int x,int y, int z){
  19.             row[rear]=x;
  20.             col[rear]=y;
  21.             len[rear]=z;
  22.             rear++;
  23.         }
  24.  
  25.         void pop(){
  26.             front++;
  27.         }
  28.  
  29.         void top(int &x, int &y, int &z){
  30.             x=row[front];
  31.             y=col[front];
  32.             z=len[front];
  33.         }
  34.  
  35.         bool empty(){
  36.             return front==rear;
  37.         }
  38. };
  39.  
  40. int r,c,x,y,len;
  41. int a[1000][1000];
  42. bool visited[1000][1000];
  43. int ans;
  44.  
  45. bool valid(int x, int y){
  46.     if(x>=0 && x<r && y>=0 && y<c && visited[x][y]==false)
  47.         return true;
  48.     return false;
  49. }
  50.  
  51. void init(){
  52.     for(int i=0; i<1000; i++)
  53.         for(int j=0; j<c; j++)
  54.             visited[i][j]=false;
  55. }
  56.  
  57. void bfs(){
  58.     if(a[x][y]==0) return;
  59.     ans=1;
  60.  
  61.     que q(r,c);
  62.     q.push(x,y,1);
  63.     visited[x][y]=true;
  64.  
  65.     while(!q.empty()){
  66.         int x,y,l;
  67.         q.top(x,y,l);
  68.         q.pop();
  69.  
  70.         // up 1 2 4 7 down 1 2 5 6  left 1 3 6 7 right 1 3 4 5
  71.  
  72.         //for going up
  73.         if(valid(x-1,y) && l+1<=len){
  74.             if(a[x-1][y]==1 || a[x-1][y]==2 || a[x-1][y]==5 || a[x-1][y]==6){
  75.                 if(a[x][y]==1 || a[x][y]==2 || a[x][y]==4 || a[x][y]==7){
  76.                     visited[x-1][y]=true;
  77.                     q.push(x-1,y,l+1);
  78.                     ans++;
  79.                 }
  80.             }
  81.         }
  82.  
  83.         //for down
  84.         if(valid(x+1,y) && l+1<=len){
  85.             if(a[x+1][y]==1 || a[x+1][y]==2 || a[x+1][y]==4 || a[x+1][y]==7){
  86.                 if(a[x][y]==1 || a[x][y]==2 || a[x][y]==5 || a[x][y]==6){
  87.                     visited[x+1][y]=true;
  88.                     q.push(x+1,y,l+1);
  89.                     ans++;
  90.                 }
  91.             }
  92.         }
  93.  
  94.         //for left
  95.         if(valid(x,y-1) && l+1<=len){
  96.             if(a[x][y-1]==1 || a[x][y-1]==3 || a[x][y-1]==4 || a[x][y-1]==5){
  97.                 if(a[x][y]==1 || a[x][y]==3 || a[x][y]==6 || a[x][y]==7){
  98.                     visited[x][y-1]=true;
  99.                     q.push(x,y-1,l+1);
  100.                     ans++;
  101.                 }
  102.             }
  103.         }
  104.  
  105.         //for right
  106.         if(valid(x,y+1) && l+1<=len){
  107.             if(a[x][y+1]==1 || a[x][y+1]==3 || a[x][y+1]==6 || a[x][y+1]==7){
  108.                 if(a[x][y]==1 || a[x][y]==3 || a[x][y]==4 || a[x][y]==5){
  109.                     visited[x][y+1]=true;
  110.                     q.push(x,y+1,l+1);
  111.                     ans++;
  112.                 }
  113.             }
  114.         }
  115.        
  116.     }
  117.  
  118. }
  119.  
  120. int main() {
  121.     int t;
  122.     cin>>t;
  123.     while(t--){
  124.         cin>>r>>c>>x>>y>>len;
  125.  
  126.         for(int i=0; i<r; i++)
  127.             for(int j=0; j<c; j++)
  128.                 cin>>a[i][j];
  129.  
  130.         init();
  131.  
  132.         ans=-1;
  133.         bfs();
  134.         cout<<ans<<endl;
  135.     }
  136.     return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment