📌  相关文章
📜  使用给定的操作最小化到达矩阵右下角的成本

📅  最后修改于: 2021-09-03 03:30:25             🧑  作者: Mango

给定一个大小为N x N的矩阵grid[][] ,任务是找到从左上角到达矩阵右下角所需的最小成本,其中移动到新单元格的成本是[S /2] + K ,其中S是前一个索引处的分数, K是当前索引处的矩阵元素。
注意:这里, [X] 是不超过X的最大整数。

例子:

处理方法:按照以下步骤解决问题:

  • 使矩阵的第一个元素为零
  • [0, N-1]范围内遍历:
    • 初始化一个列表,比如moveList ,并附加移动ij
    • 初始化另一个列表,比如可能的方式,并在其中附加moveList
    • 初始化一个列表,比如可能的WaysSum,最初是一个空列表。
    • 遍历列表可能的方式
      • 遍历附加的moveList
        • 检查移动是否等于i,然后更新i = i + 1。
        • 否则,更新j = j + 1
        • 初始化一个变量,比如tempSum。设置tempSum = tempSum + grid[i][j]
      • 在循环后的可能方式列表中附加tempSum
  • 打印来自possibleWaysSum的最小成本作为输出。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
vector > possibleWays;
 
// Returns true if str[curr] does not matches with any
// of the characters after str[start]
bool shouldSwap(char str[], int start, int curr)
{
  for (int i = start; i < curr; i++) {
    if (str[i] == str[curr]) {
      return false;
    }
  }
  return true;
}
 
// Function for the swap
void swap(char str[], int i, int j)
{
  char c = str[i];
  str[i] = str[j];
  str[j] = c;
}
 
// Prints all distinct permutations in str[0..n-1]
void findPermutations(char str[], int index, int n)
{
  if (index >= n) {
    vector l;
    for (int i = 0; i < n; i++)
      l.push_back(str[i]);
    possibleWays.push_back(l);
    return;
  }
 
  for (int i = index; i < n; i++) {
 
    // Proceed further for str[i] only if it
    // doesn't match with any of the characters
    // after str[index]
    bool check = shouldSwap(str, index, i);
    if (check) {
      swap(str, index, i);
      findPermutations(str, index + 1, n);
      swap(str, index, i);
    }
  }
}
 
// Function to print the minimum cost
void minCost(int grid[][5], int N)
{
 
  vector moveList;
 
  // Making top-left value 0
  // implicitly to generate list of moves
  grid[0][0] = 0;
  vector possibleWaysSum;
 
  for (int k = 0; k < N - 1; k++) {
 
    moveList.push_back('i');
    moveList.push_back('j');
 
    possibleWays.clear();
 
    // Convert into set to make only unique values
    int n = moveList.size();
    char str[n];
    for (int i = 0; i < n; i++)
      str[i] = moveList[i];
 
    // To store the unique permutation of moveLst
    // into the possibleWays
    findPermutations(str, 0, n);
    possibleWaysSum.clear();
 
    // Traverse the list
    for (vector way : possibleWays) {
      int i = 0, j = 0, tempSum = 0;
      for (char move : way) {
        if (move == 'i') {
          i += 1;
        }
        else {
          j += 1;
        }
 
        // Stores cost according to given
        // conditions
        tempSum = (int)(floor(tempSum / 2))
          + grid[i][j];
      }
      possibleWaysSum.push_back(tempSum);
    }
  }
 
  // Print the minimum possible cost
  int ans = possibleWaysSum[0];
  for (int i = 1; i < possibleWaysSum.size(); i++)
    ans = min(ans, possibleWaysSum[i]);
 
  cout << ans;
}
 
// Driven Program
int main()
{
   
  // Size of the grid
  int N = 4;
 
  // Given grid[][]
  int grid[][5] = { { 0, 3, 9, 6 },
                   { 1, 4, 4, 5 },
                   { 8, 2, 5, 4 },
                   { 1, 8, 5, 9 } };
 
  // Function call to print the minimum
  // cost to reach bottom-right corner
  // from the top-left corner of the matrix
  minCost(grid, N);
 
  return 0;
}
 
