Advertisement
Farjana_akter

Untitled

May 11th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int row,col;
  5. int fx[]={1,-1,0,0};
  6. int fy[]={0,0,1,-1};
  7.  
  8. int bfs(int *vis[row][col],int sx,int sy,int dx,int dy)
  9. {
  10. // cout<<sx<<" "<<sy<<endl;
  11. // cout<<dx<<" "<<dy<<endl;
  12. queue<pair<int,int> >q;
  13. int desti[row+5][col+5],i;
  14. memset(desti,0,sizeof(desti));
  15. pair<int,int>a,b,c;
  16. q.push(make_pair(sx,sy));
  17. vis[sx][sy]=1;
  18. desti[sx][sy]=0;
  19. int newx,newy;
  20. while(!q.empty())
  21. {
  22. a=q.front();
  23. q.pop();
  24. for(i=0;i<4;i++)
  25. {
  26. newx=a.first+fx[i];
  27. newy=a.second+fy[i];
  28. if(vis[newx][newy]==0 && newx>=0 && newx<row && newy>=0 && newy<col)
  29. {
  30. vis[newx][newy]=1;
  31. q.push(make_pair(newx,newy));
  32. desti[newx][newy]=desti[a.first][a.second]+1;
  33. if(newx==dx && newy==dy)
  34. return desti[newx][newy];
  35. }
  36. }
  37. }
  38. return desti[dx][dy];
  39. }
  40.  
  41.  
  42.  
  43. int main()
  44. {
  45. int t,cas,i,j,k,n,m;
  46. cin>>t;
  47. for(cas=1;cas<=t;cas++)
  48. {
  49. cin>>row>>col;
  50. char s[30][30];
  51. int vis[row+5][col+5];
  52. memset(vis,0,sizeof(vis));
  53. int sax,say,sbx,sby,scx,scy,hx,hy;
  54. for(i=0;i<row;i++)
  55. {
  56. for(j=0;j<col;j++)
  57. {
  58. cin>>s[i][j];
  59. if(s[i][j]=='a')
  60. {
  61. sax=i;
  62. say=j;
  63. // vis[i][j]=true;
  64. }
  65. else if(s[i][j]=='b')
  66. {
  67. sbx=i;
  68. sby=j;
  69. // vis[i][j]=true;
  70. }
  71. else if(s[i][j]=='c')
  72. {
  73. scx=i;
  74. scy=j;
  75. // vis[i][j]=true;
  76. }
  77. else if(s[i][j]=='h')
  78. {
  79. hx=i;
  80. hy=j;
  81. }
  82. else
  83. {
  84. vis[i][j]=1;
  85. }
  86. }
  87. }
  88. int ans1=bfs(vis,sax,say,hx,hy);
  89. int ans2=bfs(vis,sbx,sby,hx,hy);
  90. int ans3=bfs(vis,scx,scy,hx,hy);
  91. int ans=max(ans1,max(ans2,ans3));
  92. cout<<"Case "<<cas<<": "<<ans<<endl;
  93. }
  94. return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement