fork(2) download
  1. /*
  2.   *** PATIENCE ABOVE PERFECTION ***
  3.   "When in doubt, use brute force. :D"
  4. */
  5. #include<bits/stdc++.h>
  6. using namespace std;
  7. #define pb push_back
  8. #define mp make_pair
  9. #define nline cout<<"\n"
  10. #define fast ios_base::sync_with_stdio(false),cin.tie(0)
  11. #define ull unsigned long long int
  12. #define ll long long int
  13. #define pii pair<int,int>
  14. #define MAXX 55
  15. #define fr(a,b,i) for(int i=a;i<b;i++)
  16. vector<int>G[MAXX];
  17. int X[]={1,1,0,-1,-1,-1,0,1};
  18. int Y[]={0,1,1,1,0,0,-1,-1,-1};
  19. int d=0,h,w;
  20. char ch[MAXX][MAXX];
  21. bool vis[MAXX][MAXX];
  22. int maxx=-1;
  23. struct data
  24. {
  25. int x,y;
  26. void set(int _x,int _y)
  27. {
  28. x=_x;
  29. y=_y;
  30. }
  31. };
  32. void dfs(data src,int d)
  33. {
  34. vis[src.x][src.y]=true;
  35. for(int i=0;i<8;i++)
  36. {
  37. int xx=X[i]+src.x;
  38. int yy=Y[i]+src.y;
  39. if(xx>=0 and xx<w and yy>=0 and yy<h and (not vis[xx][yy]) and ((char)(ch[src.x][src.y]+1))==ch[xx][yy])
  40. {
  41. data x;
  42. x.set(xx,yy);
  43. dfs(x,++d);
  44. }
  45. }
  46. maxx=max(maxx,d);
  47. }
  48. int main()
  49. {
  50. cin>>h>>w;
  51. int final=1;
  52. while(h!=0 and w!=0)
  53. {
  54. fr(0,h,i)
  55. fr(0,w,j)
  56. cin>>ch[i][j];
  57. data point;
  58. vector<data>v;
  59. fr(0,h,i)
  60. {
  61. fr(0,w,j)
  62. {
  63. if(ch[i][j]=='A')
  64. {
  65. point.set(i,j);
  66. v.pb(point);
  67. }
  68. }
  69. }
  70. maxx=0;
  71. int f=0;
  72. for(int i=0;i<v.size();i++)
  73. {
  74. memset(vis,false,sizeof vis);
  75. dfs(v[i],0);
  76.  
  77. }
  78. //cout<<v.size()<<endl;
  79. if(not v.empty())
  80. printf("Case %d: %d",final++,maxx+1);
  81. else
  82. printf("Case %d: %d",final++,0);
  83. nline;
  84. cin>>h>>w;
  85. }
  86. return 0;
  87. }
  88.  
Success #stdin #stdout 0s 3104KB
stdin
Standard input is empty
stdout
Standard output is empty