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

📅  最后修改于: 2021-06-25 18:36:30             🧑  作者: 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 dimention 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 dimention 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 dimention of the painted square
    int x = 51, y = 100;
  
    halfsquare(n, x, y);
    }
}
// This code is contributed 
// by  anuj_67..


PHP


输出 :

YES

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。