Python + NumPy 204 202 195 Bytes
from numpy import*
def f(A):
r,c=A.shape
z,s=zeros((r,1)),array([0,2,c+3])
B=hstack((z,A,z)).flat
for i in range(1,(r-1)*(c+2)):
if B[i]and not any(B[s]):return 1<0
s+=1
return any(B[i:])
from numpy import*
def f(A):
r,c=A.shape
z,s=zeros((r,1)),array([0,2,c+3])
B=hstack((z,A,z)).flat
for i in range(1,(r-1)*(c+2)):
if B[i]and not any(B[s]):return 1<0
s+=1
return any(B[i:])
Expects A
to be a 2D numpy array.
Takes the matrix, pads zero columns left and right and flattens the matrix. s
is a stencil that points to the left, right and lower element. The loop checks each element except the last line if it is 1
and at least one of its stencil is 1
, returns False
otherwise. Afterwards, check if the last line contains any 1
.
Two testcases for you:
I1 = '001001\n011001\n010111\n110101\n011101\n001011'
A1 = array([int(c) for c in I1.replace('\n','')]).reshape(6,6)
print f(A1) #True
I2 = '001100\n111111\n110101\n010011\n111011'
A2 = array([int(c) for c in I2.replace('\n','')]).reshape(5,6)
print f(A2) #False
I1 = '001001\n011001\n010111\n110101\n011101\n001011'
A1 = array([int(c) for c in I1.replace('\n','')]).reshape(6,6)
print f(A1) #True
I2 = '001100\n111111\n110101\n010011\n111011'
A2 = array([int(c) for c in I2.replace('\n','')]).reshape(5,6)
print f(A2) #False
Edit1: 1<0
is shorter than False
Edit2: flat
is a fine alternative to flatten()
and using tabulators for the second intendation in the loop