# Find a way to fill matrix with 1’s and 0’s in blank positions

Given an N * M matrix mat[][] which consists of two types of characters ‘.’ and ‘_’. The task is to fill the matrix in positions where it contains ‘.’ with 1‘s and 0‘s. Fill the matrix in such a way that no two adjacent cells contain the same number and print the modified matrix.

Examples:

Input: mat[][] = {{‘.’, ‘_’}, {‘_’, ‘.’}}
Output:
1 _
_ 1

Input: mat[][] = {{‘_’, ‘_’}, {‘_’, ‘_’}}
Output:
_ _
_ _
There is no place to fill the numbers.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: An efficient approach is to fill the matrix in the following pattern:

10101010…
01010101…
10101010…

skipping ‘_’ characters whenever encountered.

Below is the implementation of the above approach:

## 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 `

Output:

```1 _
_ 1
``` My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : vt_m, 29AjayKumar, AnkitRai01