// C# program for the above approach
using System;
class GFG{
// Function to create the possible grids
public static void createGrid(char[,] grid,
bool is1,
int N, int M)
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
if (is1)
{
grid[i, j] = '0';
is1 = false;
}
else
{
grid[i, j] = '1';
is1 = true;
}
}
if (M % 2 == 0)
is1 = !is1;
}
}
// Function to test if any one of them
// matches with the given 2-D array
public static bool testGrid(char[,] testGrid,
char[,] Grid, int N,
int M)
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
if (Grid[i, j] != '*')
{
if (Grid[i, j] != testGrid[i, j])
{
return false;
}
}
}
}
return true;
}
// Function to print the grid, if possible
public static void printGrid(char[,] grid, int N,
int M)
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
Console.Write(grid[i, j] + " ");
}
Console.WriteLine();
}
}
// Function to check if the grid
// can be made alternating or not
public static void findPossibleGrid(int N, int M,
char[,] grid)
{
// Grids to store the possible grids
char[,] gridTest1 = new char[N, 1001];
char[,] gridTest2 = new char[N, 1001];
createGrid(gridTest1, true, N, M);
createGrid(gridTest2, false, N, M);
if (testGrid(gridTest1, grid, N, M))
{
Console.WriteLine("Yes");
printGrid(gridTest1, N, M);
}
else if (testGrid(gridTest2, grid, N, M))
{
Console.WriteLine("Yes");
printGrid(gridTest2, N, M);
}
else
{
Console.WriteLine("No");
}
}
// Driver code
public static void Main()
{
int N = 4, M = 4;
char[,] grid = { { '*', '*', '1', '0' },
{ '*', '*', '*', '*' },
{ '*', '*', '*', '*' },
{ '*', '*', '0', '1' } };
findPossibleGrid(N, M, grid);
}
}
// This code is contributed by sanjoy_62