Robot Picking Boxes

                            Robot Picking Boxes

                         


Robots are used to automate the process of picking up mobile phone boxes and relocating them within a warehouse. The boxes are stacked and arranged adjacently. All the stacks may not have equal number of boxes.
The robot can collect and pickup boxes either vertically (boxes in a single stack) or horizontally (across multiple stacks). But when picking up boxes horizontally, the robot can pick up only the boxes in continous stacks in a single attempt. Given the number of stacks N and the count of boxes in each stack, the program must print the minimum number of attempts M required to pick (collect) and relocate all the boxes.
Input Format:
The first line contains N.
The second line contains the number of boxes B(1), B(2), … B(N) in the N stacks separated by a space.
Output Format:
The first line contains M (which is the minimum number of attempts)
Boundary Conditions:
2 <= N <= 100
1 <= B(i) <= 50
Example Input/Output 1:
Input:
6
5 4 4 3 4 4
Output:
6
Explanation:
The boxes are stacked as given below where each asterisk is a box.
*
*** **
******
******
******
As the robot can pick only continuous boxes in a single attempt, at least 6 attempts are required.
Example Input/Output 2:
Input:
10
5 5 5 5 4 5 5 5 5 5
Output:
6
Example Input/Output 3:
Input:
10
29 5 5 5 4 5 5 5 5 18
Output:
8

Code:

#include<stdio.h>
#include<conio.h>
int n,a[100],i,small,count=0;
int check()
{
 for(i=0;i<n;i++)
 {
  if(a[i]!=0)
   return 0;
 }
 return 1;
}

void minimum(int start,int end)
{
 int small=a[start];
 for(i=start+1;i<end;i++)
 {
  if(small>a[i])
  {
   small=a[i];
  }
 }
 if(end-start==1&&a[start]!=0)
  small=1;
 for(i=start;i<end;i++)
 {
  a[i]-=small;
 }
 count+=small;
}

void robot(int start,int end)
{
 if(check())
  return;
 minimum(start,end);
 for(i=start;i<end;i++)
 {
  if(a[i]==0)
  {
   robot(start,i);

   robot(i+1,end); 
  }
 }

int main()
{
 scanf(“%d”,&n);
 for(i=0;i<n;i++)
 scanf(“%d”,&a[i]);
 robot(0,n);
 printf(“%d”,count);
 return 0;

}

Logic 2

#include <iostream>
 
using namespace std;
int rec(int b[],int l,int r,int h)
{
    if(l>=r)
    return 0;
    int m=l;
    for(int i=l;i<r;i++)
    {
        if(b[i]<b[m])
        m=i;
    }
    return min(r-l,rec(b,l,m,b[m])+rec(b,m+1,r,b[m])+b[m]-h);
}
int minsteps(int b[],int n)
{
    return rec(b,0,n,0);
}
int main(int argc, char** argv)
{
int n,i,b[100];
cin>>n;
for(i=0;i<n;i++)
{
cin>>b[i];
}
cout<<minsteps(b,n);
}

Logic 3:

import java.util.*;
public class Hello {
    
    static int findCount(int array[],int sIndex,int eIndex)
    {
        int relocateCount=0;
         if(eIndex-sIndex==0)
         {
             relocateCount+=1;
            return relocateCount;
         }
         int min=999999;
         for(int i=sIndex;i<=eIndex;i++)
         {
              if(array[i]<min)
              min=array[i];
         }
         relocateCount+=min;
         for(int i=sIndex;i<=eIndex;i++)
         {
             array[i]-=min;
         }
         int sTempIndex=-1;
         int eTempIndex=-1;
         
         for(int i=sIndex;i<=eIndex;i++)
         {
             if(array[i]>0)
             {
                 if(sTempIndex==-1)
                 {
                     sTempIndex=i;
                 }
             }
              if(array[i]==0)
              {
                  if(sTempIndex!=-1)
                  {
                      if(eTempIndex==-1)
                      {
                           eTempIndex=i-1;
                           relocateCount+=findCount(array,sTempIndex,eTempIndex);
                           sTempIndex=-1;
                           eTempIndex=-1;
                           
                      }
                  }
              }
         }
        if(sTempIndex!=-1&&eTempIndex==-1)
        {
            eTempIndex=eIndex;
            relocateCount+=findCount(array,sTempIndex,eTempIndex);
        }
        if((eIndex-sIndex+1)<relocateCount)
        {
            return (eIndex-sIndex+1);
        }
        else
            return relocateCount;
        
    }
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        int numberOfColumns=input.nextInt();
        int totalCount=0;
        int[] boxesInColumns=new int[numberOfColumns];
        for(int i=0;i<numberOfColumns;i++)
        {
            boxesInColumns[i]=input.nextInt();
        }
        totalCount=findCount(boxesInColumns,0,numberOfColumns-1);
        if(totalCount<numberOfColumns)
                System.out.println(totalCount);
    else
    System.out.println(numberOfColumns);
    }
}

Python:

def findCount(a,si,ei):
    rc=0
    if ei-si==0:
        rc+=1
        return rc
    m=999999
    for i in range(si,ei+1):
        if a[i]<m:
            m=a[i]
    rc+=m
    for i in range(si,ei+1):
        a[i]-=m
    sti,eti=-1,-1
    for i in range(si,ei+1):
        if a[i]>0 and sti==-1:
            sti=i
        if a[i]==0 and sti!=-1 and eti==-1:
            eti=i-1
            rc+=findCount(a,sti,eti)
            sti,eti=-1,-1
    if sti!=-1 and eti==-1:
        eti=ei
        rc+=findCount(a,sti,eti)
    return min(rc,ei-si+1)
n=input()
a=map(int,raw_input().strip().split())

print min(n,findCount(a,0,n-1))

Please do comment If u have any Queries!

8 Comments

Add a Comment

Your email address will not be published. Required fields are marked *