📌  相关文章
📜  检查二进制矩阵的元素是否可以交替

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

检查二进制矩阵的元素是否可以交替

给定一个大小为N * M的二维数组grid[][] ,由字符“1”、“0”“*”组成,其中“*”表示一个空白空间,可以用“1”替换或“0” 。任务是填充网格,使“0”“1”交替出现,并且没有两个连续的字符同时出现,即(101010)有效而(101101)无效,因为两个“1”同时出现。如果可以这样做,请打印Yes和可能的 2-D 数组。否则,打印No

例子:

方法:可能的二维数组只有两种可能性,一种是从1开始,一种是从0开始。生成它们并检查它们中的任何一个是否与给定的二维数组grid[][] 匹配。请按照以下步骤解决问题:

  • 定义一个函数createGrid(char grid[][1001], bool is1, int N, int M)并执行以下任务:
    • 使用变量i迭代范围[0, N]并执行以下任务:
      • 使用变量j迭代范围[0, M]并执行以下任务:
        • 如果is1true,则将grid[i][j]设置为'0'并将is1设置为false。
        • 否则,将grid[i][j]设置为'1'并将is1设置为true。
      • 如果M%2等于0,则将 is1 的值设置为is1的非
  • 定义一个函数testGrid(char testGrid[][1001], char Grid[][1001], int N, int M)并执行以下任务:
    • 使用变量i迭代范围[0, N]并执行以下任务:
      • 使用变量j迭代范围[0, M]并执行以下任务:
        • 如果Grid[i][j]不等于'*'并且testGrid[i][j]不等于Grid[i][j],则返回false。
    • 执行上述步骤后,返回true值作为答案。
  • 定义一个函数printGrid(char grid[][1001], int N, int M)并执行以下任务:
    • 使用变量i迭代范围[0, N]并执行以下任务:
      • 使用变量j迭代范围[0, M]并执行以下任务:
        • 打印grid[i][j] 的值。
  • 初始化两个二维数组gridTest1[N][1001]gridTest2[N][1001]以存储可能的交替网格。
  • 调用函数createGrid(gridTest1, true, N, M)createGrid(gridTest2, false, N, M)形成可能的交替网格。
  • 如果函数testGrid(gridTest1, grid, N, M)返回true,则调用函数printGrid(gridTest1, N, M)打印网格作为答案。
  • 否则,如果函数testGrid(gridTest2, grid, N, M)返回true,则调用函数printGrid(gridTest2, N, M)打印网格作为答案。
  • 否则,打印No作为答案。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to create the possible grids
void createGrid(char grid[][1001], 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
bool testGrid(char testGrid[][1001],
              char Grid[][1001],
              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
void printGrid(char grid[][1001], int N, int M)
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            cout << grid[i][j] << " ";
        }
        cout << endl;
    }
}
 
// Function to check if the grid
// can be made alternating or not
void findPossibleGrid(int N, int M,
                      char grid[][1001])
{
    // Grids to store the possible grids
    char gridTest1[N][1001], gridTest2[N][1001];
 
    createGrid(gridTest1, true, N, M);
 
    createGrid(gridTest2, false, N, M);
 
    if (testGrid(gridTest1, grid, N, M)) {
 
        cout << "Yes\n";
        printGrid(gridTest1, N, M);
    }
    else if (testGrid(gridTest2, grid, N, M)) {
 
        cout << "Yes\n";
        printGrid(gridTest2, N, M);
    }
    else {
        cout << "No\n";
    }
}
 
// Driver Code
int main()
{
    int N = 4, M = 4;
    char grid[][1001] = { { '*', '*', '1', '0' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '0', '1' } };
 
    findPossibleGrid(N, M, grid);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
 
class GFG{
     
// Function to create the possible grids
public static void createGrid(char[][] grid,
                              boolean 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 boolean 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++)
        {
            System.out.print(grid[i][j] + " ");
        }
        System.out.println();
    }
}
 
