📌  相关文章
📜  检查是否有任何正方形(带有一个彩色单元格)可以分成两个相等的部分

📅  最后修改于: 2021-10-23 08:04:33             🧑  作者: Mango

给定一个大小为n的正方形。在每个大小为 1 个单位的正方形n内有n 个2个小正方形,其中任何一个正方形都有颜色。我们的任务是将正方形n切成相等的两部分。切割线不应与彩色单元格有任何公共点,并且生成的两个部分应等于旋转。如果在这样的条件下可以切割正方形,则打印“是”,否则打印“否”。
注意: n的值应始终为偶数正数。

例子:

Input : n = 4, x = 1, y = 1
Output : YES
// n = 4 and 1 1 is the coordinate of the colored square

Input :  n = 2, x = 1, y = 1 
Output : NO 

在第一个示例中,绘制的正方形的坐标为 1 x 1。因此我们必须将较大的正方形切成两部分,以便与彩色单元格不存在任何公共点。上图中的粗线将正方形切成相等的两部分。
下面是解决这个问题的分步算法:
1 .初始化正方形的大小和绘制正方形的位置。
2 .只有当切割线穿过我们更大的正方形的中心时,才能将一个正方形分成两个相等的部分。
3 .因此,如果绘制的正方形无论如何都连接到较大正方形的中心,则不可能将较大正方形切成两个相等的部分。
4 .因此,要进行检查,请将较大正方形的大小分成两半,然后检查已绘制正方形的任何维度是否与其相关联。

下面是上述方法的实现:

C++
// C++ program to illustrate
// the above problem
 
#include 
using namespace std;
 
// function to check if it's possible to
// divide the square in two equal parts
void halfsquare(int n, int x, int y)
{
    int half = n / 2;
 
    // if the painted square is linked anyway
    // to the center of the square
    // then it's not possible
    if ((half == x || half == x - 1) &&
        (half == y || half == y - 1))
        cout << "NO" << endl;
 
    // else yes it's possible
    else
        cout << "YES" << endl;
}
 
// Driver code
int main()
{
    // initialize the size of the square
    int n = 100;
 
    // initialize the dimension of the painted square
    int x = 51, y = 100;
 
    halfsquare(n, x, y);
    return 0;
}


Java
// Java program to illustrate
// the above problem
 
import java.io.*;
 
class GFG {
  
// function to check if it's possible to
// divide the square in two equal parts
static void halfsquare(int n, int x, int y)
{
    int half = n / 2;
 
    // if the painted square is linked anyway
    // to the center of the square
    // then it's not possible
    if ((half == x || half == x - 1) &&
        (half == y || half == y - 1))
        System.out.println( "NO");
 
    // else yes it's possible
    else
        System.out.println( "YES");
}
 
// Driver code
 
    public static void main (String[] args) {
            // initialize the size of the square
    int n = 100;
 
    // initialize the dimension of the painted square
    int x = 51, y = 100;
 
    halfsquare(n, x, y);
    }
}
// This code is contributed
// by inder_verma..


Python 3
# Python 3 program to illustrate
# the above problem
 
# function to check if it's possible to
# divide the square in two equal parts
def halfsquare(n, x, y) :
    half = n // 2
 
    # if the painted square is
    # linked anyway to the center
    # of the square then it's
    # not possible
    if ((half == x or half == x - 1) and
        (half == y or half == y - 1)) :
        print("NO")
 
    # else yes it's possible
    else :
        print("YES")
         
# Driver code    
if __name__ == "__main__" :
 
    # initialize the size of the square
    n = 100
 
    # initialize the dimension
    # of the painted square
    x, y = 51, 100
 
    halfsquare(n, x, y)
 
# This code is contributed by ANKITRAI1


C#
// C# program to illustrate
// the above problem
using System;
 
class GFG {
 
// function to check if it's possible to
// divide the square in two equal parts
static void halfsquare(int n, int x, int y)
{
    int half = n / 2;
 
    // if the painted square is linked anyway
    // to the center of the square
    // then it's not possible
    if ((half == x || half == x - 1) &&
        (half == y || half == y - 1))
        Console.WriteLine( "NO");
 
    // else yes it's possible
    else
        Console.WriteLine( "YES");
}
 
// Driver code
 
    public static void Main () {
            // initialize the size of the square
    int n = 100;
 
    // initialize the dimension of the painted square
    int x = 51, y = 100;
 
    halfsquare(n, x, y);
    }
}
// This code is contributed
// by  anuj_67..


PHP


Javascript


输出:

YES