Color a grid such that all same color cells are connected either horizontally or vertically
Given three integers R, C, N, and an array arr[] of size N. The task is to color all cells of a grid of R rows and C columns such that all same color cells are connected either horizontally or vertically. N represents the colors numbered from 1 to N and arr[] denotes the quantity of each color. The total quantity of color is exactly equal to the total number of cells of the grid.
Approach:
Input: R = 3, C = 5, N = 5, arr[] = {1, 2, 3, 4, 5}
Output:
1 4 4 4 3
2 5 4 5 3
2 5 5 5 3
Explanation: Available colors are 1(count = 1), 2(count = 2), 3(count = 3) etc.
For color 5: we can reach all color 5s by going horizontally or vertically through the same color 5.
Similarly for color 3, the rightmost row contains all 3 etc.
Similarly, for the rest of the colors 1, 2, 4.
Below is an invalid grid:
1 4 3 4 4
2 5 4 5 3
2 5 5 5 3
This is because the connection for the colors 3 and 4 has been broken by the invalid position of 3
in the position(0, 2).We can no longer traverse through all the 4s or all the 3s, horizontally or vertically, by passing through the respective 3s and 4s only.
Input: R = 2, C = 2, N = 3, arr[] = {2, 1, 1}
Output:
1 1
2 3
Approach:
At first glance, it might seem that graph algorithms are required. However, we are going to follow an optimized greedy algorithm.
- Create a new 2D array which will be our final grid. Let us call it dp[][].
- Traverse the color array A[]
- For each color, i having A[i] quantities
- If the row is an odd-numbered row, fill the dp array from left to right
- Else if it is an even row, fill it from right to left
- If the quantity of color is used up, move on to the next color greedily
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve(vector< int >& arr,
int r, int c)
{
int idx = 1;
int dp[r];
for ( int i = 0; i < r; i++) {
if (i % 2 == 0) {
for ( int j = 0; j < c; j++) {
if (arr[idx - 1] == 0)
idx++;
dp[i][j] = idx;
arr[idx - 1]--;
}
}
else {
for ( int j = c - 1; j >= 0; j--) {
if (arr[idx - 1] == 0)
idx++;
dp[i][j] = idx;
arr[idx - 1]--;
}
}
}
for ( int i = 0; i < r; ++i) {
for ( int j = 0; j < c; ++j) {
cout << dp[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int r = 3, c = 5;
int n = 5;
vector< int > arr
= { 1, 2, 3, 4, 5 };
solve(arr, r, c);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void solve(List<Integer> arr,
int r, int c)
{
int idx = 1 ;
int [][] dp = new int [r];
for ( int i = 0 ; i < r; i++)
{
if (i % 2 == 0 )
{
for ( int j = 0 ; j < c; j++)
{
if (arr.get(idx - 1 ) == 0 )
idx++;
dp[i][j] = idx;
arr.set(idx - 1 ,
arr.get(idx - 1 ) - 1 );
}
}
else
{
for ( int j = c - 1 ; j >= 0 ; j--)
{
if (arr.get(idx - 1 ) == 0 )
idx++;
dp[i][j] = idx;
arr.set(idx - 1 ,
arr.get(idx - 1 ) - 1 );
}
}
}
for ( int i = 0 ; i < r; ++i)
{
for ( int j = 0 ; j < c; ++j)
{
System.out.print(dp[i][j] + " " );
}
System.out.println();
}
}
public static void main (String[] args)
{
int r = 3 , c = 5 ;
int n = 5 ;
List<Integer> arr = Arrays.asList( 1 , 2 , 3 , 4 , 5 );
solve(arr, r, c);
}
}
|
Python3
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)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void solve(List< int > arr,
int r, int c)
{
int idx = 1;
int [,] dp = new int [r, c];
for ( int i = 0; i < r; i++)
{
if (i % 2 == 0)
{
for ( int j = 0; j < c; j++)
{
if (arr[idx - 1] == 0)
idx++;
dp[i, j] = idx;
arr[idx - 1] = arr[idx - 1] - 1;
}
}
else
{
for ( int j = c - 1; j >= 0; j--)
{
if (arr[idx - 1] == 0)
idx++;
dp[i, j] = idx;
arr[idx - 1] = arr[idx - 1] - 1;
}
}
}
for ( int i = 0; i < r; ++i)
{
for ( int j = 0; j < c; ++j)
{
Console.Write(dp[i, j] + " " );
}
Console.Write( '\n' );
}
}
public static void Main ( string [] args)
{
int r = 3, c = 5;
List< int > arr = new List< int >();
arr.Add(1);
arr.Add(2);
arr.Add(3);
arr.Add(4);
arr.Add(5);
solve(arr, r, c);
}
}
|
Javascript
<script>
function solve(arr,r,c)
{
var idx = 1;
var dp = new Array(r);
var i,j;
for (i=0;i<r;i++)
dp[i] = new Array(c);
for (i = 0; i < r; i++) {
if (i % 2 == 0) {
for (j = 0; j < c; j++) {
if (arr[idx - 1] == 0)
idx++;
dp[i][j] = idx;
arr[idx - 1]--;
}
}
else {
for (j = c - 1; j >= 0; j--) {
if (arr[idx - 1] == 0)
idx++;
dp[i][j] = idx;
arr[idx - 1]--;
}
}
}
for (i = 0; i < r; ++i) {
for (j = 0; j < c; ++j) {
document.write(dp[i][j] + " " );
}
document.write( "<br>" );
}
}
var r = 3, c = 5;
var n = 5;
var arr = [1, 2, 3, 4, 5];
solve(arr, r, c);
</script>
|
Output:
1 2 2 3 3
4 4 4 4 3
5 5 5 5 5
Last Updated :
26 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...