📜  检查拼图是否可以解决

📅  最后修改于: 2021-04-27 05:49:50             🧑  作者: Mango

给定一个特殊的拼图游戏,该拼图游戏由N行和M列组成,它们都是相同的。每一块都有三个制表符和一个空白。任务是检查拼图是否可解决,方法是将棋子放置在一个棋子的凸耳完全适合另一棋子的毛坯中的位置。

注意旋转和平移碎片可以解决难题。

例子:

方法:问题中的主要观察结果是:

  • 如果拼图只有一行或只有一列。然后可以通过在共享的那一侧本身上放置一个空白选项卡来解决这个难题。
  • 如果拼图有两行两列。然后,可通过将空白的Tabs放置在圆形链中来解决难题。
  • 否则,将无法解决难题。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the jigsaw
// Puzzle is solveable or not
void checkSolveable(int n, int m)
{
   
    // Base Case
    if (n == 1 or m == 1)
        cout << "YES";
   
    // By placing the blank tabs
    // as a chain
    else if (m == 2 and n == 2)
        cout << "YES";
    else
        cout << "NO";
}
  
// Driver Code
int main()
{
    int n = 1, m = 3;
   
    checkSolveable(n, m);
}
 
// This code is contributed by jana_sayantan


Java
// Java program for the above approach
import java.util.*;
  
class GFG{
  
// Function to check if the jigsaw
// Puzzle is solveable or not
static void checkSolveable(int n, int m)
{
    
    // Base Case
    if (n == 1 || m == 1)
       System.out.print("YES");
    
    // By placing the blank tabs
    // as a chain
    else if (m == 2 && n == 2)
        System.out.print("YES");
    else
        System.out.print("NO");
}
 
// Driver Code
public static void main(String[] args)
{
     int n = 1, m = 3;
    
    checkSolveable(n, m);
}
}
 
// This code is contributed by sanjoy_62


Python
# Python program for the above approach
 
# Function to check if the jigsaw
# Puzzle is solveable or not
def checkSolveable(n, m):
 
    # Base Case
    if n == 1 or m == 1:
        print("YES")
         
    # By placing the blank tabs
    # as a chain
    elif m == 2 and n == 2:
        print("YES")
    else:
        print("NO")
 
# Driver Code
if __name__ == "__main__":
    n = 1
    m = 3
    checkSolveable(n, m)


C#
// C# program for the above approach
using System;
   
class GFG{
   
// Function to check if the jigsaw
// Puzzle is solveable or not
static void checkSolveable(int n, int m)
{
     
    // Base Case
    if (n == 1 || m == 1)
       Console.WriteLine("YES");
     
    // By placing the blank tabs
    // as a chain
    else if (m == 2 && n == 2)
        Console.WriteLine("YES");
    else
        Console.WriteLine("NO");
}
  
// Driver Code
public static void Main()
{
    int n = 1, m = 3;
   
    checkSolveable(n, m);
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
YES

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