lelouche29

Endoscope Samsung

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