### shubhamphpefy's blog

By shubhamphpefy, history, 5 weeks ago,

I submitted below codes for this problem.I got AC for one and TLE for other. I am unable to find how its happening. Please Help.

AC Submission
TLE Submission
• 0

 » 5 weeks ago, # |   -13 -is-this-fft- Maybe you can help.
 » 5 weeks ago, # |   0 Anyone?
 » 5 weeks ago, # |   0 In your TLE submission, you are marking the cell as visited when it is in front of the queue, not when it is added.Let's say you have $b3$ and $c2$ in your queue. You will be adding $a1$ twice as when $b3$ and $c2$ arrive in front of the queue, as $a1$ isn't marked as visited when added by $b3$, so it will be added again when it is $c2$'s turn. This can go upto 8 for points which can be reached from 8 different points. Random C++ Code (cus I'm bored)#include #include #include //. std::array dx = {1, 2, 2, 1, -1, -2, -2, -1}; std::array dy = {2, 1, -1, -2, -2, -1, 1, 2}; int solve() { std::string s, t; std::cin >> s >> t; int src[2], tgt[2]; src[0] = s[0] - 'a'; src[1] = s[1] - '1'; tgt[0] = t[0] - 'a'; tgt[1] = t[1] - '1'; std::vector> visited(8, std::vector(8, 0)); std::queue> Q; Q.push({src[0], src[1], 0}); visited[src[0]][src[1]] = 1; int ans = -1; while(!Q.empty()) { int x = Q.front()[0], y = Q.front()[1], d = Q.front()[2]; Q.pop(); if(x == tgt[0] && y == tgt[1]) { ans = d; break; } for(int k = 0; k < 8; k++) { int tx = x + dx[k]; int ty = y + dy[k]; if(tx >= 0 && tx < 8 && ty >= 0 && ty < 8 && visited[tx][ty] == 0) { visited[tx][ty] = 1; Q.push({tx, ty, d + 1}); } } } return ans; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0);std::cout.tie(0); #ifndef ONLINE_JUDGE freopen("input", "r", stdin); freopen("output", "w", stdout); #endif int tst; std::cin >> tst; while(tst--) std::cout << solve() << '\n'; return 0; } // g++ -std=c++20 .cpp