fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fastio \
  5.   ios_base::sync_with_stdio(0); \
  6.   cin.tie(0); \
  7.   cout.tie(0)
  8.  
  9. typedef long long ll;
  10. typedef pair<ll, ll> pll;
  11. const ll MOD = 1000000007;
  12. const ll INF = 1e18;
  13. const ll MAX_SIZE = 55;
  14.  
  15. // 8 directions
  16. const ll rows[] = {1, -1, 0, 0, 1, -1, 1, -1};
  17. const ll cols[] = {0, 0, 1, -1, 1, -1, -1, 1};
  18.  
  19. char arr[MAX_SIZE][MAX_SIZE];
  20. ll dp[MAX_SIZE][MAX_SIZE];
  21. ll N, M;
  22.  
  23. bool isValid(ll i, ll j)
  24. {
  25. return ((i >= 1 and i <= N) and (j >= 1 and j <= M));
  26. }
  27.  
  28. int main()
  29. {
  30. fastio;
  31. //freopen('input.txt','r',stdin);
  32. //freopen('output.txt','w',stdout);
  33. ll cnt = 0;
  34. while (true)
  35. {
  36. cnt++;
  37. cin >> N >> M;
  38. if (!N and !M)
  39. break;
  40.  
  41. ll longest = 0;
  42. for (int i = 1; i <= N; i++)
  43. {
  44. for (int j = 1; j <= M; j++)
  45. {
  46. dp[i][j] = -INF;
  47. cin >> arr[i][j];
  48.  
  49. if (arr[i][j] == 'A')
  50. dp[i][j] = 1;
  51. }
  52. }
  53.  
  54. for (char ch = 'A'; ch <= 'Z'; ch++)
  55. {
  56. for (int i = 1; i <= N; i++)
  57. for (int j = 1; j <= M; j++)
  58. {
  59. if (arr[i][j] != ch)
  60. continue;
  61.  
  62. for (int k = 0; k < 8; k++)
  63. {
  64. ll newX = i + rows[k];
  65. ll newY = j + cols[k];
  66. if (isValid(newX, newY) and (arr[newX][newY] == (ch - 1)))
  67. {
  68. dp[i][j] = max(dp[newX][newY] + 1, dp[i][j]);
  69. }
  70. }
  71.  
  72. longest = max(longest, dp[i][j]); // Line different from Wrong solution
  73. }
  74. }
  75.  
  76. cout << "Case " << cnt << ": " << longest << endl;
  77. }
  78.  
  79. return 0;
  80. }
  81. // END OF SOURCE CODE
Success #stdin #stdout 0s 15264KB
stdin
0 0
stdout
Standard output is empty