fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. using namespace std;
  4. ll n,m;
  5. vector<vector<char>>grid;
  6. vector<vector<ll>>vis;
  7.  
  8. ll cox[8] = {1, -1, 0, 0, 1, 1, -1, -1};
  9. ll coy[8] = {0, 0, -1, 1, 1, -1, 1, -1};
  10. bool ok(ll nx,ll ny){
  11. if (nx >= 0 && nx < n && ny >= 0 && ny < m && !vis[nx][ny])return 1;
  12.  
  13. return 0;
  14. }
  15. ll mx=0,cnt=0;
  16. void dfs(ll x,ll y){
  17. vis[x][y]=1;
  18. cnt++;
  19. mx=max(mx,cnt);
  20. for(int i=0;i<8;i++){
  21. ll nx=x+cox[i];
  22. ll ny=y+coy[i];
  23. char ch=grid[x][y]+1;
  24. if(ok(nx,ny)&&grid[nx][ny]==ch){
  25. dfs(nx,ny);
  26. }
  27. }
  28.  
  29. }
  30. int main() {
  31. do{
  32. int i=1;
  33. cin>>n>>m;
  34. if(n==0&&m==0)break;
  35. vis.assign(n + 1, vector<ll>(m + 1, 0));
  36. grid.assign(n+1, vector<char>(m + 1, 0));
  37. mx=0,cnt=0;
  38. for(int i=0;i<n;i++)for(int j=0;j<m;j++)cin>>grid[i][j];
  39. for(int i=0;i<n;i++)for(int j=0;j<m;j++){
  40. if(!vis[i][j]&&grid[i][j]=='A'){
  41. cnt=0;
  42. dfs(i,j);
  43. }
  44. }
  45. cout<<"Case "<<i<<": "<<mx<<"\n";
  46.  
  47. }while(n!=0&&m!=0);
  48.  
  49. }
  50.  
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Standard output is empty