lelouche29

laughing_bomb_Samsung

Sep 11th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class que{
  5.     int row[1000];
  6.     int col[1000];
  7.     int front,rear;
  8.     public:
  9.         que(){
  10.             front=rear=0;
  11.         }
  12.         void push(int x, int y){
  13.             row[rear]=x;
  14.             col[rear]=y;
  15.             rear++;
  16.         }
  17.         void pop(){
  18.             front++;
  19.         }
  20.         void top(int *x, int *y){
  21.             *x=row[front];
  22.             *y=col[front];
  23.         }
  24.         bool empty(){
  25.             return front==rear;
  26.         }
  27.         int size(){
  28.             return rear-front;
  29.         }
  30. };
  31.  
  32. int r,c;
  33. int mat[100][100];
  34.  
  35. bool canCheck(int x, int y){
  36.     if(x>=0 && x<r && y>=0 && y<c && mat[x][y]==1)
  37.         return true;
  38.     return false;
  39.  
  40. }
  41.  
  42. int bfs(int s1,int s2){
  43.     int dx[]={0,1,0,-1};
  44.     int dy[]={1,0,-1,0};
  45.    
  46.     que q;
  47.     q.push(s1,s2);
  48.     mat[s1][s2]=2;
  49.  
  50.     int time=0,flag=0;
  51.     while(!q.empty()){
  52.         int size=q.size();
  53.         while(size--){
  54.             int x,y;
  55.             q.top(&x,&y);
  56.             q.pop();
  57.  
  58.             for(int i=0; i<4; i++){
  59.                 int m = x + dx[i];
  60.                 int n = y + dy[i];
  61.  
  62.                 if(canCheck(m,n)){
  63.                     q.push(m,n);
  64.                     mat[m][n]=2;
  65.                     flag=1;
  66.                 }
  67.             }
  68.         }
  69.         time++;
  70.     }
  71.     return time;
  72. }
  73.  
  74. int main() {
  75.     int t;
  76.     cin>>t;
  77.     while(t--){
  78.         cin>>c>>r;     //row and colomn
  79.  
  80.         for(int i=0; i<r; i++)
  81.             for(int j=0; j<c; j++)
  82.                 cin>>mat[i][j];
  83.  
  84.         int x2,x1;
  85.         cin>>x2>>x1;
  86.         if(mat[x1][x2]==0) cout<<0<<endl;
  87.         else{
  88.             int ans= bfs(x1-1,x2-1);
  89.             cout<<ans<<endl;
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment