📌  相关文章
📜  检查相邻交换后网格是否可以按行和按列排序

📅  最后修改于: 2021-04-26 18:51:07             🧑  作者: Mango

给定一个大小为nx len的网格,并用小写字符填充。我们可以在同一行和同一列中交换两个相邻的字符。现在,我们必须检查是否可以按照字典顺序对每一行和每一列进行排序的顺序进行排列。

例子:

Input : abcde
        fghij
        olmkn
        trpqs
        xywuv
Output : Yes
Explanation :
The grid can be rearranged as
abcde
fghij
klmno
pqrst
uvwxy

解决上述问题的想法非常简单,我们可以简单地将同一行中的字符排序,然后
检查列虎钳是否将新网格排序为虎钳。请不要对相邻的交换进行排序(例如,气泡排序仅对相邻的交换进行排序)

下面给出上述想法的实现。

C++
// C++ program to check if we can make a
// grid of character sorted using adjacent
// swaps.
#include 
using namespace std;
  
// v[] is vector of strings. len is length
// of strings in every row.
bool check(vector v, int len)
{
    int n = v.size();
    for (int i = 0; i < n; i++) 
        sort(v[i].begin(), v[i].end());
      
    for (int i = 0; i < len-1; i++) 
        for (int j = 0; j < n; j++) 
            if (v[i][j] > v[i+1][j])
                return false;
    return true;
}
  
// Driver code
int main()
{
    vector v = { "ebcda", "ihgfj", "klmno", 
                               "pqrst", "yvwxu" };
    int len = 5; // Length of strings
    check(v, len)? cout << "Yes" : cout << "No";
    return 0;
}


Java
// Java program to check if we can make a grid
// of character sorted using adjacent swaps.
import java.util.Arrays;
  
class GfG
{
  
    // v[] is vector of strings. len is
    // length of strings in every row.
    static boolean check(String[] v, int len)
    {
        int n = v.length;
        char[] tempArray;
          
        for (int i = 0; i < n; i++)
        {
            tempArray = v[i].toCharArray();
            Arrays.sort(tempArray);
            v[i] = new String(tempArray);
        }
          
        for (int i = 0; i < len-1; i++) 
            for (int j = 0; j < n; j++) 
                if (v[i].charAt(j) > v[i+1].charAt(j))
                    return false;
        return true;
    }
  
    // Driver code
    public static void main(String []args)
    {
          
        String[] v = { "ebcda", "ihgfj", "klmno", 
                            "pqrst", "yvwxu" };
          
        int len = 5; // Length of strings
        if (check(v, len))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
  
// This code is contributed by Rituraj Jain


Python
# Python program to check if we can make a
# grid of character sorted using adjacent
# swaps.
  
# v[] is vector of strings. len is length
# of strings in every row.
def check(v, l):
    n = len(v)
    for i in v:
        i = ''.join(sorted(i))
       
    for i in range(l - 1):
        for j in range(n):
            if (v[i][j] > v[i + 1][j]):
                return False
    return True
   
# Driver code
v = [ "ebcda", "ihgfj", "klmno", "pqrst", "yvwxu" ]
l = 5 # Length of strings
if check(v, l):
    print "Yes"
else:
    print "No"
  
# This code is contributed by Sachin Bisht


C#
// C# program to check if we can make a grid 
// of character sorted using adjacent swaps.
using System;
  
class GfG 
{ 
  
    // v[] is vector of strings. len is 
    // length of strings in every row. 
    static Boolean check(String[] v, int len) 
    { 
        int n = v.Length; 
        char[] tempArray; 
          
        for (int i = 0; i < n; i++) 
        { 
            tempArray = v[i].ToCharArray(); 
            Array.Sort(tempArray); 
            v[i] = new String(tempArray); 
        } 
          
        for (int i = 0; i < len-1; i++) 
            for (int j = 0; j < n; j++) 
                if (v[i][j] > v[i+1][j]) 
                    return false; 
        return true; 
    } 
  
    // Driver code 
    public static void Main(String []args) 
    { 
          
        String[] v = { "ebcda", "ihgfj", "klmno", 
                            "pqrst", "yvwxu" }; 
          
        int len = 5; // Length of strings 
        if (check(v, len)) 
            Console.WriteLine("Yes"); 
        else
            Console.WriteLine("No"); 
    } 
} 
  
// This code has been contributed by 29AjayKumar


输出:

Yes