gourav_27's blog

By gourav_27, history, 4 months ago, In English

Here is the problem...

You are given three integers m, n, and k. Construct any m x n grid consisting only of the characters '.' and '#', where:

  • '.' represents a free cell.
  • '#' represents an obstacle cell.

A valid path is a sequence of free cells that:

  • Starts at the top-left cell (0, 0).
  • Ends at the bottom-right cell (m - 1, n - 1).
  • Moves only:
  • Right, from (i, j) to (i, j + 1), or
  • Down, from (i, j) to (i + 1, j).

Return any grid such that there are exactly k valid paths from the top-left cell to the bottom-right cell. If no such grid exists, return an empty array.

Example 1: Input: m = 2, n = 3, k = 2 Output: ["...","#.."] Explanation:

There are exactly k = 2 valid paths from (0, 0) to (1, 2):

  • (0, 0) → (0, 1) → (0, 2) → (1, 2)
  • (0, 0) → (0, 1) → (1, 1) → (1, 2)

Example 2: Input: m = 3, n = 3, k = 4 Output: ["..#","...","#.."] Explanation:

There are exactly k = 4 valid paths from (0, 0) to (2, 2):

  • (0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2)
  • (0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)
  • (0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)
  • (0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2)

Example 3: Input: m = 1, n = 4, k = 2 Output: [] Explanation: No grid exists with exactly k = 2 valid paths for a 1 x 4 grid, so the answer is an empty array.

Constraints: * 1 <= m, n <= 1000 * 1 <= k <= 1e12

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

»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it
»
4 months ago, hide # |
 
Vote: I like it +8 Vote: I do not like it

105259B - Mazes has the same setup, but it lets you choose $$$n$$$ and $$$m$$$.As for your problem, when $$$n \gt 80$$$ and $$$m \gt 40$$$, you can just reuse a construction from my problem to get a valid answer, but I'm not sure whether there is a general solution because you may have constraints under which most simple constructions would fail but there would still exist complex ones which only work for some values of $$$k$$$.

»
4 months ago, hide # |
← Rev. 2  
Vote: I like it +3 Vote: I do not like it

I'm away so if I solve it'll be in a reply/edit but the main thing I found that is probably a path to the solution. First it is far easier to assume all obstacles then build a path.

So realise the path going to bottomleftmost node (i.e.full bottom then full right) node is 1 path exactly.

if we go to the above row and reveal one column we get 1 more apth. if we add another we get 1, and so on. so thats clean.

So the pattern, om each row: then if we add another column, we get 1 x number of paths on the bottom row following this node, meaning it gets complicated but its workable.

i.e 1st node we add on row 3 is the number of revealed row 2 columns — 1. then 2nd column we reveal is number of revealed row2 cols -1, and so on. because we now have got a new way to reach every one of those nodes following this one.

the same applies continuously i.e row 4, first coll we add is the sum of row 3 contrib, nezt col is sum of row 3 contrib — first, and so on and so forth.

So you can dp this, here you can pull DP (number of paths = number of paths in the node to the right of it + number of paths in the node to the bottom of it) or the opposite way (number of total paths subtracted by the paths to the left with it, using a prefix sum). not fully sure. when i'm done ill do it

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

    okay... I think I got the gist of the idea, but I'd be really grateful if you could solve it and provide the code here... It would help me understand the approach much better, & Thank you:)

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

I'm not 100% sure but I think I'm pretty close! This is a permutation problem. To see the number of possible path, you need to use this formula:

P = (m+n-2)!/((n-1)!*(m-1)!)

if P>K: you can solve it

if P=K: the solution is the given grid without obstacles

if P<K: there is no grid -> []

Now we need to look at the first case (P>K) (this is the part I'm not fully sure, but it should work)

Let's say we have m=5,n=4,k=6 and create the grid: . . . . . . . . . . . . . . . . . . . .

we can find the number of steps to reach any point by adding the number of steps of the points over ad on the left of the point we are considering. 1--1--1--1 1--2--3--4 1--3--6--10 1--4--10--20 1--5--15--35

if we use the formula from above P=35

since K=6 we need to find this grid: 1--?--?--? ?--?--?--? ?--?--?--? ?--?--?--? ?--?--?--6

I tried some approaches but i couldn't find one that would work on every possible set of input. I know this isn't the solution but at least you can eliminate 2 cases

»
4 months ago, hide # |
 
Vote: I like it -10 Vote: I do not like it

Hi, if you're stil stuck here's my solution for this problem considering the constraints

#include <bits/stdc++.h>
using namespace std;


void solve(){
    int n;
    int m;
    long long k;
    cin >> m;
    cin >> n;
    cin >> k;
    int maxlength = m + n;
    vector<vector<long long>> C(maxlength, vector<long long>(maxlength, 0));
    for(int i =0; i < maxlength; i++){
        C[i][0] = 1;

        for(int j =1; j <= i; j++){
            C[i][j] = C[i-1][j -1] + C[i-1][j];

            if (C[i][j] > k){
                C[i][j] = k + 1;
            }
        }
    }
    int total_steps = (m -1) + (n -1);
    int lower_steps = m -1;

    if (C[total_steps][lower_steps] < k){
        cout << "-1\n";
        return;
    }

    auto get_path = [&](int x, int y) -> long long {
        if (x >= m || y >= n){
            return 0;
        }
        int dx = m - 1 - x;
        int dy = n - 1 - y;
        return C[dx + dy][dx];
    };
    

    auto add_cap = [&] (long long a, long long b) -> long long {
        return min(k + 1 , a +b);
    };
    auto mul_cap = [&] (long long a, long long b) -> long long {
        if ( a == 0 || b == 0) return 0;
        if ( k / a +1 < b) return k + 1;
        return min(k +1,  a * b);
    };

    vector<string> grid (m, string(n, '.'));
    vector<vector<long long>> dp(m, vector<long long>(n, 0));

    for (int i = 0; i < m; i++) {
        
    
        vector<long long> s(n + 1, 0);
        if (i > 0) {
            for (int c = n - 1; c >= 0; c--) {
                long long cd = mul_cap(dp[i-1][c], get_path(i, c));
                s[c] = add_cap(s[c+1], cd);
            }
        }
        
        long long p = 0; 

        for (int j = 0; j < n; j++) {
            
            if ((i == 0 && j == 0) || (i == m - 1 && j == n - 1)) {
                dp[i][j] = (i == 0 && j == 0) ? 1 : add_cap(dp[i-1][j], dp[i][j-1]);
                
               
                long long cd = mul_cap(dp[i][j], get_path(i+1, j));
                p = add_cap(p, cd);
                continue;
            }

            
            long long ct = add_cap(p, s[j + 1]);

            if (ct >= k) {
                
                grid[i][j] = '#';
                dp[i][j] = 0; 
            } else {
                
                grid[i][j] = '.';
                long long dh = (i > 0) ? dp[i-1][j] : 0;
                long long dg = (j > 0) ? dp[i][j-1] : 0;
                dp[i][j] = add_cap(dh, dg);
            }

            long long cd = mul_cap(dp[i][j], get_path(i+1, j));
            p = add_cap(p, cd);
        }
    }

    for (int i = 0; i < m; i++) {
        cout << grid[i] << "\n";
    }

    }


int main()  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();

  return 0;
}
»
4 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Top___Cat, now we cant make it a problem in our round :(