📜  找到一种在空白位置填充1和0的矩阵的方法

📅  最后修改于: 2021-06-26 13:24:09             🧑  作者: Mango

给定一个N * M矩阵mat [] [] ,它由两种类型的字符“。”组成‘_’ 。任务是在包含“。”的位置填充矩阵10 。填充矩阵,以使两个相邻的单元格都不包含相同的数字,并打印修改后的矩阵。
例子:

方法:一种有效的方法是按以下模式填充矩阵:

遇到任何情况都跳过“ _”字符。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define N 2
#define M 2
 
// Function to generate and
// print the required matrix
void Matrix(char a[N][M])
{
    char ch = '1';
 
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
 
            // Replace the '.'
            if (a[i][j] == '.')
                a[i][j] = ch;
 
            // Toggle number
            ch = (ch == '1') ? '0' : '1';
 
            cout << a[i][j] << " ";
        }
        cout << endl;
 
        // For each row, change
        // the starting number
        if (i % 2 == 0)
            ch = '0';
        else
            ch = '1';
    }
}
 
// Driver code
int main()
{
    char a[N][M] = { { '.', '_' },
                     { '_', '.' } };
 
    Matrix(a);
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
 
static int N = 2;
static int M = 2;
 
// Function to generate and
// print the required matrix
static void Matrix(char a[][])
{
    char ch = '1';
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
 
            // Replace the '.'
            if (a[i][j] == '.')
                a[i][j] = ch;
 
            // Toggle number
            ch = (ch == '1') ? '0' : '1';
 
            System.out.print( a[i][j] + " ");
        }
        System.out.println();
 
        // For each row, change
        // the starting number
        if (i % 2 == 0)
            ch = '0';
        else
            ch = '1';
    }
}
 
    // Driver code
    public static void main (String[] args)
    {
        char a[][] = { { '.', '_' },
                    { '_', '.' } };
 
        Matrix(a);
    }
}
 
// This code is contributed by anuj_67..


Python3
# Python3 implementation of the approach
 
N = 2
M = 2
 
# Function to generate and
# print the required matrix
def Matrix(a) :
    ch = '1';
 
    for i in range(N) :
        for j in range(M) :
 
            # Replace the '.'
            if (a[i][j] == '.') :
                a[i][j] = ch;
 
            # Toggle number
            if (ch == '1') :
                ch == '0'
            else :
                ch = '1'
 
            print(a[i][j],end = " ");
             
        print()
 
        # For each row, change
        # the starting number
        if (i % 2 == 0) :
            ch = '0';
        else :
            ch = '1';
 
# Driver code
if __name__ == "__main__" :
 
    a = [
            [ '.', '_' ],
            [ '_', '.' ],
        ]
 
    Matrix(a);
     
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
     
class GFG
{
 
static int N = 2;
static int M = 2;
 
// Function to generate and
// print the required matrix
static void Matrix(char [,]a)
{
    char ch = '1';
 
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < M; j++)
        {
 
            // Replace the '.'
            if (a[i,j] == '.')
                a[i,j] = ch;
 
            // Toggle number
            ch = (ch == '1') ? '0' : '1';
 
            Console.Write( a[i,j] + " ");
        }
        Console.WriteLine();
 
        // For each row, change
        // the starting number
        if (i % 2 == 0)
            ch = '0';
        else
            ch = '1';
    }
}
 
// Driver code
public static void Main (String[] args)
{
    char [,]a = { { '.', '_' },
                { '_', '.' } };
 
    Matrix(a);
}
}
 
// This code has been contributed by 29AjayKumar


Javascript


输出:
1 _ 
_ 1

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。