Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define IOS \
- std::ios::sync_with_stdio(false); \
- cin.tie(NULL); \
- cout.tie(NULL);
- char grid[55][55];
- vector<vector<int>> dp;
- int n, m;
- vector<pair<int, int>> directions{{0, 1}, {1, 1}, {1, 0}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
- bool isvalid(int r, int c) { return (r >= 0 and r < n and c >= 0 and c < m); }
- int dfs(int i, int j, char curr)
- {
- if (dp[i][j] != -1)
- return dp[i][j];
- dp[i][j] = 1LL;
- char next = curr + 1;
- for (auto dir : directions)
- {
- int x_next = dir.first + i;
- int y_next = dir.second + j;
- if (isvalid(x_next, y_next) and grid[x_next][y_next] == next)
- dp[i][j] = max(dp[i][j], 1 + dfs(x_next, y_next, next));
- }
- return dp[i][j];
- }
- void init()
- {
- int x = 1;
- while (1)
- {
- cin >> n >> m;
- if (n == 0 and m == 0)
- break;
- dp.clear();
- dp.resize(55, vector<int>(55, -1));
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- cin >> grid[i][j];
- }
- }
- int mx = 0;
- for (int i = 0; i < n; i++)
- {
- for (int j = 0; j < m; j++)
- {
- if (grid[i][j] == 'A')
- {
- mx = max(mx, dfs(i, j, 'A'));
- }
- }
- }
- cout << "Case " << x << ": " << mx << endl;
- x++;
- }
- }
- int32_t main()
- {
- IOS;
- init();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement