human
Color a grid such that all same color cells are connected either horizontally or vertically | Python3 program to color a grid such that all same color cells are connected either horizontally or vertically.
Current color.
Final grid.
If even row.
Traverse from left to right.
If color has been exhausted , move to the next color.
Color the grid at this position print ( i , j ).
Reduce the color count.
Traverse from right to left for odd rows.
Print the grid.
Driver code"
Solution in Python:
assistant
#Here is code to color a grid such that all same color cells are connected either horizontally or vertically and the other steps required.
 def solve ( arr , r , c ) :
	idx = 1
	dp = [ [ 0 for i in range ( c ) ] for i in range ( r ) ]
	for i in range ( r ) :
		if ( i % 2 == 0 ) :
			for j in range ( c ) :
				if ( arr [ idx - 1 ] == 0 ) :
					idx += 1
				dp [ i ] [ j ] = idx
				arr [ idx - 1 ] -= 1
		else :
			for j in range ( c - 1 , - 1 , - 1 ) :
				if ( arr [ idx - 1 ] == 0 ) :
					idx += 1
				dp [ i ] [ j ] = idx
				arr [ idx - 1 ] -= 1
	for i in range ( r ) :
		for j in range ( c ) :
			print ( dp [ i ] [ j ] , end = " ▁ " )
		print ( )
if __name__ == ' _ _ main _ _ ' :
	r = 3
	c = 5
	n = 5
	arr = [ 1 , 2 , 3 , 4 , 5 ]
	solve ( arr , r , c )