Problem link : https://cses.fi/problemset/task/1194 Submission link: https://cses.fi/problemset/result/6625261/
I have done the problem in n*m which is the most optimised one, (1<=n,m<=10^3) . Even then I am getting TLE in one test case
| # | User | Rating |
|---|---|---|
| 1 | Benq | 3792 |
| 2 | VivaciousAubergine | 3647 |
| 3 | Kevin114514 | 3611 |
| 4 | jiangly | 3583 |
| 5 | strapple | 3515 |
| 6 | tourist | 3470 |
| 7 | dXqwq | 3436 |
| 8 | Radewoosh | 3415 |
| 9 | Otomachi_Una | 3413 |
| 10 | Um_nik | 3376 |
| # | User | Contrib. |
|---|---|---|
| 1 | Qingyu | 161 |
| 2 | adamant | 150 |
| 3 | Um_nik | 146 |
| 4 | Dominater069 | 144 |
| 5 | errorgorn | 141 |
| 6 | cry | 139 |
| 7 | Proof_by_QED | 136 |
| 8 | YuukiS | 135 |
| 9 | chromate00 | 134 |
| 9 | TheScrasse | 134 |
Problem link : https://cses.fi/problemset/task/1194 Submission link: https://cses.fi/problemset/result/6625261/
I have done the problem in n*m which is the most optimised one, (1<=n,m<=10^3) . Even then I am getting TLE in one test case
| Name |
|---|



Start a BFS from E, and find shortest distance ($$$D_{N_{i}}$$$) to every node ($$$(N_{i}$$$) and if for any monster, there exists a ($$$D_{N_{i}}$$$) which is less than or equal to ($$$D_{N_{i}}$$$) from S to E, we can't reach the end point, otherwise we can.
You can check this out. Hope it helps
I havent used maps , I have only used 2-D arrays
2-D arrays is much faster than maps, If u can implement my solution by replacing maps with 2-D arrays, then ur code's runtime would be lesser than mine and would get accepted at the very first try as I have to grind a lot to escape TLE.
your submission link isn't working, it's blank.
I guess CSES submission link is private-
include "bits/stdc++.h"
using namespace std;
define ll long long
int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ll n, m; cin >> n >> m; vector<vector> arr(n, vector(m)); ll b = -1, c = -1; queue<pair<ll, ll>> q; vector<vector> visited(n, vector(m, false));
for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { cin >> arr[i][j]; if (arr[i][j] == 'A') { b = i; c = j; } else if (arr[i][j] == 'M') { q.push({i, j}); visited[i][j] = true; } } } vector<vector<ll>> time(n, vector<ll>(m, INT_MAX)); ll steps = 0; while (!q.empty()) { ll sz = q.size(); while (sz--) { auto front = q.front(); q.pop(); ll x = front.first, y = front.second; time[x][y] = steps; if (x + 1 < n and !visited[x + 1][y] and arr[x + 1][y] == '.') { visited[x + 1][y] = true; q.push({x + 1, y}); } if (x - 1 >= 0 and !visited[x - 1][y] and arr[x - 1][y] == '.') { visited[x - 1][y] = true; q.push({x - 1, y}); } if (y + 1 < m and !visited[x][y + 1] and arr[x][y + 1] == '.') { visited[x][y + 1] = true; q.push({x, y + 1}); } if (y - 1 >= 0 and !visited[x][y - 1] and arr[x][y - 1] == '.') { visited[x][y - 1] = true; q.push({x, y - 1}); } } ++steps; } // for (auto x : time) // { // for (auto y : x) // cout << y << " "; // cout << endl; // } for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) visited[i][j] = false; } queue<pair<string, pair<ll, ll>>> pq; pq.push({"", {b, c}}); visited[b][c] = true; steps = 0; while (!pq.empty()) { ll sz = pq.size(); while (sz--) { auto front = pq.front(); pq.pop(); string res = front.first; ll x = front.second.first, y = front.second.second; // cout << x << " " << y << endl; visited[x][y] = true; if (x == 0 or x == n - 1 or y == 0 or y == m - 1) { // cout << x << " " << y << endl; cout << "YES" << endl; cout << steps << endl << res; return 0; } if (x + 1 < n and !visited[x + 1][y] and arr[x + 1][y] == '.' and steps + 1 < time[x + 1][y]) { visited[x + 1][y] = true; pq.push({res + 'D', {x + 1, y}}); } if (x - 1 >= 0 and !visited[x - 1][y] and arr[x - 1][y] == '.' and steps + 1 < time[x - 1][y]) { visited[x - 1][y] = true; pq.push({res + 'U', {x - 1, y}}); } if (y + 1 < m and !visited[x][y + 1] and arr[x][y + 1] == '.' and steps + 1 < time[x][y + 1]) { visited[x][y + 1] = true; pq.push({res + 'R', {x, y + 1}}); } if (y - 1 >= 0 and !visited[x][y - 1] and arr[x][y - 1] == '.' and steps + 1 < time[x][y - 1]) { visited[x][y - 1] = true; pq.push({res + 'L', {x, y - 1}}); } } ++steps; } cout << "NO"; return 0;}
your code is pushing too many strings into a queue, which each of them are O(n), where n is the length of the string, and that's what makes your code slow. Your approach is totally fine, I guess. So you just need to change the last part of the code.
You should learn Multi source BFS