fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. using namespace std;
  6. const int dx[8] = {1, 1, 1, 0, 0, -1, -1, -1};
  7. const int dy[8] = {1, 0, -1, 1, -1, 1, 0, -1};
  8. int main() {
  9. for (int cs = 1;; cs++) {
  10. int R, C; cin >> R >> C; if (R==0) return 0;
  11. vector<string> V(R);
  12. vector<vector<int> > dp(R, vector<int>(C, -1e9));
  13. for (int i = 0; i < R; i++) {
  14. cin >> V[i];
  15. }
  16. int res = 0;
  17. for (char c = 'A'; c <= 'Z'; c++) {
  18. for (int i = 0; i < R; i++) {
  19. for (int j = 0; j < C; j++) {
  20. if (V[i][j] != c) continue;
  21. if (c == 'A') dp[i][j] = 1;
  22. for (int k = 0; k < 8; k++) {
  23. int r = i + dx[k], c = j + dy[k];
  24. if (r >= 0 && c >= 0 && r < R && c < C &&
  25. V[r][c] == V[i][j] - 1) {
  26. dp[i][j] = max(dp[i][j], dp[r][c] + 1);
  27. }
  28. }
  29. res = max(res, dp[i][j]);
  30. }
  31. }
  32. }
  33. cout << "Case " << cs << ": " << res << endl;
  34. }
  35. }
Success #stdin #stdout 0s 3236KB
stdin
4  3

ABE

CFG

BDH

ABC

0 0
stdout
Case 1: 4