Cree una array con rectángulos alternos de O y X

Escriba un código que ingrese dos números m y n y cree una array de tamaño mxn (m filas y n columnas) en la que cada elemento sea X o 0. Las X y los 0 deben completarse alternativamente, la array debe tener un rectángulo exterior de Xs, luego un rectángulo de 0s, luego un rectángulo de Xs, y así sucesivamente.

Ejemplos:  

Input: m = 3, n = 3
Output: Following matrix 
X X X
X 0 X
X X X

Input: m = 4, n = 5
Output: Following matrix
X X X X X
X 0 0 0 X
X 0 0 0 X
X X X X X

Input:  m = 5, n = 5
Output: Following matrix
X X X X X
X 0 0 0 X
X 0 X 0 X
X 0 0 0 X
X X X X X

Input:  m = 6, n = 7
Output: Following matrix
X X X X X X X
X 0 0 0 0 0 X
X 0 X X X 0 X
X 0 X X X 0 X
X 0 0 0 0 0 X
X X X X X X X 

Recomendamos encarecidamente minimizar el navegador y probarlo usted mismo primero.

Esta pregunta se hizo en el reclutamiento del campus de Shreepartners Gurgaon. Seguí el siguiente enfoque.

  1. Use el código para Imprimir Array en forma de Espiral
  2. En lugar de imprimir la array, inserte el elemento ‘X’ o ‘0’ alternativamente en la array.

A continuación se muestra la implementación del enfoque anterior.  

C++

#include <bits/stdc++.h>
using namespace std;
 
// Function to print alternating rectangles of 0 and X
void fill0X(int m, int n)
{
    /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator    */
    int i, k = 0, l = 0;
 
    // Store given number of rows and columns for later use
    int r = m, c = n;
 
    // A 2D array to store the output to be printed
    char a[m][n];
    char x = 'X'; // Initialize the character to be stored in a[][]
 
    // Fill characters in a[][] in spiral form. Every iteration fills
    // one rectangle of either Xs or Os
    while (k < m && l < n)
    {
        /* Fill the first row from the remaining rows */
        for (i = l; i < n; ++i)
            a[k][i] = x;
        k++;
 
        /* Fill the last column from the remaining columns */
        for (i = k; i < m; ++i)
            a[i][n-1] = x;
        n--;
 
        /* Fill the last row from the remaining rows */
        if (k < m)
        {
            for (i = n-1; i >= l; --i)
                a[m-1][i] = x;
            m--;
        }
 
        /* Print the first column from the remaining columns */
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
                a[i][l] = x;
            l++;
        }
 
        // Flip character for next iteration
        x = (x == '0')? 'X': '0';
    }
 
    // Print the filled matrix
    for (i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
            cout <<" "<< a[i][j];
        cout <<"\n";
    }
}
 
/* Driver program to test above functions */
int main()
{
    puts("Output for m = 5, n = 6");
    fill0X(5, 6);
 
    puts("\nOutput for m = 4, n = 4");
    fill0X(4, 4);
 
    puts("\nOutput for m = 3, n = 4");
    fill0X(3, 4);
 
    return 0;
}
 
// This code is contributed by shivanisinghss2110

C

#include <stdio.h>
 
// Function to print alternating rectangles of 0 and X
void fill0X(int m, int n)
{
    /*  k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator    */
    int i, k = 0, l = 0;
 
    // Store given number of rows and columns for later use
    int r = m, c = n;
 
    // A 2D array to store the output to be printed
    char a[m][n];
    char x = 'X'; // Initialize the character to be stored in a[][]
 
    // Fill characters in a[][] in spiral form. Every iteration fills
    // one rectangle of either Xs or Os
    while (k < m && l < n)
    {
        /* Fill the first row from the remaining rows */
        for (i = l; i < n; ++i)
            a[k][i] = x;
        k++;
 
        /* Fill the last column from the remaining columns */
        for (i = k; i < m; ++i)
            a[i][n-1] = x;
        n--;
 
        /* Fill the last row from the remaining rows */
        if (k < m)
        {
            for (i = n-1; i >= l; --i)
                a[m-1][i] = x;
            m--;
        }
 
        /* Print the first column from the remaining columns */
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
                a[i][l] = x;
            l++;
        }
 
        // Flip character for next iteration
        x = (x == '0')? 'X': '0';
    }
 
    // Print the filled matrix
    for (i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
            printf("%c ", a[i][j]);
        printf("\n");
    }
}
 
/* Driver program to test above functions */
int main()
{
    puts("Output for m = 5, n = 6");
    fill0X(5, 6);
 
    puts("\nOutput for m = 4, n = 4");
    fill0X(4, 4);
 
    puts("\nOutput for m = 3, n = 4");
    fill0X(3, 4);
 
    return 0;
}

Java

// Java code to demonstrate the working.
 
import java.io.*;
 
class GFG {
 
// Function to print alternating
// rectangles of 0 and X
 static void fill0X(int m, int n)
{
    /* k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator */
    int i, k = 0, l = 0;
 
    // Store given number of rows
        // and columns for later use
    int r = m, c = n;
 
    // A 2D array to store
        // the output to be printed
    char a[][] = new char[m][n];
 
        // Initialize the character
        // to be stored in a[][]
    char x = 'X';
 
    // Fill characters in a[][] in spiral
        // form. Every iteration fills
    // one rectangle of either Xs or Os
    while (k < m && l < n)
    {
        /* Fill the first row from the remaining rows */
        for (i = l; i < n; ++i)
            a[k][i] = x;
        k++;
 
        /* Fill the last column from the remaining columns */
        for (i = k; i < m; ++i)
            a[i][n-1] = x;
        n--;
 
        /* Fill the last row from the remaining rows */
        if (k < m)
        {
            for (i = n-1; i >= l; --i)
                a[m-1][i] = x;
            m--;
        }
 
        /* Print the first column
                // from the remaining columns */
        if (l < n)
        {
            for (i = m-1; i >= k; --i)
                a[i][l] = x;
            l++;
        }
 
        // Flip character for next iteration
        x = (x == '0')? 'X': '0';
    }
 
    // Print the filled matrix
    for (i = 0; i < r; i++)
    {
        for (int j = 0; j < c; j++)
            System.out.print(a[i][j] + " ");
        System.out.println();
    }
}
 
/* Driver program to test above functions */
public static void main (String[] args) {
 
    System.out.println("Output for m = 5, n = 6");
    fill0X(5, 6);
 
    System.out.println("Output for m = 4, n = 4");
    fill0X(4, 4);
 
    System.out.println("Output for m = 3, n = 4");
    fill0X(3, 4);
         
    }
}
 
// This code  is contributed by vt_m

Python3

# Python3 program to Create a matrix with
# alternating rectangles of O and X
 
# Function to print alternating rectangles
# of 0 and X
def fill0X(m, n):
     
    # k - starting row index
    # m - ending row index
    # l - starting column index
    # n - ending column index
    # i - iterator
    i, k, l = 0, 0, 0
 
    # Store given number of rows and
    # columns for later use
    r = m
    c = n
 
    # A 2D array to store the output
    # to be printed
    a = [[None] * n for i in range(m)]
    x = 'X' # Initialize the character to
            # be stored in a[][]
 
    # Fill characters in a[][] in spiral form.
    # Every iteration fills one rectangle of
    # either Xs or Os
    while k < m and l < n:
         
        # Fill the first row from the
        # remaining rows
        for i in range(l, n):
            a[k][i] = x
        k += 1
 
        # Fill the last column from
        # the remaining columns
        for i in range(k, m):
            a[i][n - 1] = x
        n -= 1
 
        # Fill the last row from the
        # remaining rows
        if k < m:
            for i in range(n - 1, l - 1, -1):
                a[m - 1][i] = x
            m -= 1
 
        # Print the first column from
        # the remaining columns
        if l < n:
            for i in range(m - 1, k - 1, -1):
                a[i][l] = x
            l += 1
 
        # Flip character for next iteration
        x = 'X' if x == '0' else '0'
 
    # Print the filled matrix
    for i in range(r):
        for j in range(c):
            print(a[i][j], end = " ")
        print()
 
# Driver Code
if __name__ == '__main__':
     
    print("Output for m = 5, n = 6")
    fill0X(5, 6)
 
    print("Output for m = 4, n = 4")
    fill0X(4, 4)
 
    print("Output for m = 3, n = 4")
    fill0X(3, 4)
     
# This code is contributed by pranchalK

C#

// C# code to demonstrate the working.
using System;
 
class GFG {
 
    // Function to print alternating
    // rectangles of 0 and X
    static void fill0X(int m, int n)
    {
         
        /* k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator */
        int i, k = 0, l = 0;
     
        // Store given number of rows
        // and columns for later use
        int r = m, c = n;
     
        // A 2D array to store
        // the output to be printed
        char [,]a = new char[m,n];
     
        // Initialize the character
        // to be stored in a[][]
        char x = 'X';
     
        // Fill characters in a[][] in spiral
        // form. Every iteration fills
        // one rectangle of either Xs or Os
        while (k < m && l < n)
        {
             
            /* Fill the first row from the
            remaining rows */
            for (i = l; i < n; ++i)
                a[k,i] = x;
            k++;
     
            /* Fill the last column from the
            remaining columns */
            for (i = k; i < m; ++i)
                a[i,n-1] = x;
            n--;
     
            /* Fill the last row from the
            remaining rows */
            if (k < m)
            {
                for (i = n-1; i >= l; --i)
                    a[m-1,i] = x;
                m--;
            }
     
            /* Print the first column
            from the remaining columns */
            if (l < n)
            {
                for (i = m-1; i >= k; --i)
                    a[i,l] = x;
                l++;
            }
     
            // Flip character for next
            // iteration
            x = (x == '0')? 'X': '0';
        }
     
        // Print the filled matrix
        for (i = 0; i < r; i++)
        {
            for (int j = 0; j < c; j++)
                Console.Write(a[i,j] + " ");
            Console.WriteLine();
        }
    }
     
