Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- int row,col;
- int fx[]={1,-1,0,0};
- int fy[]={0,0,1,-1};
- int bfs(int *vis[row][col],int sx,int sy,int dx,int dy)
- {
- // cout<<sx<<" "<<sy<<endl;
- // cout<<dx<<" "<<dy<<endl;
- queue<pair<int,int> >q;
- int desti[row+5][col+5],i;
- memset(desti,0,sizeof(desti));
- pair<int,int>a,b,c;
- q.push(make_pair(sx,sy));
- vis[sx][sy]=1;
- desti[sx][sy]=0;
- int newx,newy;
- while(!q.empty())
- {
- a=q.front();
- q.pop();
- for(i=0;i<4;i++)
- {
- newx=a.first+fx[i];
- newy=a.second+fy[i];
- if(vis[newx][newy]==0 && newx>=0 && newx<row && newy>=0 && newy<col)
- {
- vis[newx][newy]=1;
- q.push(make_pair(newx,newy));
- desti[newx][newy]=desti[a.first][a.second]+1;
- if(newx==dx && newy==dy)
- return desti[newx][newy];
- }
- }
- }
- return desti[dx][dy];
- }
- int main()
- {
- int t,cas,i,j,k,n,m;
- cin>>t;
- for(cas=1;cas<=t;cas++)
- {
- cin>>row>>col;
- char s[30][30];
- int vis[row+5][col+5];
- memset(vis,0,sizeof(vis));
- int sax,say,sbx,sby,scx,scy,hx,hy;
- for(i=0;i<row;i++)
- {
- for(j=0;j<col;j++)
- {
- cin>>s[i][j];
- if(s[i][j]=='a')
- {
- sax=i;
- say=j;
- // vis[i][j]=true;
- }
- else if(s[i][j]=='b')
- {
- sbx=i;
- sby=j;
- // vis[i][j]=true;
- }
- else if(s[i][j]=='c')
- {
- scx=i;
- scy=j;
- // vis[i][j]=true;
- }
- else if(s[i][j]=='h')
- {
- hx=i;
- hy=j;
- }
- else
- {
- vis[i][j]=1;
- }
- }
- }
- int ans1=bfs(vis,sax,say,hx,hy);
- int ans2=bfs(vis,sbx,sby,hx,hy);
- int ans3=bfs(vis,scx,scy,hx,hy);
- int ans=max(ans1,max(ans2,ans3));
- cout<<"Case "<<cas<<": "<<ans<<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement