1360E - Polygon - CodeForces Solution


dp graphs implementation shortest paths *1300

Please click on ads to support us..

Python Code:

for i in' '*int(input()):
    l=[]
    g=0
    n=int(input())
    for i in' '*n:
        l.append(input()+'1')
    l.append('1'*n)
    for x in range(n):
        for i in range(n):
            if l[x][i]=='1'and not(l[x+1][i]=='1'or l[x][i+1]=='1'):
                g=1
    print('YNEOS'[g::2])

C++ Code:

// Problem: E. Polygon
// Contest: Codeforces - Codeforces Round #644 (Div. 3)
// URL: https://codeforces.com/contest/1360/problem/E
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<utility>
#include<set>
#include<unordered_set>
#include<list>
#include<iterator>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<bitset>
#include<random>
#include<map>
#include<unordered_map>
#include<stdio.h>
#include<complex>
#include<math.h>
#include<cstring>
#include<chrono>
#include<string>

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("no-stack-protector")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
// #pragma GCC optimize("fast-math") 

using namespace std;
#define pb push_back
#define ff first
#define ss second
#define vi vector <int>
#define vvi vector <vi>
#define pii pair<int, int>
#define ppi pair<pii, int>
#define vii vector<pii>
#define mii map<int, int>
#define mci map<char, int>
#define miv map<int, vi>
#define mis map<int, set<int>>
#define setbits(n) __builtin_popcount(n)
#define msb(x) __builtin_clz(x)
#define lsb(x) __builtin_ctz(x)
#define all(v) (v).begin(), (v).end()
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
#define endl "\n"
#define fo(i,n) for(int i=0;i<n;i++)
#define in(a,n)	for(int i=0;i<n;i++) cin>>a[i];
#define dbg(x) cout << #x << " = " << (x) << endl
#define show2(a, b) cout<<a<<' '<<b<<endl
#define show3(a, b, c) cout<<a<<' '<<b<<' '<<c<<endl
#define show(arr) for (auto i:arr) cout<<i<<' '; 
#define Endl endl
const long long N=1e5+5;
const long long mod=1000000007; //998244353;
//always use arrays if you want to coordinate compress.
vvi vis;
vector<string> s;
int n;
bool check(int x, int y)
{
	if(x==n || y==n)return true;
	if(vis[x][y]!=-1)return vis[x][y];
	if(s[x][y]!='1')return vis[x][y]=false;
	return vis[x][y]=(check(x+1,y)||check(x,y+1));
}
void cases(){
	cin>>n;
	s.clear();s.resize(n);in(s,n);
	vis.clear();vis.assign(n,vi(n,-1));
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<n;j++)
		{
			if(vis[i][j]==1 || s[i][j]=='0')continue;
			if(!check(i,j))
			{
				// show2(i,j);
				no;return;
			}
		}
	}
	yes;
}

int32_t main(){
    std::ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int t=1;
    cin>>t;
    for (int i=0; i<t; i++){
    	//cout<<"Case #"<<i+1<<": ";
		cases();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

137C - History
1443C - The Delivery Dilemma
6C - Alice Bob and Chocolate
1077C - Good Array
285B - Find Marble
6A - Triangle
1729A - Two Elevators
1729B - Decode String
1729C - Jumping on Tiles
1729E - Guess the Cycle Size
553B - Kyoya and Permutation
1729D - Friends and the Restaurant
1606C - Banknotes
580C - Kefa and Park
342A - Xenia and Divisors
1033A - King Escape
39D - Cubical Planet
1453A - Cancel the Trains
645A - Amity Assessment
1144A - Diverse Strings
1553B - Reverse String
1073A - Diverse Substring
630N - Forecast
312B - Archer
34D - Road Map
630I - Parking Lot
160B - Unlucky Ticket
371B - Fox Dividing Cheese
584B - Kolya and Tanya
137B - Permutation