    /* Driver program to test
    above functions */
    public static void Main ()
    {
        Console.WriteLine("Output for"
                    + " m = 5, n = 6");
        fill0X(5, 6);
     
        Console.WriteLine("Output for"
                    + " m = 4, n = 4");
        fill0X(4, 4);
     
        Console.WriteLine("Output for"
                    + " m = 3, n = 4");
        fill0X(3, 4);
    }
}
 
// This code is contributed by Sam007.

PHP

<?php
// PHP program to Create a matrix with
// alternating rectangles of O and X
 
// Function to print alternating
// rectangles of 0 and X
function fill0X($m, $n)
{
     
    /* k - starting row index
        m - ending row index
        l - starting column index
        n - ending column index
        i - iterator */
    $k = 0;
    $l = 0;
 
    // Store given number of rows
    // and columns for later use
    $r = $m;
    $c = $n;
 
    // A 2D array to store the
    // output to be printed
    // Initialize the character
    // to be stored in a[][]
    $x = 'X';
 
    // Fill characters in a[][] in
    // spiral form. Every iteration fills
    // one rectangle of either Xs or Os
    while ($k < $m && $l < $n)
    {
         
        /* Fill the first row from
           the remaining rows */
        for ($i = $l; $i < $n; ++$i)
            $a[$k][$i] = $x;
        $k++;
 
        /* Fill the last column from
           the remaining columns */
        for ($i = $k; $i < $m; ++$i)
            $a[$i][$n - 1] = $x;
        $n--;
 
        /* Fill the last row from
           the remaining rows */
        if ($k < $m)
        {
            for ($i = $n - 1; $i >= $l; --$i)
                $a[$m - 1][$i] = $x;
            $m--;
        }
 
        /* Print the first column from
           the remaining columns */
        if ($l < $n)
        {
            for ($i = $m - 1; $i >= $k; --$i)
                $a[$i][$l] = $x;
            $l++;
        }
 
        // Flip character for
        // next iteration
        $x = ($x == '0')? 'X': '0';
    }
 
    // Print the filled matrix
    for ($i = 0; $i < $r; $i++)
    {
        for ($j = 0; $j < $c; $j++)
            echo($a[$i][$j]." ");
        echo "\n";
    }
}
 
// Driver Code
echo "Output for m = 5, n = 6\n";
fill0X(5, 6);
 
echo "\nOutput for m = 4, n = 4\n";
fill0X(4, 4);
 
echo "\nOutput for m = 3, n = 4\n";
fill0X(3, 4);
 
// This code is contributed by ChitraNayal.
?>

Javascript

<script>
 
// JavaScript code to demonstrate the working.
 
// Function to print alternating
// rectangles of 0 and X
function fill0X(m, n)
{
     
    /* k - starting row index
    m - ending row index
    l - starting column index
    n - ending column index
    i - iterator */
    let i, k = 0, l = 0;
     
    // Store given number of rows
    // and columns for later use
    let r = m, c = n;
     
    // A 2D array to store
    // the output to be printed
    let a = new Array(m);
    for(let i = 0; i < m; i++)
    {
        a[i] = new Array(n);
    }
     
    // Initialize the character
    // to be stored in a[][]
    let x = 'X';
     
    // Fill characters in a[][] in spiral
    // form. Every iteration fills
    // one rectangle of either Xs or Os
    while (k < m && l < n)
    {
         
        // Fill the first row from the
        // remaining rows
        for(i = l; i < n; ++i)
        {
            a[k][i] = x;
        }
        k++;
         
        // Fill the last column from the
        // remaining columns
        for(i = k; i < m; ++i)
        {
            a[i][n - 1] = x;
        }
        n--;
         
        // Fill the last row from the
        // remaining rows
        if (k < m)
        {
            for(i = n - 1; i >= l; --i)
                a[m - 1][i] = x;
             
            m--;
        }
         
        // Print the first column
        // from the remaining columns
        if (l < n)
        {
            for(i = m - 1; i >= k; --i)
            {
                a[i][l] = x;
            }
            l++;
        }
         
        // Flip character for next iteration
        x = (x == '0') ? 'X' : '0';
    }
     
    // Print the filled matrix
    for(i = 0; i < r; i++)
    {
        for(let j = 0; j < c; j++)
        {
            document.write(a[i][j] + " ");
        }
        document.write("<br>");
    }
}
 
// Driver Code
document.write("Output for m = 5, n = 6 <br>");
fill0X(5, 6);
 
document.write("Output for m = 4, n = 4<br>");
fill0X(4, 4);
 
document.write("Output for m = 3, n = 4<br>");
fill0X(3, 4);
 
// This code is contributed by rag2127
 
</script>
Producción

Output for m = 5, n = 6
 X X X X X X
 X 0 0 0 0 X
 X 0 X X 0 X
 X 0 0 0 0 X
 X X X X X X

Output for m = 4, n = 4
 X X X X
 X 0 0 X
 X 0 0 X
 X X X X

Output for m = 3, n = 4
 X X X X
 X 0 0 X
 X X X X

Tiempo Complejidad: O(mn) 
Espacio Auxiliar: O(mn)

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *