📌  相关文章
📜  检查是否可以通过相邻移动对矩阵进行混洗

📅  最后修改于: 2021-05-31 22:26:28             🧑  作者: Mango

给定一个N x M矩阵,分别表示行数和列数。矩阵的每个单元格仅由一个学生占用。任务是确定我们是否可以按以下方式对每个学生进行洗牌,即每个学生应该占用与该学生原始单元格相邻的一个单元格,即紧靠该单元格的左侧,右侧,顶部或底部,并且在对每个单元格进行改组后应该只由一个学生占据。
例子:

方法:检查行数或列数是否均匀,然后可以进行改组,否则不能进行改组。
下面是上述方法的实现:

C++
// C++ implementation of above approach
#include 
using namespace std;
 
// Function that will check whether
// Shuffling is possible or not
void Shuffling(int N, int M)
{
    if (N % 2 == 0 || M % 2 == 0)
        cout << "Shuffling is possible";
    else
        cout << "Shuffling not possible";
}
 
// Driver code
int main()
{
    int N = 4, M = 5;
 
    // Calling function.
    Shuffling(N, M);
    return 0;
}
Java // Java implementation of above approach
import java.io.*;

class GFG 
{
    
// Function that will check whether
// Shuffling is possible or not
static void Shuffling(int N, int M)
{
    if (N % 2 == 0 || M % 2 == 0)
        System.out.println("Shuffling is possible");
    else
        System.out.println("Shuffling not possible");
}

    // Driver code
    public static void main (String[] args) 
    {
        int N = 4, M = 5;

    // Calling function.
    Shuffling(N, M);
    }
}

// This code is contributed by anuj_67..


Python3
# Python3 implementation of above approach
 
# Function that will check whether
# Shuffling is possible or not
def Shuffling(N, M) :
     
    if (N % 2 == 0 or M % 2 == 0) :
        print("Shuffling is possible");
    else :
        print("Shuffling not possible");
 
# Driver Code
if __name__ == "__main__" :
 
    # Driver code
    N = 4;
    M = 5;
 
    # Calling function.
    Shuffling(N, M);
     
# This code is contributed by Ryuga


C#
// C# implementation of above approach
using System;
 
class GFG
{
     
// Function that will check whether
// Shuffling is possible or not
static void Shuffling(int N, int M)
{
    if (N % 2 == 0 || M % 2 == 0)
        Console.Write("Shuffling is possible");
    else
        Console.Write("Shuffling not possible");
}
 
    // Driver code
    public static void Main ()
    {
        int N = 4, M = 5;
 
        // Calling function.
        Shuffling(N, M);
    }
}
 
// This code is contributed by Ita_c.


PHP


Javascript


输出:
Shuffling is possible
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”