Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class que{
- int row[1000];
- int col[1000];
- int front,rear;
- public:
- que(){
- front=rear=0;
- }
- void push(int x, int y){
- row[rear]=x;
- col[rear]=y;
- rear++;
- }
- void pop(){
- front++;
- }
- void top(int *x, int *y){
- *x=row[front];
- *y=col[front];
- }
- bool empty(){
- return front==rear;
- }
- int size(){
- return rear-front;
- }
- };
- int r,c;
- int mat[100][100];
- bool canCheck(int x, int y){
- if(x>=0 && x<r && y>=0 && y<c && mat[x][y]==1)
- return true;
- return false;
- }
- int bfs(int s1,int s2){
- int dx[]={0,1,0,-1};
- int dy[]={1,0,-1,0};
- que q;
- q.push(s1,s2);
- mat[s1][s2]=2;
- int time=0,flag=0;
- while(!q.empty()){
- int size=q.size();
- while(size--){
- int x,y;
- q.top(&x,&y);
- q.pop();
- for(int i=0; i<4; i++){
- int m = x + dx[i];
- int n = y + dy[i];
- if(canCheck(m,n)){
- q.push(m,n);
- mat[m][n]=2;
- flag=1;
- }
- }
- }
- time++;
- }
- return time;
- }
- int main() {
- int t;
- cin>>t;
- while(t--){
- cin>>c>>r; //row and colomn
- for(int i=0; i<r; i++)
- for(int j=0; j<c; j++)
- cin>>mat[i][j];
- int x2,x1;
- cin>>x2>>x1;
- if(mat[x1][x2]==0) cout<<0<<endl;
- else{
- int ans= bfs(x1-1,x2-1);
- cout<<ans<<endl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment