📜  检查矩阵是否可以叠加在给定的矩阵上

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

检查矩阵是否可以叠加在给定的矩阵上

给定一个大小为N * M的矩阵letter[][] ,由'#''*'组成,另一个大小为X * Y的矩阵stamp[][]仅包含'$' 。任务是通过将印章矩阵叠加在字母矩阵上来查找是否可以将较大的“*”替换为“$”

注意:在叠加操作中,只能考虑所有字符为“*”“$”的区域。

例子:

方法:可以通过检查两个“#”之间或行尾和行首的“*”字符数来解决问题。如果这些计数中的任何一个小于标记矩阵的行长度,则无法解决,同样适用于列。按照下面提到的步骤来实施该方法:

  • 循环遍历完整的较大矩阵行
  • 对于每一行,计算在行的开头或结尾之前或两个'#'之间的连续'*'的数量。如果此计数小于标记矩阵行长度,则它是不可能的,并返回不可能作为答案。
  • 以这种方式检查整个字母矩阵。
  • 也对列应用相同的过程。
  • 如果字母矩阵可以叠加,则返回 true。否则,返回假。

下面是上述方法的实现

C++
// C++ code to implement the above approach
#include 
using namespace std;
 
// Function to check if superimpose
// is possible or not
bool solution(int n, int m, int x, int y,
              vector& letter)
{
  // Looping over rows
  for (int i = 0; i < n; i++) {
 
    // Initializing a length variable
    // for counting the number of *
    int len = 0;
    for (int j = 0; j < m; j++) {
 
      // If character encountered
      // is *, then we
      // increment the length
      if (letter[i][j] == '*')
        len++;
 
      // If its not then there are
      // two cases
      else {
 
        // 1st case is length is
        // smaller than number of
        // rows in stamp matrix,
        // that would be impossible
        // to stamp so return false
        if (len != 0 && len < x)
          return false;
 
        // 2nd case is if its
        // possible, reset len
        else
          len = 0;
      }
    }
  }
 
  // Do similarly for columns
  // Looping over columns
  for (int i = 0; i < m; i++) {
 
    // Initializing length variable
    int len = 0;
    for (int j = 0; j < n; j++) {
 
      // If encountered character
      // is *, increment length
      if (letter[j][i] == '*')
        len++;
      else {
 
        // If its not possible
        // return false
        if (len != 0 && len < y)
          return false;
 
        // Otherwise reset len
        else
          len = 0;
      }
    }
  }
  return true;
}
 
// Driver code
int main()
{
  int n = 3, x = 2, y = 2;
 
  vector letter = { "#***", "#***", "#***" };
  int m = letter[0].size();
  /*
            Stamp Matrix:
            $$
            $$
            A 2*2 matrix ( x*y)
            */
 
  if (solution(n, m, x, y, letter))
    cout << "Possible\n";
  else
    cout << "Impossible\n";
 
  // 2nd case
  vector letter2 = { "#***", "#*#*", "#***" };
 
  m = letter2[0].size();
 
  if (solution(n, m, x, y, letter2))
    cout << "Possible\n";
  else
    cout << "Impossible\n";
 
  return 0;
}
 
// This code is contributed by rakeshsahni


Java
// Java code to implement the above approach
import java.util.*;
 
class GFG {
 
    // Function to check if superimpose
    // is possible or not
    public static boolean solution(int n,
                                   int m, int x,
                                   int y,
                                   String[] letter)
    {
        // Looping over rows
        for (int i = 0; i < n; i++) {
 
            // Initializing a length variable
            // for counting the number of *
            int len = 0;
            for (int j = 0; j < m; j++) {
 
                // If character encountered
                // is *, then we
                // increment the length
                if (letter[i].charAt(j)
                    == '*')
                    len++;
 
                // If its not then there are
                // two cases
                else {
 
                    // 1st case is length is
                    // smaller than number of
                    // rows in stamp matrix,
                    // that would be impossible
                    // to stamp so return false
                    if (len != 0 && len < x)
                        return false;
 
                    // 2nd case is if its
                    // possible, reset len
                    else
                        len = 0;
                }
            }
        }
 
        // Do similarly for columns
        // Looping over columns
        for (int i = 0; i < m; i++) {
 
            // Initializing length variable
            int len = 0;
            for (int j = 0; j < n; j++) {
 
                // If encountered character
                // is *, increment length
                if (letter[j].charAt(i)
                    == '*')
                    len++;
                else {
 
                    // If its not possible
                    // return false
                    if (len != 0 && len < y)
                        return false;
 
                    // Otherwise reset len
                    else
                        len = 0;
                }
            }
        }
        return true;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = 3, x = 2, y = 2;
 
        String[] letter = { "#***",
                           "#***",
                           "#***" };
        int m = letter[0].length();
        /*
        Stamp Matrix:
        $$
        $$
        A 2*2 matrix ( x*y)
        */
 
        if (solution(n, m, x, y, letter))
            System.out.println("Possible");
        else
            System.out.println("Impossible");
 
        // 2nd case
        String[] letter2 = { "#***",
                            "#*#*",
                            "#***" };
 
        m = letter2[0].length();
 
        if (solution(n, m, x, y, letter2))
            System.out.println("Possible");
        else
            System.out.println("Impossible");
    }
}