// 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))
    {
        System.out.println("Yes");
        printGrid(gridTest1, N, M);
    }
    else if (testGrid(gridTest2, grid, N, M))
    {
        System.out.println("Yes");
        printGrid(gridTest2, N, M);
    }
    else
    {
        System.out.println("No");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int N = 4, M = 4;
    char[][] grid = { { '*', '*', '1', '0' },
                      { '*', '*', '*', '*' },
                      { '*', '*', '*', '*' },
                      { '*', '*', '0', '1' } };
 
    findPossibleGrid(N, M, grid);
}
}
 
// This code is contributed by maddler


Python3
# python 3 program for the above approach
 
# Function to create the possible grids
def createGrid(grid, is1, N, M):
    for i in range(N):
        for j in range(M):
            if (is1):
                grid[i][j] = '0'
                is1 = False
 
            else:
                grid[i][j] = '1'
                is1 = True
 
        if (M % 2 == 0):
            is1 = True if is1 == False else False
 
# Function to test if any one of them
# matches with the given 2-D array
def testGrid(testGrid, Grid, N, M):
    for i in range(N):
        for j in range(M):
            if (Grid[i][j] != '*'):
                if (Grid[i][j] != testGrid[i][j]):
                    return False
 
    return True
 
# Function to print the grid, if possible
def printGrid(grid, N, M):
    for i in range(N):
        for j in range(M):
            print(grid[i][j],end = " ")
        print("\n",end = "")
 
# Function to check if the grid
# can be made alternating or not
def findPossibleGrid(N, M, grid):
    # Grids to store the possible grids
    gridTest1 = [['' for i in range(1001)] for j in range(N)]
    gridTest2 = [['' for i in range(1001)] for j in range(N)]
 
    createGrid(gridTest1, True, N, M)
 
    createGrid(gridTest2, False, N, M)
 
    if(testGrid(gridTest1, grid, N, M)):
        print("Yes")
        printGrid(gridTest1, N, M)
 
    elif(testGrid(gridTest2, grid, N, M)):
        print("Yes")
        printGrid(gridTest2, N, M)
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
    N = 4
    M = 4
    grid  = [['*', '*', '1', '0'],
             ['*', '*', '*', '*'],
             ['*', '*', '*', '*'],
             ['*', '*', '0', '1']]
 
    findPossibleGrid(N, M, grid)
     
    # This code is contributed by ipg2016107.


C#
// 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


Javascript


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check for the grid in
// one of the alternating ways
bool PosCheck(char a[][1001], int n,
              int m, char check)
{
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Iterate over the range
        for (int j = 0; j < m; j++) {
 
            if (a[i][j] == '*') {
                continue;
            }
            else {
 
                // (i+j)%2==1 cells should be with
                // the character check and the rest
                // should be with the other character
                if (((i + j) & 1) && a[i][j] != check) {
                    return false;
                }
                if (!((i + j) & 1) && a[i][j] == check) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
// Function to fill the grid in a possible way
void fill(char a[][1001], int n, int m,
          char odd, char even)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if ((i + j) & 1) {
                a[i][j] = odd;
            }
            else {
                a[i][j] = even;
            }
        }
    }
}
 
// Function to find if the grid can be made alternating
void findPossibleGrid(int n, int m, char a[][1001])
{
    bool flag = true;
    int k = -1, o = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
 
            if (a[i][j] != '*') {
 
                // If grid contains atleast
                // one 1 or 0
                k = i;
                o = j;
                break;
            }
        }
        if (k != -1) {
            break;
        }
    }
    if (k != -1) {
        flag = PosCheck(a, n, m, '1');
        if (flag) {
            fill(a, n, m, '1', '0');
        }
        else {
            flag = PosCheck(a, n, m, '0');
            if (flag) {
                fill(a, n, m, '0', '1');
            }
        }
    }
    else {
        // Fill the grid in any possible way
        char h = '1';
        for (int i = 0; i < m; i++) {
            a[0][i] = h;
            if (h == '1') {
                h = '0';
            }
            else {
                h = '1';
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i - 1 < 0) {
                    continue;
                }
                if (a[i - 1][j] == '1') {
                    a[i][j] = '0';
                }
                else {
                    a[i][j] = '1';
                }
            }
        }
        flag = true;
    }
 
    if (!flag) {
        cout << "NO\n";
    }
    else {
        cout << "YES\n";
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << a[i][j];
            }
            cout << endl;
        }
    }
}
 
// Driver Code
int main()
{
    int n = 4, m = 4;
    char grid[][1001] = { { '*', '*', '1', '0' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '0', '1' } };
 
    findPossibleGrid(n, m, grid);
 
    return 0;
}


Java
/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static boolean PosCheck(char[][] a, int n, int m,
                                   char check)
    {
 
        // Iterate over the range
        for (int i = 0; i < n; i++) {
 
            // Iterate over the range
            for (int j = 0; j < m; j++) {
 
                if (a[i][j] == '*') {
                    continue;
                }
                else {
 
                    // (i+j)%2==1 cells should be with
                    // the character check and the rest
                    // should be with the other character
                    if ((((i + j) & 1) == 1)
                        && a[i][j] != check) {
                        return false;
                    }
                    if (!(((i + j) & 1) == 1)
                        && a[i][j] == check) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    // Function to fill the grid in a possible way
    public static void fill(char[][] a, int n, int m,
                            char odd, char even)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (((i + j) & 1) == 1) {
                    a[i][j] = odd;
                }
                else {
                    a[i][j] = even;
                }
            }
        }
    }
 
    // Function to find if the grid can be made alternating
    public static void findPossibleGrid(int n, int m,
                                        char[][] a)
    {
        boolean flag = true;
        int k = -1, o = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                if (a[i][j] != '*') {
 
                    // If grid contains atleast
                    // one 1 or 0
                    k = i;
                    o = j;
                    break;
                }
            }
            if (k != -1) {
                break;
            }
        }
        if (k != -1) {
            flag = PosCheck(a, n, m, '1');
            if (flag) {
                fill(a, n, m, '1', '0');
            }
            else {
                flag = PosCheck(a, n, m, '0');
                if (flag) {
                    fill(a, n, m, '0', '1');
                }
            }
        }
        else {
            // Fill the grid in any possible way
            char h = '1';
            for (int i = 0; i < m; i++) {
                a[0][i] = h;
                if (h == '1') {
                    h = '0';
                }
                else {
                    h = '1';
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i - 1 < 0) {
                        continue;
                    }
                    if (a[i - 1][j] == '1') {
                        a[i][j] = '0';
                    }
                    else {
                        a[i][j] = '1';
                    }
                }
            }
            flag = true;
        }
 
        if (!flag) {
            System.out.println("No");
        }
        else {
            System.out.println("Yes");
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    System.out.print(a[i][j]);
                }
                System.out.println();
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4, m = 4;
        char[][] grid = { { '*', '*', '1', '0' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '0', '1' } };
 
        findPossibleGrid(n, m, grid);
    }
}
 
// This code is contributed by maddler.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
    public static bool PosCheck(char[,] a, int n, int m,
                                   char check)
    {
 
        // Iterate over the range
        for (int i = 0; i < n; i++) {
 
            // Iterate over the range
            for (int j = 0; j < m; j++) {
 
                if (a[i, j] == '*') {
                    continue;
                }
                else {
 
                    // (i+j)%2==1 cells should be with
                    // the character check and the rest
                    // should be with the other character
                    if ((((i + j) & 1) == 1)
                        && a[i, j] != check) {
                        return false;
                    }
                    if (!(((i + j) & 1) == 1)
                        && a[i, j] == check) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    // Function to fill the grid in a possible way
    public static void fill(char[,] a, int n, int m,
                            char odd, char even)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (((i + j) & 1) == 1) {
                    a[i, j] = odd;
                }
                else {
                    a[i, j] = even;
                }
            }
        }
    }
 
    // Function to find if the grid can be made alternating
    public static void findPossibleGrid(int n, int m,
                                        char[,] a)
    {
        bool flag = true;
        int k = -1, o = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                if (a[i, j] != '*') {
 
                    // If grid contains atleast
                    // one 1 or 0
                    k = i;
                    o = j;
                    break;
                }
            }
            if (k != -1) {
                break;
            }
        }
        if (k != -1) {
            flag = PosCheck(a, n, m, '1');
            if (flag) {
                fill(a, n, m, '1', '0');
            }
            else {
                flag = PosCheck(a, n, m, '0');
                if (flag) {
                    fill(a, n, m, '0', '1');
                }
            }
        }
        else {
            // Fill the grid in any possible way
            char h = '1';
            for (int i = 0; i < m; i++) {
                a[0, i] = h;
                if (h == '1') {
                    h = '0';
                }
                else {
                    h = '1';
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i - 1 < 0) {
                        continue;
                    }
                    if (a[i - 1, j] == '1') {
                        a[i, j] = '0';
                    }
                    else {
                        a[i, j] = '1';
                    }
                }
            }
            flag = true;
        }
 
        if (!flag) {
            Console.WriteLine("No");
        }
        else {
            Console.WriteLine("Yes");
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    Console.Write(a[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
 
// 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 splevel62.


Javascript


输出
Yes
1 0 1 0 
0 1 0 1 
1 0 1 0 
0 1 0 1 

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

有效方法:这个想法是使用两个变量而不是创建二维数组来维护可能的交替数组。请按照以下步骤解决问题:

  • 定义一个函数posCheck(char grid[][1001], int N, int M, char check)并执行以下任务:
    • 使用变量i迭代范围[0, N]并执行以下任务:
      • 使用变量j迭代范围[0, M]并执行以下任务:
        • 如果grid[i][j]等于'*',则继续。
        • 否则,如果(i+j)%2等于1grid[i][j]不等于check ,则返回false。
        • 否则,如果(i+j)%2等于0grid[i][j]等于check ,则返回false。
    • 执行上述步骤后,返回true值作为答案。
  • 定义一个函数posCheck(char grid[][1001], int N, int M, char odd, char even)并执行以下任务:
    • 使用变量i迭代范围[0, N]并执行以下任务:
      • 使用变量j迭代范围[0, M]并执行以下任务:
        • 如果(i+j)%2等于1,则将grid[i][j]的值设置为奇数,否则为偶数。
  • 将布尔变量标志初始化为真。
  • 将变量ko初始化为-1以存储第一个不包含字符“*”的单元格的值。
  • 使用变量i迭代范围[0, N]并执行以下任务:
    • 使用变量j迭代范围[0, M]并执行以下任务:
      • 如果Grid[i][j]不等于'*',则将k的值设置为i ,将o的值设置为j和 break
    • 如果k不等于-1,则中断。
  • 如果k不等于-1,则调用函数PosCheck(grid, n, m, '1')并将函数返回的值存储在变量flag中,如果flag真,则调用函数fill(grid , n, m, '1', '0')以其中一种可能的方式填充网格。
  • 如果flagfalse,则调用函数PosCheck(grid, n, m, '0')并将函数返回的值存储在变量flag中,如果flagtrue,则调用函数fill(grid, n, m , '0', '1')以其中一种可能的方式填充网格。
  • 如果k等于-1,则将 char 变量h初始化为'0'
  • 使用变量i迭代范围[0, M]并执行以下任务:
    • grid[0][i]的值设置为h ,如果h为' 0 ',则设置为' 1 ',否则为'0'。
  • 使用变量i迭代范围[0, N]并执行以下任务:
    • 使用变量j迭代范围[0, M]并执行以下任务:
      • 如果i-1小于0,则继续。
      • 如果grid[i-1][j]'1',则将其设置为'0',否则设置为'1'。
  • flag的值设置为true ,因为网格是交替的
  • 如果flagfalse,则打印“NO”
  • 否则,打印“YES”并打印grid[][] 的元素。

下面是上述方法的实现。

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function to check for the grid in
// one of the alternating ways
bool PosCheck(char a[][1001], int n,
              int m, char check)
{
 
    // Iterate over the range
    for (int i = 0; i < n; i++) {
 
        // Iterate over the range
        for (int j = 0; j < m; j++) {
 
            if (a[i][j] == '*') {
                continue;
            }
            else {
 
                // (i+j)%2==1 cells should be with
                // the character check and the rest
                // should be with the other character
                if (((i + j) & 1) && a[i][j] != check) {
                    return false;
                }
                if (!((i + j) & 1) && a[i][j] == check) {
                    return false;
                }
            }
        }
    }
    return true;
}
 
// Function to fill the grid in a possible way
void fill(char a[][1001], int n, int m,
          char odd, char even)
{
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if ((i + j) & 1) {
                a[i][j] = odd;
            }
            else {
                a[i][j] = even;
            }
        }
    }
}
 
// Function to find if the grid can be made alternating
void findPossibleGrid(int n, int m, char a[][1001])
{
    bool flag = true;
    int k = -1, o = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
 
            if (a[i][j] != '*') {
 
                // If grid contains atleast
                // one 1 or 0
                k = i;
                o = j;
                break;
            }
        }
        if (k != -1) {
            break;
        }
    }
    if (k != -1) {
        flag = PosCheck(a, n, m, '1');
        if (flag) {
            fill(a, n, m, '1', '0');
        }
        else {
            flag = PosCheck(a, n, m, '0');
            if (flag) {
                fill(a, n, m, '0', '1');
            }
        }
    }
    else {
        // Fill the grid in any possible way
        char h = '1';
        for (int i = 0; i < m; i++) {
            a[0][i] = h;
            if (h == '1') {
                h = '0';
            }
            else {
                h = '1';
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (i - 1 < 0) {
                    continue;
                }
                if (a[i - 1][j] == '1') {
                    a[i][j] = '0';
                }
                else {
                    a[i][j] = '1';
                }
            }
        }
        flag = true;
    }
 
    if (!flag) {
        cout << "NO\n";
    }
    else {
        cout << "YES\n";
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                cout << a[i][j];
            }
            cout << endl;
        }
    }
}
 
// Driver Code
int main()
{
    int n = 4, m = 4;
    char grid[][1001] = { { '*', '*', '1', '0' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '0', '1' } };
 
    findPossibleGrid(n, m, grid);
 
    return 0;
}

Java

/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
    public static boolean PosCheck(char[][] a, int n, int m,
                                   char check)
    {
 
        // Iterate over the range
        for (int i = 0; i < n; i++) {
 
            // Iterate over the range
            for (int j = 0; j < m; j++) {
 
                if (a[i][j] == '*') {
                    continue;
                }
                else {
 
                    // (i+j)%2==1 cells should be with
                    // the character check and the rest
                    // should be with the other character
                    if ((((i + j) & 1) == 1)
                        && a[i][j] != check) {
                        return false;
                    }
                    if (!(((i + j) & 1) == 1)
                        && a[i][j] == check) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    // Function to fill the grid in a possible way
    public static void fill(char[][] a, int n, int m,
                            char odd, char even)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (((i + j) & 1) == 1) {
                    a[i][j] = odd;
                }
                else {
                    a[i][j] = even;
                }
            }
        }
    }
 
    // Function to find if the grid can be made alternating
    public static void findPossibleGrid(int n, int m,
                                        char[][] a)
    {
        boolean flag = true;
        int k = -1, o = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                if (a[i][j] != '*') {
 
                    // If grid contains atleast
                    // one 1 or 0
                    k = i;
                    o = j;
                    break;
                }
            }
            if (k != -1) {
                break;
            }
        }
        if (k != -1) {
            flag = PosCheck(a, n, m, '1');
            if (flag) {
                fill(a, n, m, '1', '0');
            }
            else {
                flag = PosCheck(a, n, m, '0');
                if (flag) {
                    fill(a, n, m, '0', '1');
                }
            }
        }
        else {
            // Fill the grid in any possible way
            char h = '1';
            for (int i = 0; i < m; i++) {
                a[0][i] = h;
                if (h == '1') {
                    h = '0';
                }
                else {
                    h = '1';
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i - 1 < 0) {
                        continue;
                    }
                    if (a[i - 1][j] == '1') {
                        a[i][j] = '0';
                    }
                    else {
                        a[i][j] = '1';
                    }
                }
            }
            flag = true;
        }
 
        if (!flag) {
            System.out.println("No");
        }
        else {
            System.out.println("Yes");
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    System.out.print(a[i][j]);
                }
                System.out.println();
            }
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 4, m = 4;
        char[][] grid = { { '*', '*', '1', '0' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '*', '*' },
                          { '*', '*', '0', '1' } };
 
        findPossibleGrid(n, m, grid);
    }
}
 
// This code is contributed by maddler.

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
     
    public static bool PosCheck(char[,] a, int n, int m,
                                   char check)
    {
 
        // Iterate over the range
        for (int i = 0; i < n; i++) {
 
            // Iterate over the range
            for (int j = 0; j < m; j++) {
 
                if (a[i, j] == '*') {
                    continue;
                }
                else {
 
                    // (i+j)%2==1 cells should be with
                    // the character check and the rest
                    // should be with the other character
                    if ((((i + j) & 1) == 1)
                        && a[i, j] != check) {
                        return false;
                    }
                    if (!(((i + j) & 1) == 1)
                        && a[i, j] == check) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
 
    // Function to fill the grid in a possible way
    public static void fill(char[,] a, int n, int m,
                            char odd, char even)
    {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (((i + j) & 1) == 1) {
                    a[i, j] = odd;
                }
                else {
                    a[i, j] = even;
                }
            }
        }
    }
 
    // Function to find if the grid can be made alternating
    public static void findPossibleGrid(int n, int m,
                                        char[,] a)
    {
        bool flag = true;
        int k = -1, o = -1;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
 
                if (a[i, j] != '*') {
 
                    // If grid contains atleast
                    // one 1 or 0
                    k = i;
                    o = j;
                    break;
                }
            }
            if (k != -1) {
                break;
            }
        }
        if (k != -1) {
            flag = PosCheck(a, n, m, '1');
            if (flag) {
                fill(a, n, m, '1', '0');
            }
            else {
                flag = PosCheck(a, n, m, '0');
                if (flag) {
                    fill(a, n, m, '0', '1');
                }
            }
        }
        else {
            // Fill the grid in any possible way
            char h = '1';
            for (int i = 0; i < m; i++) {
                a[0, i] = h;
                if (h == '1') {
                    h = '0';
                }
                else {
                    h = '1';
                }
            }
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    if (i - 1 < 0) {
                        continue;
                    }
                    if (a[i - 1, j] == '1') {
                        a[i, j] = '0';
                    }
                    else {
                        a[i, j] = '1';
                    }
                }
            }
            flag = true;
        }
 
        if (!flag) {
            Console.WriteLine("No");
        }
        else {
            Console.WriteLine("Yes");
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    Console.Write(a[i, j]);
                }
                Console.WriteLine();
            }
        }
    }
 
// 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 splevel62.

Javascript


输出
YES
1010
0101
1010
0101

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