fork(3) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. int row,col;
  4. char arr[1005][1005];
  5. bool visited[1005][1005]={false};
  6. const int X[]={0,-1,0,1};
  7. const int Y[]={-1,0,1,0};
  8. int maxi=-1,maxr,maxc;
  9. bool isinlabyrinth(int i,int j)
  10. {
  11. return ((i>=0 && i<row) && (j>=0 && j<col) && arr[i][j]=='.');
  12. }
  13. int dfs(int i,int j,int tempcount)
  14. {
  15. visited[i][j]=true;
  16. if(tempcount>maxi)
  17. {
  18. maxi=tempcount;
  19. maxr=i;
  20. maxc=j;
  21. }
  22. for(int t=0;t<4;t++)
  23. {
  24. if(isinlabyrinth(i+X[t],j+Y[t]) && !visited[i+X[t]][j+Y[t]])
  25. dfs(i+X[t],j+Y[t],tempcount+1);
  26. }
  27. }
  28. int main()
  29. {
  30. int testcases;
  31. cin >> testcases;
  32. while(testcases-->0)
  33. {
  34. memset(visited,0,sizeof(visited));
  35. cin >> col >> row;
  36. for(int i=0;i<row;i++)
  37. {
  38. for(int j=0;j<col;j++)
  39. cin >> arr[i][j];
  40. }
  41. for(int i=0;i<row;i++)
  42. {
  43. for(int j=0;j<col;j++)
  44. {
  45. if(isinlabyrinth(i,j))
  46. {
  47. dfs(i,j,0);
  48. break;
  49. }
  50. }
  51. }
  52. memset(visited,0,sizeof(visited));
  53. // cout << maxi <<" " << p.first << " " << p.second << endl;
  54. maxi=-1;
  55. dfs(maxr,maxc,0);
  56. cout << "Maximum rope length is " << maxi << "." << endl;
  57. }
  58. }
  59.  
Success #stdin #stdout 0s 5444KB
stdin
2
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
#######
stdout
Maximum rope length is 0.
Maximum rope length is 8.