// This code is contributed by Kingash.


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG
{
    static ArrayList > possibleWays;
 
    // Returns true if str[curr] does not matches with any
    // of the characters after str[start]
    static boolean shouldSwap(char str[], int start,
                              int curr)
    {
        for (int i = start; i < curr; i++)
        {
            if (str[i] == str[curr])
            {
                return false;
            }
        }
        return true;
    }
 
    // Prints all distinct permutations in str[0..n-1]
    static void findPermutations(char str[], int index,
                                 int n)
    {
        if (index >= n)
        {
            ArrayList l = new ArrayList<>();
            for (char ch : str)
                l.add(ch);
            possibleWays.add(l);
            return;
        }
 
        for (int i = index; i < n; i++) {
 
            // Proceed further for str[i] only if it
            // doesn't match with any of the characters
            // after str[index]
            boolean check = shouldSwap(str, index, i);
            if (check) {
                swap(str, index, i);
                findPermutations(str, index + 1, n);
                swap(str, index, i);
            }
        }
    }
 
    // Function for the swap
    static void swap(char[] str, int i, int j)
    {
        char c = str[i];
        str[i] = str[j];
        str[j] = c;
    }
 
    // Function to print the minimum cost
    static void minCost(int grid[][], int N)
    {
 
        ArrayList moveList = new ArrayList<>();
 
        // Making top-left value 0
        // implicitly to generate list of moves
        grid[0][0] = 0;
        ArrayList possibleWaysSum
            = new ArrayList<>();
 
        for (int k = 0; k < N - 1; k++) {
 
            moveList.add('i');
            moveList.add('j');
 
            possibleWays = new ArrayList<>();
 
            // Convert into set to make only unique values
            int n = moveList.size();
            char str[] = new char[n];
            for (int i = 0; i < n; i++)
                str[i] = moveList.get(i);
 
            // To store the unique permutation of moveLst
            // into the possibleWays
            findPermutations(str, 0, n);
            possibleWaysSum = new ArrayList<>();
 
            // Traverse the list
            for (ArrayList way : possibleWays) {
                int i = 0, j = 0, tempSum = 0;
                for (char move : way) {
                    if (move == 'i') {
                        i += 1;
                    }
                    else {
                        j += 1;
                    }
                   
                    // Stores cost according to given
                    // conditions
                    tempSum = (int)(Math.floor(tempSum / 2))
                              + grid[i][j];
                }
                possibleWaysSum.add(tempSum);
            }
        }
 
        // Print the minimum possible cost
        int ans = possibleWaysSum.get(0);
        for (int i = 1; i < possibleWaysSum.size(); i++)
            ans = Math.min(ans, possibleWaysSum.get(i));
 
        System.out.println(ans);
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // Size of the grid
        int N = 4;
 
        // Given grid[][]
        int grid[][] = { { 0, 3, 9, 6 },
                         { 1, 4, 4, 5 },
                         { 8, 2, 5, 4 },
                         { 1, 8, 5, 9 } };
 
        // Function call to print the minimum
        // cost to reach bottom-right corner
        // from the top-left corner of the matrix
        minCost(grid, N);
    }
}
 
// This code is contributed by Kingash.


Python3
# Python3 program for the above approach
 
from itertools import permutations
from math import floor
 
# Function to print the minimum cost
 
 
def minCost(grid, N):
    moveList = []
 
    # Making top-left value 0
    # implicitly to generate list of moves
    grid[0][0] = 0
 
    for i in range(N - 1):
        moveList.append('i')
        moveList.append('j')
 
        # Convert into set to make only unique values
        possibleWays = list(set(permutations(moveList)))
        possibleWaysSum = []
 
        # Traverse the list
        for way in possibleWays:
            i, j, tempSum = 0, 0, 0
            for move in way:
                if move == 'i':
                    i += 1
                else:
                    j += 1
 
                # Stores cost according to given conditions
                tempSum = floor(tempSum/2) + grid[i][j]
 
            possibleWaysSum.append(tempSum)
        minWayIndex = possibleWaysSum.index(min(possibleWaysSum))
 
    # Print the minimum possible ccost
    print(min(possibleWaysSum))
 
 
# Size of the grid
N = 4
 
# Given grid[][]
grid = [[0, 3, 9, 6], [1, 4, 4, 5], [8, 2, 5, 4], [1, 8, 5, 9]]
 
# Function call to print the minimum
# cost to reach bottom-right corner
# from the top-left corner of the matrix
minCost(grid, N)


输出:
12

时间复杂度: O(N 2 )
辅助空间: O(N)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live