Open In App

Check if the structure is stable or not after following given conditions

Last Updated : 20 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Given a binary matrix Matrix[][] of size M * N. Then the task is to return YES or NO, by checking whether the structure follows the given conditions or not.

  • All the group of zeros should be entrapped by ones(only from three sides left, right, and down)
  • 1 shouldn’t place in a cell such that the cell beneath it contains 0

Examples:

Input: N = 3, M = 3 

1 0 0
1 1 1
1 1 1
Output: NO
Explanation: In matrix cells (1, 2) and (1, 3) containing character 0, The zeros are not entrapped by ones. The cell at (1, 3) having zero is free from the right side The structure doesn’t follow the first condition. Hence the output is NO

Input: N = 3, M = 3

1 0 1 
1 1 1
1 1 1
Output: YES
Explanation: The zero is trapped between ones from all three sides and there is no cell such that it contains 1 and the cell beneath it is 0. The structure follows the conditions. Hence the output is YES.

Input: N = 2, M = 2

1 1
1 0
Output: NO
Explanation:  Cell (1, 2) is contains 1 and it is placed over cell (2, 2) which contains 0. Formally, structure doesn’t follow the second condition. It should be noted that 0 is also not entrapped by 1. Therefore, output is NO. 

Approach: Implement the idea below to solve the problem

The problem is observation based and can be solved by implementing those observations by a code.   

Steps were taken to solve the problem:

  • Create a boolean flag and mark it as true.
  • Run two nested loops for i = 0 to i < n and j = 0 to j < m and follow the below-mentioned steps under the scope of the loop:
    • if (str[ i ] .charAt( j ) == ‘1’)
      • if ( i + 1 < n )
        •  if (str[ i + 1 ].charAt( j ) == ‘1’) then continue else mark the flag as false and break.
    •  if (str[ i ].charAt( j ) == ‘0’)
      •  if ( j == 0 || j == m – 1 ) then mark the flag as false and break
      •  if ( j > 0 && j < m ) 
        • if (str[ i ].charAt( j – 1 ) == ‘1’ || str[ i ].charAt( j – 1 )== ‘0’ ) then continue else mark the flag as false and break the loop.
      •  if ( j + 1 < m && j > 0 ) 
        • if (str[ i ].charAt( j + 1 ) == ‘1’ || str[ i ].charAt( j + 1 ) == ‘0’) then continue else mark the flag as false and break the loop.
      •   if ( i + 1 < n )
        • if (str[ i + 1 ].charAt( j ) == ‘1’ || str[ i + 1 ].charAt( j )== ‘0’ ) then continue else mark the flag as false and break the loop.
  • If the flag is true then output YES else NO.

Below is the code to implement the approach:

C++

// C++ code to implement the approach

#include <iostream>
using namespace std;

// Function to check stability of structure
void Is_Stable(string str[], int n, int m)
{

    // Flag initialized as true
    bool flag = true;

    // Traversing matrix and implementing approach
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (str[i][j] == '1') {
                if (i + 1 < n) {
                    if (str[i + 1][j] == '1') {
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
            }
            if (str[i][j] == '0') {
                if (j == 0 || j == m - 1) {
                    flag = false;
                    break;
                }
                if (j > 0 && j < m) {
                    if (str[i][j - 1] == '1'
                        || str[i][j - 1] == '0') {
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }

                if (j + 1 < m && j > 0) {
                    if (str[i][j + 1] == '1'
                        || str[i][j + 1] == '0') {
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
                if (i + 1 < n) {
                    if (str[i + 1][j] == '1'
                        || str[i + 1][j] == '0') {
                        continue;
                    }
                    else {
                        flag = false;
                        break;
                    }
                }
            }
        }
    }

    // Printing output
    if (flag)
        cout << "YES" << endl;
    else
        cout << "NO" << endl;
}

int main()
{

    // Inputs
    int n = 2;
    int m = 3;
    string str[] = { "101", "111" };

    // Function call
    Is_Stable(str, n, m);
    return 0;
}

// This code is contributed by karthik.

Java

// Java code to implement the approach

import java.io.*;
import java.lang.*;
import java.util.*;

class GFG {

    // Driver Function
    public static void main(String[] args)
        throws java.lang.Exception
    {

        // Inputs
        int n = 2;
        int m = 3;
        String[] str = { "101", "111" };

        // Function call
        Is_Stable(str, n, m);
    }

    // Method to check stability
    // of structure
    static void Is_Stable(String[] str, int n, int m)
    {

        // Flag initialized as true
        boolean flag = true;

        // Traversing matrix and
        // implementing approach
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (str[i].charAt(j) == '1') {
                    if (i + 1 < n) {
                        if (str[i + 1].charAt(j) == '1') {
                            continue;
                        }
                        else {
                            flag = false;
                            break;
                        }
                    }
                }
                if (str[i].charAt(j) == '0') {
                    if (j == 0 || j == m - 1) {
                        flag = false;
                        break;
                    }
                    if (j > 0 && j < m) {
                        if (str[i].charAt(j - 1) == '1'
                            || str[i].charAt(j - 1)
                                   == '0') {
                            continue;
                        }
                        else {
                            flag = false;
                            break;
                        }
                    }

                    if (j + 1 < m && j > 0) {
                        if (str[i].charAt(j + 1) == '1'
                            || str[i].charAt(j + 1)
                                   == '0') {
                            continue;
                        }
                        else {
                            flag = false;
                            break;
                        }
                    }
                    if (i + 1 < n) {
                        if (str[i + 1].charAt(j) == '1'
                            || str[i + 1].charAt(j)
                                   == '0') {
                            continue;
                        }
                        else {
                            flag = false;
                            break;
                        }
                    }
                }
            }
        }

        // Printing output
        if (flag)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}

Python

# Python code to implement the approach
def Is_Stable(str, n, m):
 
    # Flag initialized as true
    flag = True
 
    # Traversing matrix and implementing approach
    for i in range(n):
        for j in range(m):
            if (str[i][j] == '1'):
                if (i + 1 < n):
                    if (str[i + 1][j] == '1'):
                        continue
                    else:
                        flag = False
                        break
            if (str[i][j] == '0'):
                if (j == 0 or j == m - 1):
                    flag = False
                    break
                if (j > 0 and j < m):
                    if (str[i][j - 1] == '1' or str[i][j - 1] == '0'):
                        continue
                    else:
                        flag = False
                        break
                if (j + 1 < m and j > 0):
                    if (str[i][j + 1] == '1' or str[i][j + 1] == '0'):
                        continue
                    else:
                        flag = False
                        break
                if (i + 1 < n):
                    if (str[i + 1][j] == '1' or str[i + 1][j] == '0'):
                        continue
                    else:
                        flag = False
                        break
 
    # Printing output
    if (flag):
        print("YES")
    else:
        print("NO")
 
# Driver Function
n = 2
m = 3
str = ["101", "111"]
 
# Function call
Is_Stable(str, n, m)

Javascript

function Is_Stable(str, n, m) {
// Flag initialized as true
let flag = true;
// Traversing matrix and implementing approach
for (let i = 0; i < n; i++) {
    for (let j = 0; j < m; j++) {
        if (str[i].charAt(j) == '1') {
            if (i + 1 < n) {
                if (str[i + 1].charAt(j) == '1') {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }
        }
        if (str[i].charAt(j) == '0') {
            if (j == 0 || j == m - 1) {
                flag = false;
                break;
            }
            if (j > 0 && j < m) {
                if (str[i].charAt(j - 1) == '1' || str[i].charAt(j - 1) == '0') {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }

            if (j + 1 < m && j > 0) {
                if (str[i].charAt(j + 1) == '1' || str[i].charAt(j + 1) == '0') {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }
            if (i + 1 < n) {
                if (str[i + 1].charAt(j) == '1' || str[i + 1].charAt(j) == '0') {
                    continue;
                } else {
                    flag = false;
                    break;
                }
            }
        }
    }
}

// Printing output
if (flag)
    console.log("YES");
else
    console.log("NO");
    }

// Driver Function

// Inputs
const n = 2;
const m = 3;
const str = ["101", "111"];
// Function call
Is_Stable(str, n, m);

C#

// C# code to implement the approach
using System;

class GFG {

  // Driver Function
  public static void Main()
  {

    // Inputs
    int n = 2;
    int m = 3;
    string[] str = { "101", "111" };

    // Function call
    Is_Stable(str, n, m);
  }

  // Method to check stability
  // of structure
  static void Is_Stable(string[] str, int n, int m)
  {

    // Flag initialized as true
    bool flag = true;

    // Traversing matrix and
    // implementing approach
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (str[i][j] == '1') {
          if (i + 1 < n) {
            if (str[i + 1][j] == '1') {
              continue;
            }
            else {
              flag = false;
              break;
            }
          }
        }
        if (str[i][j] == '0') {
          if (j == 0 || j == m - 1) {
            flag = false;
            break;
          }
          if (j > 0 && j < m) {
            if (str[i][j - 1] == '1'
                || str[i][j - 1] == '0') {
              continue;
            }
            else {
              flag = false;
              break;
            }
          }

          if (j + 1 < m && j > 0) {
            if (str[i][j + 1] == '1'
                || str[i][j + 1] == '0') {
              continue;
            }
            else {
              flag = false;
              break;
            }
          }
          if (i + 1 < n) {
            if (str[i + 1][j] == '1'
                || str[i + 1][j] == '0') {
              continue;
            }
            else {
              flag = false;
              break;
            }
          }
        }
      }
    }

    // Printing output
    if (flag)
      Console.WriteLine("YES");
    else
      Console.WriteLine("NO");
  }
}

// This code is contributed by Prajwal Kandekar
Output

YES

Time Complexity: (M * N)
Auxiliary Space:  O(1) 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads