📌  相关文章
📜  通过放置K 1来最小化从矩阵的左上到右下的唯一路径的数量

📅  最后修改于: 2021-06-25 11:15:35             🧑  作者: Mango

给定两个整数NM ,其中MN表示尺寸N * M的矩阵,仅由0组成。任务是通过仅在矩阵中精确地放置1个K ,以最小化从0组成的单元格从矩阵的左上角(0,0)到右下角(N – 1,M – 1)的唯一路径的数量。

注意:右下方和左上方的单元格都不能修改为0。

例子:

方法:可以通过考虑以下可能的情况来解决该问题。

  1. 如果K≥2:通过在矩阵的(0,1)和(1,0)单元中放置两个1,可以将可能的路径数减少为0
  2. 如果K = 0:计数保持为C(N + M-2,N-1)。
  3. 如果K = 1:将1放在矩阵的中心((N-1)/ 2,(M-1)/ 2)以最小化路径数。因此,这种情况下可能的路径计数如下:

下面是上述方法的实现:

C++
// C++ Program to implememnt
// the above approach
 
#include 
using namespace std;
 
// Function to return the value of
// Binomial Coefficient C(n, k)
int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }
 
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
int countPath(int N, int M, int K)
{
    int answer;
    if (K >= 2)
        answer = 0;
    else if (K == 0)
        answer = ncr(N + M - 2, N - 1);
    else {
 
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        X = ((N - 1) - (N - 1) / 2)
            + ((M - 1) - (M - 1) / 2);
 
        Y = ((N - 1) - (N - 1) / 2);
        midCount *= ncr(X, Y);
        answer -= midCount;
    }
    return answer;
}
 
// Driver Code
int main()
{
    int N = 3;
    int M = 3;
    int K = 1;
 
    cout << countPath(N, M, K);
 
    return 0;
}


Java
// Java Program to implememnt
// the above approach
import java.util.*;
class GFG{
 
// Function to return the value of
// Binomial Coefficient C(n, k)
static int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
static int countPath(int N, int M, int K)
{
    int answer;
    if (K >= 2)
        answer = 0;
    else if (K == 0)
        answer = ncr(N + M - 2, N - 1);
    else
    {
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        X = ((N - 1) - (N - 1) / 2) +
            ((M - 1) - (M - 1) / 2);
        Y = ((N - 1) - (N - 1) / 2);
        midCount *= ncr(X, Y);
        answer -= midCount;
    }
    return answer;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 3;
    int M = 3;
    int K = 1;
    System.out.print(countPath(N, M, K));
}
}
 
// This code is contributed by shikhasingrajput


Python3
#Python3 Program to implememnt
#the above approach
#Function to return the value of
#Binomial Coefficient C(n, k)
def ncr(n, k):
    res = 1
 
    #Since C(n, k) = C(n, n-k)
    if (k > n - k):
        k = n - k
 
    #Calculate the value of
    #[n * (n-1) *---* (n-k+1)] /
    #[k * (k-1) *----* 1]
    for i in range(k):
        res *= (n - i)
        res //= (i + 1)
 
    return res
 
#Function to find the minimum
#count of paths from top
#left to bottom right by
#placing K 1s in the matrix
def countPath(N, M, K):
    answer = 0
    if (K >= 2):
        answer = 0
    elif (K == 0):
        answer = ncr(N + M - 2, N - 1)
    else:
 
        #Count of ways without 1s
        answer = ncr(N + M - 2, N - 1)
 
        #Count of paths from starting
        #poto mid point
        X = (N - 1) // 2 + (M - 1) // 2
        Y = (N - 1) // 2
        midCount = ncr(X, Y)
 
        #Count of paths from mid
        #poto end point
        X = ((N - 1) - (N - 1) // 2)+
            ((M - 1) - (M - 1) // 2)
 
        Y = ((N - 1) - (N - 1) // 2)
        midCount *= ncr(X, Y)
        answer -= midCount
 
    return answer
 
#Driver Code
if __name__ == '__main__':
    N = 3
    M = 3
    K = 1
    print(countPath(N, M, K))
 
# This code is contributed by Mohit Kumar 29


C#
// C# program to implememnt
// the above approach
using System;
 
class GFG{
 
// Function to return the value of
// Binomial Coefficient C(n, k)
static int ncr(int n, int k)
{
    int res = 1;
 
    // Since C(n, k) = C(n, n-k)
    if (k > n - k)
        k = n - k;
 
    // Calculate the value of
    // [n * (n-1) *---* (n-k+1)] /
    // [k * (k-1) *----* 1]
    for(int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
 
// Function to find the minimum
// count of paths from top
// left to bottom right by
// placing K 1s in the matrix
static int countPath(int N, int M, int K)
{
    int answer;
     
    if (K >= 2)
        answer = 0;
    else if (K == 0)
        answer = ncr(N + M - 2, N - 1);
    else
    {
         
        // Count of ways without 1s
        answer = ncr(N + M - 2, N - 1);
 
        // Count of paths from starting
        // point to mid point
        int X = (N - 1) / 2 + (M - 1) / 2;
        int Y = (N - 1) / 2;
        int midCount = ncr(X, Y);
 
        // Count of paths from mid
        // point to end point
        X = ((N - 1) - (N - 1) / 2) +
            ((M - 1) - (M - 1) / 2);
        Y = ((N - 1) - (N - 1) / 2);
         
        midCount *= ncr(X, Y);
        answer -= midCount;
    }
    return answer;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 3;
    int M = 3;
    int K = 1;
     
    Console.Write(countPath(N, M, K));
}
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
2

时间复杂度: O(N + M)
辅助空间: O(1)