brute force constructive algorithms graphs implementation

Please click on ads to support us..

Python Code:

t = int(input())
while t:
    n, m = input().split()
    n = int(n)
    m = int(m)
    c = n
    grid = []
    while c:
        s = input()
        grid.append(s)
        c = c - 1
    for i in range(n):
        grid[i] = list(grid[i])
    for i in range(n):
        for j in range(m):
            grid[i][j] = int(grid[i][j])
    found = 0
    for i in range(n):
        for j in range(m):
            s = grid[i][j]
            if i != n - 1:
                s += grid[i + 1][j]
            if j != m - 1:
                s += grid[i][j + 1]
            if i != n - 1 and j != m - 1:
                s += grid[i + 1][j + 1]
            if s == 3:
                found = 1
                break
    if found == 1:
        print('NO')
    else:
        print('YES')
    t = t - 1

C++ Code:

// Online C++ compiler to run C++ program online
#include <bits/stdc++.h>
using namespace std;
// const int N=1e5+10;
// vector<int> graph[N];

int main() {
    int t;
    cin>>t;
    while(t--){
        int n,m;
        cin>>n>>m;
        vector<string> st;
        for(int i=0;i<n;i++){
            string s;
            cin>>s;
            st.push_back(s);
        }
        bool flag=false;
        for(int i=0;i<n-1;i++){
            for(int j=0;j<m-1;j++){
                int sum = st[i][j]+st[i+1][j]+st[i][j+1]+st[i+1][j+1] - 4*(48);
                if(sum == 3){ flag = true;break;}
            }
            if(flag==true) break;
        }
        if(!flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push