📜  TCS CodeVita 9(第 1 区)2020 |星座

📅  最后修改于: 2022-05-13 01:57:21.473000             🧑  作者: Mango

TCS CodeVita 9(第 1 区)2020 |星座

三个字符{#, *, . }代表空间中的恒星和星系星座。每个星系都由# 个字符划定。在一个给定的星系中可以有一颗或多颗恒星。星形只能是元音{A, E, I, O, U} 。元音形状的*集合是一个星形。一颗星星包含在一个 3×3 的块中。星星不能重叠。点(.)字符表示空格。

给定一个由{#, *, .}字符组成的维度为 3×N 的矩阵mat[][] ,任务是找到星系和其中的恒星。

例子:

方法:这个想法是使用范围[0, N – 1] 中的变量i逐列遍历矩阵并检查给定的 {#, *, . } 形成一个星系、一个空的空间或一个元音。出现以下情况:

  • 当在给定列中遇到所有'#'时:在这种情况下,打印'#'并继续遍历矩阵。
  • 当所有'.'在给定的列中遇到:在这种情况下,跳过当前列并继续遍历矩阵。
  • 对于所有其他情况,检查给定的{#, *, .}排列是否形成元音,然后打印元音并将列索引i更新为(i + 3)

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the vowels(stars)
// in order of their occurrence within
// the given galaxy
void printGalaxy(
    vector > mat, int n)
{
 
    // Iterate the matrix column-wise
    for (int i = 0; i < n; i++) {
 
        // If all '#' is encountered
        // in the given column, print '#'
        if (mat[0][i] == '#'
            && mat[1][i] == '#'
            && mat[2][i] == '#') {
            cout << '#';
        }
 
        // If all '.' is encountered in
        // the given column, skip the
        // current column
        else if (mat[0][i] == '.'
                 && mat[1][i] == '.'
                 && mat[2][i] == '.') {
        }
 
        // If above cases fail
        else {
 
            char a, b, c, a1, b1;
            char c1, a2, b2, c2;
            int x1 = i;
            a = mat[0][x1];
            b = mat[0][x1 + 1];
            c = mat[0][x1 + 2];
            a1 = mat[1][x1];
            b1 = mat[1][x1 + 1];
            c1 = mat[1][x1 + 2];
            a2 = mat[2][x1];
            b2 = mat[2][x1 + 1];
            c2 = mat[2][x1 + 2];
 
            // Check if the arrangement
            // of '.' and '*' forms
            // character 'A'
            if (a == '.' && b == '*'
                && c == '.' && a1 == '*'
                && b1 == '*' && c1 == '*'
                && a2 == '*' && b2 == '.'
                && c2 == '*') {
 
                // If true, print A
                cout << "A";
 
                // Increment column number
                i = i + 2;
            }
 
            // Check if the arrangement
            // of '.' and '*' forms
            // character 'E'
            if (a == '*' && b == '*'
                && c == '*' && a1 == '*'
                && b1 == '*' && c1 == '*'
                && a2 == '*' && b2 == '*'
                && c2 == '*') {
 
                // If true, print E
                cout << "E";
 
                // Increment column number
                i = i + 2;
            }
 
            // Check if the arrangement
            // of '.' and '*' forms
            // character 'I'
            if (a == '*' && b == '*'
                && c == '*' && a1 == '.'
                && b1 == '*' && c1 == '.'
                && a2 == '*' && b2 == '*'
                && c2 == '*') {
 
                // If true, print I
                cout << "I";
 
                // Increment column number
                i = i + 2;
            }
 
            // Check if the arrangement
            // of '.' and '*' forms
            // character 'O'
            if (a == '*' && b == '*'
                && c == '*' && a1 == '*'
                && b1 == '.' && c1 == '*'
                && a2 == '*' && b2 == '*'
                && c2 == '*') {
 
                // If true, print O
                cout << "O";
 
                // Increment column number
                i = i + 2;
            }
 
            // Check if the arrangement
            // of '.' and '*' forms
            // character 'U'
            if (a == '*' && b == '.'
                && c == '*' && a1 == '*'
                && b1 == '.' && c1 == '*'
                && a2 == '*' && b2 == '*'
                && c2 == '*') {
 
                // If True, print U
                cout << "U";
 
                // Increment column number
                i = i + 2;
            }
        }
    }
}
 
// Driver Code
int main()
{
    // Given n, number of columns
    int N = 18;
 
    // Given matrix
    vector > mat
        = { { '*', '.', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '.', '*', '.' },
            { '*', '.', '*', '#', '*', '.', '*', '#', '.',
              '*', '.', '#', '*', '*', '*', '*', '*', '*' },
            { '*', '*', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '*', '.',
              '*' } };
 
    // Function Call
    printGalaxy(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG
{
 
    // Function to print the vowels(stars)
    // in order of their occurrence within
    // the given galaxy
    static void printGalaxy(char mat[][], int n)
    {
 
        // Iterate the matrix column-wise
        for (int i = 0; i < n; i++)
        {
 
            // If all '#' is encountered
            // in the given column, print '#'
            if (mat[0][i] == '#' && mat[1][i] == '#'
                && mat[2][i] == '#') {
                System.out.print('#');
            }
 
            // If all '.' is encountered in
            // the given column, skip the
            // current column
            else if (mat[0][i] == '.' && mat[1][i] == '.'
                     && mat[2][i] == '.') {
            }
 
            // If above cases fail
            else {
 
                char a, b, c, a1, b1;
                char c1, a2, b2, c2;
                int x1 = i;
                a = mat[0][x1];
                b = mat[0][x1 + 1];
                c = mat[0][x1 + 2];
                a1 = mat[1][x1];
                b1 = mat[1][x1 + 1];
                c1 = mat[1][x1 + 2];
                a2 = mat[2][x1];
                b2 = mat[2][x1 + 1];
                c2 = mat[2][x1 + 2];
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'A'
                if (a == '.' && b == '*' && c == '.'
                    && a1 == '*' && b1 == '*' && c1 == '*'
                    && a2 == '*' && b2 == '.'
                    && c2 == '*') {
 
                    // If true, print A
                    System.out.print("A");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'E'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '*' && b1 == '*' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print E
                    System.out.print("E");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'I'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '.' && b1 == '*' && c1 == '.'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print I
                    System.out.print("I");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'O'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '*' && b1 == '.' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print O
                    System.out.print("O");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'U'
                if (a == '*' && b == '.' && c == '*'
                    && a1 == '*' && b1 == '.' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If True, print U
                    System.out.print("U");
 
                    // Increment column number
                    i = i + 2;
                }
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given n, number of columns
        int N = 18;
 
        // Given matrix
        char mat[][] = {
            { '*', '.', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '.', '*', '.' },
            { '*', '.', '*', '#', '*', '.', '*', '#', '.',
              '*', '.', '#', '*', '*', '*', '*', '*', '*' },
            { '*', '*', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '*', '.', '*' }
        };
 
        // Function Call
        printGalaxy(mat, N);
    }
}
 
// This code is contributed by Potta Lokesh


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
    // Function to print the vowels(stars)
    // in order of their occurrence within
    // the given galaxy
    static void printGalaxy(char [,]mat, int n)
    {
 
        // Iterate the matrix column-wise
        for (int i = 0; i < n; i++)
        {
 
            // If all '#' is encountered
            // in the given column, print '#'
            if (mat[0,i] == '#' && mat[1,i] == '#'
                && mat[2,i] == '#') {
                Console.Write('#');
            }
 
            // If all '.' is encountered in
            // the given column, skip the
            // current column
            else if (mat[0,i] == '.' && mat[1,i] == '.'
                     && mat[2,i] == '.') {
            }
 
            // If above cases fail
            else {
 
                char a, b, c, a1, b1;
                char c1, a2, b2, c2;
                int x1 = i;
                a = mat[0,x1];
                b = mat[0,x1 + 1];
                c = mat[0,x1 + 2];
                a1 = mat[1,x1];
                b1 = mat[1,x1 + 1];
                c1 = mat[1,x1 + 2];
                a2 = mat[2,x1];
                b2 = mat[2,x1 + 1];
                c2 = mat[2,x1 + 2];
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'A'
                if (a == '.' && b == '*' && c == '.'
                    && a1 == '*' && b1 == '*' && c1 == '*'
                    && a2 == '*' && b2 == '.'
                    && c2 == '*') {
 
                    // If true, print A
                    Console.Write("A");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'E'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '*' && b1 == '*' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print E
                    Console.Write("E");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'I'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '.' && b1 == '*' && c1 == '.'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print I
                    Console.Write("I");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'O'
                if (a == '*' && b == '*' && c == '*'
                    && a1 == '*' && b1 == '.' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If true, print O
                    Console.Write("O");
 
                    // Increment column number
                    i = i + 2;
                }
 
                // Check if the arrangement
                // of '.' and '*' forms
                // character 'U'
                if (a == '*' && b == '.' && c == '*'
                    && a1 == '*' && b1 == '.' && c1 == '*'
                    && a2 == '*' && b2 == '*'
                    && c2 == '*') {
 
                    // If True, print U
                    Console.Write("U");
 
                    // Increment column number
                    i = i + 2;
                }
            }
        }
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
       
        // Given n, number of columns
        int N = 18;
 
        // Given matrix
        char [,]mat = {
            { '*', '.', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '.', '*', '.' },
            { '*', '.', '*', '#', '*', '.', '*', '#', '.',
              '*', '.', '#', '*', '*', '*', '*', '*', '*' },
            { '*', '*', '*', '#', '*', '*', '*', '#', '*',
              '*', '*', '#', '*', '*', '*', '*', '.', '*' }
        };
 
        // Function Call
        printGalaxy(mat, N);
    }
}
 
// This code is contributed by Amit Katiyar


Javascript


输出:
U#O#I#EA

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程