Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define max 1000
- using namespace std;
- class que{
- private:
- int *row;
- int *col;
- int front,rear;
- public:
- que(){}
- que(int r,int c){
- row = new int[r*c];
- col = new int[r*c];
- front = 0;
- rear = 0;
- }
- void push(int x, int y){
- row[rear]=x;
- col[rear]=y;
- rear++;
- }
- void pop(){
- front++;
- }
- bool empty(){
- return front==rear;
- }
- void top(int *x,int *y){
- *x=row[front];
- *y=col[front];
- }
- int size(){
- return rear-front;
- }
- };
- struct form{
- int top[10];
- bool bottom[10];
- bool right[10];
- bool left[10];
- };
- form pipe[8];
- void fillDetails(){
- pipe[1].top[1] = pipe[1].top[2] = pipe[1].top[5] = pipe[1].top[6] = 1;
- pipe[1].bottom[1] = pipe[1].bottom[4] = pipe[1].bottom[2] = pipe[1].bottom[7] = 1;
- pipe[1].right[1] = pipe[1].right[3] = pipe[1].right[6] = pipe[1].right[7] = 1;
- pipe[1].left[1] = pipe[1].left[3] = pipe[1].left[4] = pipe[1].left[5] = 1;
- pipe[2].top[1] = pipe[2].top[2] = pipe[2].top[5] = pipe[2].top[6] = 1;
- pipe[2].bottom[1 ] = pipe[2].bottom[4] = pipe[2].bottom[2] = pipe[2].bottom[7] = 1;
- pipe[3].right[1] = pipe[3].right[3] = pipe[3].right[6] = pipe[3].right[7] = 1;
- pipe[3].left[1] = pipe[3].left[3] = pipe[3].left[4] = pipe[3].left[5] = 1;
- pipe[4].top[2] = pipe[4].top[5] = pipe[4].top[6] = pipe[4].top[1] = 1;
- pipe[4].right[1] = pipe[4].right[3] = pipe[4].right[6] = pipe[4].right[7] = 1;
- pipe[5].bottom[1] = pipe[5].bottom[2] = pipe[5].bottom[4] = pipe[5].bottom[7] = 1;
- pipe[5].right[1] = pipe[5].right[3] = pipe[5].right[7] = pipe[5].right[6] = 1;
- pipe[6].bottom[1] = pipe[6].bottom[2] = pipe[6].bottom[4] = pipe[6].bottom[7] = 1;
- pipe[6].left[1] = pipe[6].left[3] = pipe[6].left[5] = pipe[6].left[4] = 1;
- pipe[7].top[1] = pipe[7].top[2] = pipe[7].top[5] = pipe[7].top[6] = 1;
- pipe[7].left[1] = pipe[7].left[3] = pipe[7].left[5] = pipe[7].left[4] = 1;
- }
- bool check(int r, int c, int x,int y,bool visited[][max],int mat[][max]){
- if(x>=0 && x<r && y>=0 && y<c && visited[x][y]==false && mat[x][y]!=0)
- return true;
- return false;
- }
- bool canMove(int pos, int x, int y,int m, int n, int mat[][max],int distance, int len){
- int cur=mat[x][y];
- int k=mat[m][n];
- 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)
- return true;
- return false;
- }
- int bfs(int mat[][max], int r, int c, int len, int s1, int s2){
- bool visited[max][max];
- for(int i=0; i<r; i++)
- for(int j=0; j<c; j++)
- visited[i][j]=false;
- if(mat[s1][s2]==0) return 0;
- que *q= new que(r,c);
- q->push(s1,s2);
- visited[s1][s2]=true;
- int distance=1;
- int count=1;
- while(!q->empty()){
- int size= q->size();
- while(size--){
- int x,y;
- q->top(&x,&y);
- q->pop();
- int dx[]={-1,0,1,0};
- int dy[]={0,1,0,-1};
- for(int i=0; i<4; i++){
- int m = x + dx[i];
- int n = y + dy[i];
- if(check(r,c,m,n,visited,mat)){
- if(canMove(i,x,y,m,n,mat,distance,len)){
- q->push(m,n);
- visited[m][n]=true;
- count=count+1;
- }
- }
- }
- }
- distance++;
- }
- return count;
- }
- int main(){
- int t;
- cin>>t;
- fillDetails();
- while(t--){
- int r,c,x,y,len;
- cin>>r>>c>>x>>y>>len;
- int mat[max][max];
- for(int i=0; i<r; i++)
- for(int j=0; j<c; j++)
- cin>>mat[i][j];
- cout<<bfs(mat,r,c,len,x,y)<<endl;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment