📌  相关文章
📜  通过选择设置位索引并将整行更改为 1 将给定坐标设置为 1 的最小操作

📅  最后修改于: 2022-05-13 01:56:07.825000             🧑  作者: Mango

通过选择设置位索引并将整行更改为 1 将给定坐标设置为 1 的最小操作

给定具有N行和M列的二进制矩阵arr[][] ,任务是计算将坐标(x, y)的值设置为1所需的最小操作次数,其中每个操作选择任意索引,使得它的值为1并将其所有行元素或列元素设置为1

例子:

方法:给定的问题是一个基于实现的问题,它可以分为以下几种情况:

  • 案例 1 arr[x][y]已经是1 。在这种情况下,将需要0 次操作。
  • 情况 2,其中第x行或第y列中的任何一个都包含值为1的索引。在这种情况下,将需要1次操作。
  • 情况 3,其中至少存在一个块,使得arr[i][j] = 1 。在这种情况下,将需要2次移动。
  • 情况 4,其中不存在值为1的块。在这种情况下,不可能执行给定的任务。

因此,请按照将提供所需答案的相应顺序检查每个提到的案例。

下面是上述方法的实现:

C++
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find minimum operations
// required to set the value of the given
// coordinates as 1 in matrix arr[][]
int minOperations(
    vector > arr,
    int x, int y)
{
    // Case 1 where arr[x][y] = 1
    if (arr[x][y]) {
        // Return Answer
        return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr[0].size(); i++) {
 
        // Case 2 for Xth row
        if (arr[x][i])
            return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.size(); j++) {
 
        // Case 2 for Yth column
        if (arr[j][y])
            return 1;
    }
 
    // Loop to traverse arr[][]
    for (int i = 0; i < arr.size(); i++) {
        for (int j = 0; j < arr[0].size(); j++) {
 
            // Case 3 where any
            // arr[i][j] = 1
            if (arr[i][j])
                return 1;
        }
    }
 
    // Case 4
    return -1;
}
 
// Driver Code
int main()
{
    vector > arr{ { 0, 1, 0, 0 },
                              { 1, 1, 1, 0 },
                              { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    cout << minOperations(arr, x, y);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
  // Function to find minimum operations
  // required to set the value of the given
  // coordinates as 1 in matrix arr[][]
  static int minOperations(
    int[][] arr,
    int x, int y)
  {
    // Case 1 where arr[x][y] = 1
    if (arr[x][y] > 0) {
      // Return Answer
      return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr[0].length; i++) {
 
      // Case 2 for Xth row
      if (arr[x][i] > 0)
        return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.length; j++) {
 
      // Case 2 for Yth column
      if (arr[j][y] > 0)
        return 1;
    }
 
    // Loop to traverse arr[][]
    for (int i = 0; i < arr.length; i++) {
      for (int j = 0; j < arr[0].length; j++) {
 
        // Case 3 where any
        // arr[i][j] = 1
        if (arr[i][j] > 0)
          return 1;
      }
    }
 
    // Case 4
    return -1;
  }
 
  // Driver Code
  public static void main (String[] args) {
    int[][] arr = { { 0, 1, 0, 0 },
                   { 1, 1, 1, 0 },
                   { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    System.out.println(minOperations(arr, x, y));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python program of the above approach
 
# Function to find minimum operations
# required to set the value of the given
# coordinates as 1 in matrix arr[][]
def minOperations(arr, x,  y):
 
    # Case 1 where arr[x][y] = 1
    if (arr[x][y]):
        # Return Answer
        return 0
 
    # Loop to iterate a row
    for i in range(0, len(arr[0])):
 
        # Case 2 for Xth row
        if (arr[x][i]):
            return 1
 
    # Loop to iterate a column
    for j in range(0, len(arr)):
 
        # Case 2 for Yth column
        if (arr[j][y]):
            return 1
 
    # Loop to traverse arr[][]
    for i in range(0, len(arr)):
        for j in range(0, len(arr[0])):
 
            # Case 3 where any
            # arr[i][j] = 1
            if (arr[i][j]):
                return 1
 
    # Case 4
    return -1
 
# Driver Code
 
arr = [[0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]]
x = 1
y = 3
 
print(minOperations(arr, x, y))
 
# This code is contributed by Taranpreet


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
 
  // Function to find minimum operations
  // required to set the value of the given
  // coordinates as 1 in matrix [,]arr
  static int minOperations(
    int[,] arr,
    int x, int y)
  {
 
    // Case 1 where arr[x,y] = 1
    if (arr[x, y] > 0)
    {
 
      // Return Answer
      return 0;
    }
 
    // Loop to iterate a row
    for (int i = 0; i < arr.GetLength(0); i++) {
 
      // Case 2 for Xth row
      if (arr[x,i] > 0)
        return 1;
    }
 
    // Loop to iterate a column
    for (int j = 0; j < arr.Length; j++) {
 
      // Case 2 for Yth column
      if (arr[j,y] > 0)
        return 1;
    }
 
    // Loop to traverse [,]arr
    for (int i = 0; i < arr.GetLength(0); i++) {
      for (int j = 0; j < arr.GetLength(1); j++) {
 
        // Case 3 where any
        // arr[i,j] = 1
        if (arr[i,j] > 0)
          return 1;
      }
    }
 
    // Case 4
    return -1;
  }
 
  // Driver Code
  public static void Main(String[] args) {
    int[,] arr = { { 0, 1, 0, 0 },
                  { 1, 1, 1, 0 },
                  { 0, 0, 1, 1 } };
    int x = 1;
    int y = 3;
 
    Console.WriteLine(minOperations(arr, x, y));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
1

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