fork(5) download
  1. #include <bits/stdc++.h>>
  2.  
  3. using namespace std;
  4.  
  5. #define For(i,a,b) for(int i=(int)(a);i<=(int)(b);++i)
  6. #define Fill(a,b) memset(&a,b,sizeof(a))
  7.  
  8. const int dr[]={0,0,1,-1};
  9. const int dc[]={1,-1,0,0};
  10.  
  11. int cols, rows, maxd, maxr, maxc;
  12. char s[1005][1005], v[1005][1005];
  13.  
  14. void dfs(int r, int c, int l){
  15. if (l > maxd) {maxd=l; maxr=r; maxc=c;}
  16. v[r][c]=1;
  17. For(i,0,3) if (r+dr[i]>=0&&r+dr[i]<rows&&c+dc[i]>=0&&c+dc[i]<cols&&
  18. v[r+dr[i]][c+dc[i]]==0&&s[r+dr[i]][c+dc[i]]=='.')
  19. dfs(r+dr[i],c+dc[i],l+1);
  20. }
  21.  
  22. int main(){
  23. int t;
  24. scanf("%d",&t);
  25.  
  26. For(z,1,t) {
  27. scanf("%d%d\n",&cols,&rows);
  28. For(j,1,rows) gets(s[j-1]);
  29. For(i,0,rows-1) For(j,0,cols-1) if (s[i][j]=='.') {
  30. maxd=-1; Fill(v,0);
  31. dfs(i,j,0);
  32.  
  33. maxd=-1; Fill(v,0);
  34. dfs(maxr,maxc,0);
  35.  
  36. printf("Maximum rope length is %d.\n",maxd);
  37. goto fin;
  38. }
  39. fin:;
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5316KB
stdin
3
3 3
###
#.#
###
7 6
#######
#.#.###
#.#.###
#.#.#.#
#.....#
####### 
5 5 
##.## 
##.## 
.....
##.## 
##.##
stdout
Maximum rope length is 0.
Maximum rope length is 8.
Maximum rope length is 4.