Codeforces Round #777 Div 2 – Problem B

Original problem statement taken from this link.

Madoka and the Elegant Gift

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Madoka’s father just reached 11 million subscribers on Mathub! So the website decided to send him a personalized award — The Mathhub’s Bit Button!

The Bit Button is a rectangular table with N rows and M columns with 00 or 11 in each cell. After exploring the table Madoka found out that:

  • A subrectangle A is contained in a subrectangle B if there’s no cell contained in A but not contained in B.
  • Two subrectangles intersect if there is a cell contained in both of them.
  • A subrectangle is called black if there’s no cell with value 00 inside it.
  • A subrectangle is called nice if it’s black and it’s not contained in another black subrectangle.
  • The table is called elegant if there are no two nice intersecting subrectangles.

For example, in the first illustration the red subrectangle is nice, but in the second one it’s not, because it’s contained in the purple subrectangle.

Help Madoka to determine whether the table is elegant.

Input

Each test contains multiple test cases. The first line contains a single integer T (1 <= T <= 200) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two positive integers N and M (1 <= N, M <= 100).

The next N lines contain strings of length M consisting of zeros and ones — the description of the table.

It is guaranteed that the sum of the values of N and the sum of the values of M for all test cases do not exceed 777.

Output

For each test case print “YES” if its table is elegant or print “NO” otherwise.

You may print each letter in any case (for example, “YES”, “Yes”, “yes”, “yEs” will all be recognized as positive answer).

Example input:

5
3 3
100
011
011
3 3
110
111
110
1 5
01111
4 5
11111
01010
01000
01000
3 2
11
00
11

Example output:

YES
NO
YES
NO
YES

Note:

In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.

In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.

Solution

This is how one possible implementation would look like:

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, const char * argv[]) {
    int t;
    cin >> t;
    while (t--) {
        int n, m;
        cin >> n  >> m;
        vector< vector<bool> > a(n, vector<bool>(m, 0));
        for (int i=0;i<n;i++) {
            for (int j=0;j<m;j++) {
                char k;
                cin >> k;
                a[i][j] = (k == '0') ? false : true;
            }
        }
        
        int i;
        for (i=0;i<n-1;i++) {
            int j;
            for (j=0;j<m-1;j++) {
                // 1 1
                // 0 1
                if (a[i][j] == 1 && a[i][j+1] == 1 && a[i+1][j] == 0 && a[i+1][j+1] == 1) break;
                
                // 1 0
                // 1 1
                if (a[i][j] == 1 && a[i][j+1] == 0 && a[i+1][j] == 1 && a[i+1][j+1] == 1) break;
                
                // 1 1
                // 1 0
                if (a[i][j] == 1 && a[i][j+1] == 1 && a[i+1][j] == 1 && a[i+1][j+1] == 0) break;
                
                // 0 1
                // 1 1
                if (a[i][j] == 0 && a[i][j+1] == 1 && a[i+1][j] == 1 && a[i+1][j+1] == 1) break;
            }
            if (j < m-1) {
                cout << "NO" << endl;
                break;
            }
        }
        if (i == n-1) cout << "YES" << endl;
    }
    
    return 0;
}

Leave a Comment