2 / 2
May 12

#include <bits/stdc++.h>

using namespace std;

vector<vector> moves = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};

bool isValid(int x, int y, int n, int m)
{
return (x >= 0 && y >= 0 && x < n && y < m);
}

void solve(int n, int m, int cnt)
{
vector<vector> arr(n, vector(m));
vector<vector> vis(n, vector(m, 0));

char c;
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; j++)
    {
        cin >> c;
        arr[i][j] = c - 'A';
    }
}

int ans = 0;
for (int i = 0; i < n; i++)
{
    for (int j = 0; j < m; j++)
    {
        if (arr[i][j] != 0)
        {
            continue;
        }
        queue<vector<int>> qq;
        qq.push({i, j});
        vis[i][j] = 1;
        while (!qq.empty())
        {
            int u = qq.front()[0];
            int v = qq.front()[1];
            qq.pop();
            ans = max(ans, arr[u][v] + 1);
            for (auto p : moves)
            {
                int uu = p[0] + u;
                int vv = p[1] + v;
                if (isValid(uu, vv, n, m) && (arr[uu][vv] == arr[u][v] + 1) && !vis[uu][vv])
                {
                    vis[uu][vv] = 1;
                    qq.push({uu, vv});
                }
            }
        }
    }
}
cout << "Case " << (cnt++) << ": " << ans << endl;

}

int main()
{
int n = 1, m = 1, cnt = 1;
while (n != 0 && m != 0)
{
cin >> n >> m;
if (n != 0 && m != 0)
solve(n, m, cnt);
}
}

Can Someone please explain why this give WA instead of TLE?

I can explain the WA - did you test it with multiple test cases?