human
Maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix | Python3 program to find the maximum distance from a 0 - cell to a 1 - cell.
If the matrix consists of only 0 ' s ▁ ▁ or ▁ only ▁ 1' s.
If it 's a 0-cell.
Calculate its distance with every 1 - cell.
Compare and store the minimum.
Compare ans store the maximum.
Driver code"
Solution in Python:
assistant
#Here is code to maximum of all distances to the nearest 1 cell from any 0 cell in a Binary matrix and the other steps required.
 def maxDistance ( grid ) :
	one = [ ]
	M = len ( grid )
	N = len ( grid [ 0 ] )
	ans = - 1
	for i in range ( M ) :
		for j in range ( N ) :
		if ( grid [ i ] [ j ] == 1 ) :
			one . append ( [ i , j ] )
	if ( one == [ ] or M * N == len ( one ) ) :
		return - 1
	for i in range ( M ) :
		for j in range ( N ) :
		if ( grid [ i ] [ j ] == 1 ) :
			continue
		dist = float ( ' inf ' )
		for p in one :
			d = abs ( p [ 0 ] - i ) + abs ( p [ 1 ] - j )
			dist = min ( dist , d )
			if ( dist <= ans ) :
			break
			ans = max ( ans , dist )
	return ans
arr = [ [ 0 , 0 , 1 ] , [ 0 , 0 , 0 ] , [ 0 , 0 , 0 ] ]
print ( maxDistance ( arr ) )