pratikshaw's blog

By pratikshaw, history, 17 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

| Write comment?
»
17 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-

  • »
    »
    17 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;

    }

    • »
      »
      »
      17 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.

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

You should learn Multi source BFS