human
Unique paths covering every non | Function for dfs . i , j == > Current cell indexes vis == > To mark visited cells ans == > Result z == > Current count 0 s visited z_count == > Total 0 s present.
Mark the block as visited.
Update the count.
If end block reached.
If path covered all the non - obstacle blocks.
Up.
Down.
Left.
Right.
Unmark the block ( unvisited ).
Function to return the count of the unique paths.
Total 0 s present.
Count non - obstacle blocks.
Starting position.
Driver code"
Solution in Python:
assistant
#Here is code to unique paths covering every non and the other steps required.
 def dfs ( i , j , grid , vis , ans , z , z_count ) :
	n = len ( grid )
	m = len ( grid [ 0 ] )
	vis [ i ] [ j ] = 1
	if ( grid [ i ] [ j ] == 0 ) :
		z += 1
	if ( grid [ i ] [ j ] == 2 ) :
		if ( z == z_count ) :
			ans += 1
		vis [ i ] [ j ] = 0
		return grid , vis , ans
	if ( i >= 1 and not vis [ i - 1 ] [ j ] and grid [ i - 1 ] [ j ] != - 1 ) :
		grid , vis , ans = dfs ( i - 1 , j , grid , vis , ans , z , z_count )
	if ( i < n - 1 and not vis [ i + 1 ] [ j ] and grid [ i + 1 ] [ j ] != - 1 ) :
		grid , vis , ans = dfs ( i + 1 , j , grid , vis , ans , z , z_count )
	if ( j >= 1 and not vis [ i ] [ j - 1 ] and grid [ i ] [ j - 1 ] != - 1 ) :
		grid , vis , ans = dfs ( i , j - 1 , grid , vis , ans , z , z_count )
	if ( j < m - 1 and not vis [ i ] [ j + 1 ] and grid [ i ] [ j + 1 ] != - 1 ) :
		grid , vis , ans = dfs ( i , j + 1 , grid , vis , ans , z , z_count )
	vis [ i ] [ j ] = 0
	return grid , vis , ans
def uniquePaths ( grid ) :
	z_count = 0
	n = len ( grid )
	m = len ( grid [ 0 ] )
	ans = 0
	vis = [ [ 0 for j in range ( m ) ] for i in range ( n ) ]
	x = 0
	y = 0
	for i in range ( n ) :
		for j in range ( m ) :
			if grid [ i ] [ j ] == 0 :
				z_count += 1
			elif ( grid [ i ] [ j ] == 1 ) :
				x = i
				y = j
	grid , vis , ans = dfs ( x , y , grid , vis , ans , 0 , z_count )
	return ans
if __name__ == ' _ _ main _ _ ' :
	grid = [ [ 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] , [ 0 , 0 , 2 , - 1 ] ]
	print ( uniquePaths ( grid ) )