human
Distance of nearest cell having 1 in a binary matrix | Prthe distance of nearest cell having 1 for each cell ..
Initialize the answer matrix with INT_MAX ..
For each cell.
Traversing the whole matrix to find the minimum distance ..
If cell contain 1 , check for minimum distance ..
Printing the answer ..
Driver Code"
Solution in Python:
 Write me a code to distance of nearest cell having 1 in a binary matrix and the other steps required?
assistant
#Here is code to distance of nearest cell having 1 in a binary matrix and the other steps required.
 def printDistance ( mat ) :
	global N , M
	ans = [ [ None ] * M for i in range ( N ) ]
	for i in range ( N ) :
		for j in range ( M ) :
			ans [ i ] [ j ] = 999999999999
	for i in range ( N ) :
		for j in range ( M ) :
			for k in range ( N ) :
				for l in range ( M ) :
					if ( mat [ k ] [ l ] == 1 ) :
						ans [ i ] [ j ] = min ( ans [ i ] [ j ] , abs ( i - k ) + abs ( j - l ) )
	for i in range ( N ) :
		for j in range ( M ) :
			print ( ans [ i ] [ j ] , end = " ▁ " )
		print ( )
N = 3
M = 4
mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 0 , 1 , 1 ] , [ 0 , 1 , 1 , 0 ] ]
printDistance ( mat )