Python3
# Python code to implement the above approach
 
# Function to check if superimpose
# is possible or not
def solution(n, m, x, y, letter):
     
    # Looping over rows
    for i in range(n):
       
        # Initializing a length variable
        # for counting the number of *
        len = 0
        for j in range(m):
           
            # If character encountered
            # is *, then we
            # increment the length
            if (letter[i][j] == '*'):
                len += 1
                 
            # If its not then there are
            # two cases
            else:
               
                # 1st case is length is
                # smaller than number of
                # rows in stamp matrix,
                # that would be impossible
                # to stamp so return false
                if (len != 0 and len < x):
                    return False
                 
                # 2nd case is if its
                # possible, reset len
                else:
                    len = 0
     
    # Do similarly for columns
    # Looping over columns
    for i in range(m):
       
        # Initializing length variable
        len = 0
        for j in range(n):
             
            # If encountered character
            # is *, increment length
            if (letter[j][i] == '*'):
                len += 1     
            else:
                # If its not possible
                # return false
                if (len != 0 and len < y):
                    return False
                # Otherwise reset len
                else:
                    len = 0
                     
    return True
 
# Driver code
n = 3
x = 2
y = 2
 
letter = ["#***", "#***", "#***"]
m = len(letter[0])
 
'''
Stamp Matrix:
$$
$$
A 2*2 matrix ( x*y)
'''
if (solution(n, m, x, y, letter)):
    print("Possible")
else:
    print("Impossible")
     
# 2nd case
letter2 = ["#***", "#*#*", "#***"]
m = len(letter2[0])
 
if (solution(n, m, x, y, letter2)):
    print("Possible")
else:
    print("Impossible")
 
# This code is contributed by Shubham Singh


C#
// C# code to implement the above approach
using System;
 
class GFG{
 
// Function to check if superimpose
// is possible or not
public static bool solution(int n, int m, int x, int y,
                            string[] letter)
{
     
    // Looping over rows
    for(int i = 0; i < n; i++)
    {
         
        // Initializing a length variable
        // for counting the number of *
        int len = 0;
        for(int j = 0; j < m; j++)
        {
             
            // If character encountered
            // is *, then we
            // increment the length
            if (letter[i][j] == '*')
                len++;
 
            // If its not then there are
            // two cases
            else
            {
                 
                // 1st case is length is
                // smaller than number of
                // rows in stamp matrix,
                // that would be impossible
                // to stamp so return false
                if (len != 0 && len < x)
                    return false;
 
                // 2nd case is if its
                // possible, reset len
                else
                    len = 0;
            }
        }
    }
 
    // Do similarly for columns
    // Looping over columns
    for(int i = 0; i < m; i++)
    {
         
        // Initializing length variable
        int len = 0;
        for(int j = 0; j < n; j++)
        {
             
            // If encountered character
            // is *, increment length
            if (letter[j][i] == '*')
                len++;
            else
            {
                 
                // If its not possible
                // return false
                if (len != 0 && len < y)
                    return false;
 
                // Otherwise reset len
                else
                    len = 0;
            }
        }
    }
    return true;
}
 
// Driver code
public static void Main(string[] args)
{
    int n = 3, x = 2, y = 2;
    string[] letter = { "#***", "#***", "#***" };
    int m = letter.GetLength(0);
     
    /*
    Stamp Matrix:
    $$
    $$
    A 2*2 matrix ( x*y)
    */
    if (solution(n, m, x, y, letter))
        Console.WriteLine("Possible");
    else
        Console.WriteLine("Impossible");
 
    // 2nd case
    String[] letter2 = { "#***", "#*#*", "#***" };
 
    m = letter2.GetLength(0);
 
    if (solution(n, m, x, y, letter2))
        Console.WriteLine("Possible");
    else
        Console.WriteLine("Impossible");
}
}
 
// This code is contributed by ukasp


Javascript


输出
Impossible

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