Codeforces and Polygon may be unavailable from May 23, 4:00 (UTC) to May 23, 8:00 (UTC) due to technical maintenance. ×
Rating changes for last rounds are temporarily rolled back. They will be returned soon. ×

pratikshaw's blog

By pratikshaw, history, 10 months ago, In English

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

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by pratikshaw (previous revision, new revision, compare).

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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.

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Yeah I have done that. All cases have been accepted except for one which is giving TLE.

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You can check this out. Hope it helps

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I havent used maps , I have only used 2-D arrays

    • »
      »
      »
      10 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        It still isnt getting accepted. TLE is still there

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

your submission link isn't working, it's blank.

I guess CSES submission link is private-

  • »
    »
    10 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    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;

    }

    • »
      »
      »
      10 months ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        10 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        But I am pushing only those which are necessary, i think

»
10 months ago, # |
  Vote: I like it 0 Vote: I do not like it

You should learn Multi source BFS