Advertisement
akashtadwai

ABCPath

Sep 23rd, 2021 (edited)
785
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define IOS                           \
  4.     std::ios::sync_with_stdio(false); \
  5.     cin.tie(NULL);                    \
  6.     cout.tie(NULL);
  7. char grid[55][55];
  8. vector<vector<int>> dp;
  9. int n, m;
  10. vector<pair<int, int>> directions{{0, 1}, {1, 1}, {1, 0}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
  11. bool isvalid(int r, int c) { return (r >= 0 and r < n and c >= 0 and c < m); }
  12. int dfs(int i, int j, char curr)
  13. {
  14.     if (dp[i][j] != -1)
  15.         return dp[i][j];
  16.     dp[i][j] = 1LL;
  17.     char next = curr + 1;
  18.     for (auto dir : directions)
  19.     {
  20.         int x_next = dir.first + i;
  21.         int y_next = dir.second + j;
  22.         if (isvalid(x_next, y_next) and grid[x_next][y_next] == next)
  23.             dp[i][j] = max(dp[i][j], 1 + dfs(x_next, y_next, next));
  24.     }
  25.     return dp[i][j];
  26. }
  27. void init()
  28. {
  29.     int x = 1;
  30.     while (1)
  31.     {
  32.         cin >> n >> m;
  33.         if (n == 0 and m == 0)
  34.             break;
  35.         dp.clear();
  36.         dp.resize(55, vector<int>(55, -1));
  37.         for (int i = 0; i < n; i++)
  38.         {
  39.             for (int j = 0; j < m; j++)
  40.             {
  41.                 cin >> grid[i][j];
  42.             }
  43.         }
  44.         int mx = 0;
  45.         for (int i = 0; i < n; i++)
  46.         {
  47.             for (int j = 0; j < m; j++)
  48.             {
  49.                 if (grid[i][j] == 'A')
  50.                 {
  51.                     mx = max(mx, dfs(i, j, 'A'));
  52.                 }
  53.             }
  54.         }
  55.  
  56.         cout << "Case " << x << ": " << mx << endl;
  57.         x++;
  58.     }
  59. }
  60. int32_t main()
  61. {
  62.     IOS;
  63.     init();
  64.     return 0;
  65. }
  66.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement