human
What is a code to check if a matrix contains a square submatrix with 0 as boundary element and the other steps required given the following:
Check if a matrix contains a square submatrix with 0 as boundary element | Function checks if square with all 0 's in boundary exists in the matrix.
r1 is the top row , c1 is the left col r2 is the bottom row , c2 is the right.
Function checks if the boundary of the square consists of 0 's.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to check if a matrix contains a square submatrix with 0 as boundary element and the other steps required.
 def squareOfZeroes ( ) :
	global matrix , cache
	lastIdx = len ( matrix ) - 1
	return hasSquareOfZeroes ( 0 , 0 , lastIdx , lastIdx )
def hasSquareOfZeroes ( r1 , c1 , r2 , c2 ) :
	global matrix , cache
	if ( r1 >= r2 or c1 >= c2 ) :
		return False
	key = ( str ( r1 ) + ' - ' + str ( c1 ) + ' - ' + str ( r2 ) + ' - ' + str ( c2 ) )
	if ( key in cache ) :
		return cache [ key ]
	cache [ key ] = ( isSquareOfZeroes ( r1 , c1 , r2 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 - 1 , c2 - 1 ) )
	cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 , c1 + 1 , r2 - 1 , c2 ) or hasSquareOfZeroes ( r1 + 1 , c1 , r2 , c2 - 1 ) )
	cache [ key ] = ( cache [ key ] or hasSquareOfZeroes ( r1 + 1 , c1 + 1 , r2 , c2 ) or hasSquareOfZeroes ( r1 , c1 , r2 - 1 , c2 - 1 ) )
	return cache [ key ]
def isSquareOfZeroes ( r1 , c1 , r2 , c2 ) :
	global matrix
	for row in range ( r1 , r2 + 1 ) :
		if ( matrix [ row ] [ c1 ] != 0 or matrix [ row ] [ c2 ] != 0 ) :
			return False
	for col in range ( c1 , c2 + 1 ) :
		if ( matrix [ r1 ] [ col ] != 0 or matrix [ r2 ] [ col ] != 0 ) :
			return False
	return True
if __name__ == ' _ _ main _ _ ' :
	cache = { }
	matrix = [ [ 1 , 1 , 1 , 0 , 1 , 0 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 1 , 0 , 1 ] , [ 0 , 1 , 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 , 0 , 1 ] ]
	ans = squareOfZeroes ( )
	if ( ans == 1 ) :
		print ( " True " )
	else :
		print ( " False " )