📌  相关文章
📜  检查是否可以执行给定的网格划分

📅  最后修改于: 2021-04-24 21:29:37             🧑  作者: Mango

给定两个整数NM ,它们代表网格的大小。还给出了大小为P的整数数组arr [] ,该整数表示给定的网格被划分为P部分,每个部分都包含来自网格的arr [i]像元。任务是检查是否可以按给定的方式划分网格。
例子:

方法:为了使划分成为可能,所有部分的像元之和必须等于给定网格中像元的总数,即所有数组元素的总和必须等于N *M。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if it is possible to
// divide the grid satisfying the given conditions
bool isPossible(int arr[], int p, int n, int m)
{
 
    // To store the sum of all the
    // cells of the given parts
    int sum = 0;
    for (int i = 0; i < p; i++)
        sum += arr[i];
 
    // If the sum is equal to the total number
    // of cells in the given grid
    if (sum == (n * m))
        return true;
    return false;
}
 
// Driver code
int main()
{
    int n = 3, m = 4;
    int arr[] = { 6, 3, 2, 1 };
    int p = sizeof(arr) / sizeof(arr[0]);
 
    if (isPossible(arr, p, n, m))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
class GFG
{
 
// Function that returns true if it is possible to
// divide the grid satisfying the given conditions
static boolean isPossible(int arr[], int p,
                          int n, int m)
{
 
    // To store the sum of all the
    // cells of the given parts
    int sum = 0;
    for (int i = 0; i < p; i++)
        sum += arr[i];
 
    // If the sum is equal to the total number
    // of cells in the given grid
    if (sum == (n * m))
        return true;
    return false;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 3, m = 4;
    int arr[] = { 6, 3, 2, 1 };
    int p = arr.length;
 
    if (isPossible(arr, p, n, m))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
 
# Function that returns true if
# it is possible to divide the grid
# satisfying the given conditions
def isPossible(arr, p, n, m):
     
    # To store the sum of all the
    # cells of the given parts
    sum = 0;
    for i in range(p):
        sum += arr[i];
 
    # If the sum is equal to the total number
    # of cells in the given grid
    if (sum == (n * m)):
        return True;
    return False;
 
# Driver code
if __name__ == '__main__':
 
    n = 3;
    m = 4;
    arr = [6, 3, 2, 1];
    p = len(arr);
 
    if (isPossible(arr, p, n, m)):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by Rajput-Ji


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
// Function that returns true if it is possible to
// divide the grid satisfying the given conditions
static bool isPossible(int []arr, int p,
                       int n, int m)
{
 
    // To store the sum of all the
    // cells of the given parts
    int sum = 0;
    for (int i = 0; i < p; i++)
        sum += arr[i];
 
    // If the sum is equal to the total number
    // of cells in the given grid
    if (sum == (n * m))
        return true;
    return false;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 3, m = 4;
    int []arr = { 6, 3, 2, 1 };
    int p = arr.Length;
 
    if (isPossible(arr, p, n, m))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes