Can some one help me undertand why this solution is giving a TLE for a particular test case.
Task :https://cses.fi/problemset/task/1193/
Solution :
Code
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
bool inside(ll i, ll j, ll r, ll c)
{
return (i < r && j < c && i >= 0 && j >= 0);
}
pair<ll, ll> directions[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
void solve()
{
int n, m;
cin >> n >> m;
vector<vector<char>> arr(n, vector<char>(m, '#'));
int s1 = -1, s2 = -1;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
if (arr[i][j] == 'A')
{
s1 = i;
s2 = j;
}
}
vector<vector<bool>> vis(n, vector<bool>(m, false));
map<pair<int, int>, char> hash;
hash[{0, 1}] = 'R';
hash[{1, 0}] = 'D';
hash[{0, -1}] = 'L';
hash[{-1, 0}] = 'U';
queue<pair<pair<int, int> , string> > q;
// cout<<s1<<" "<<s2<<endl<<endl;
q.push({{s1, s2} , ""});
vis[s1][s2]= true ;
while (q.empty() == false)
{
auto u = q.front();
q.pop();
for (auto p : directions)
{
int x = p.first + u.first.first;
int y = p.second + u.first.second;
if (inside(x, y, n, m) && vis[x][y] ==false && (arr[x][y] == '.' || arr[x][y] == 'B'))
{
string str = u.second;
str += hash[{p.first, p.second}];
// cout<<x<<":"<<y<<endl;
if (arr[x][y] == 'B')
{
cout << "YES" << endl;
cout << str.length() << endl;
cout << str << endl;
return;
}
// cout << x << ":" << y << endl;
vis[x][y] = true;
q.push({{x, y} , str});
}
}
}
cout << "NO" << endl;
}
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ull t = 1;
// cin >> t;
// cout << "nooml";
while (t--)
{
solve();
}
}
...
TLE test case : https://cses.fi/file/3d5f25fa10486d5059bfe87f28554e8fa18d9e0f1f1d6f27f3d3f3d324ab45f0/1/1/