Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #define mp make_pair
- using namespace std;
- #define ll long long int
- const int maxn=501;
- bool visited[maxn][maxn];
- int x[4]= {-1,0,0,1};
- int y[4]= {0,-1,1,0};
- int mat[501][501];
- int R,C,cnt=0;
- int mark[maxn][maxn];
- void clr()
- {
- //memset(cost, 0, sizeof(cost));
- memset(visited, 0, sizeof(visited));
- }
- int bfs(int s,int e)
- {
- clr();
- cnt=0;
- queue<int>q;
- q.push(s);
- q.push(e);
- visited[s][e]=1;
- if(mat[s][e]==1)
- cnt++;
- // cost[s][e]=0;
- while(!q.empty())
- {
- int ux,uy;
- ux=q.front();
- q.pop();
- uy=q.front();
- q.pop();
- for(int k=0; k<4; k++)
- {
- int vx=ux+x[k];
- int vy=uy+y[k];
- if((vx>=1 && vx<=R) && ((vy>=1 && vy<=C ) && visited[vx][vy]==0))
- {
- //cout<<ux<<" "<<uy<<" "<<x[k]<<" "<<y[k]<<endl;
- // cout<<" "<<vx<<" "<<vy<<endl;
- if(mat[vx][vy]==-1)
- continue;
- if(mat[vx][vy]==1)
- cnt++;
- //cost[vx][vy]=cost[ux][uy]+1;
- visited[vx][vy]=1;
- q.push(vx);
- q.push(vy);
- }
- }
- }
- return cnt;
- }
- int main()
- {
- int n, t, p,qq,m;
- scanf("%d", &t);
- for(int ch=1; ch<=t; ch++)
- {
- clr();
- memset(mark,0,sizeof(mark));
- int i,j;
- scanf("%d%d%d",&m,&n,&qq);
- R=m;
- C=n;
- for(i=1; i<=m; i++)
- {
- for(j=0; j<n; j++)
- {
- char c;
- scanf(" %c", &c);
- if(c=='.')
- {
- // cout<<c<<"ydfv "<<endl;
- mat[i][j+1]=0;
- }
- else if(c=='#')
- {
- mat[i][j+1]=-1;
- }
- else if(c=='C')
- {
- mat[i][j+1]=1;
- }
- }
- }
- printf("Case %d:\n",ch);
- // printf("Case luiwreyg ",ch);
- int sx,sy,py,mm,nm;
- for(int ii=0; ii<qq; ii++)
- {
- scanf("%d %d", &mm,&nm);
- if(mark[mm][nm])
- {
- py=mark[mm][nm];
- //kjdf;
- }
- else
- {
- clr();
- py=bfs(mm,nm);
- for(i=1; i<=m; i++)
- {
- for(j=1; j<=n; j++)
- {
- if(visited[i][j])
- mark[i][j]=py;
- }
- }
- }
- printf("%d\n", py);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment