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