I am trying to solve thisproblem. I have implemented the code but there seems to be some mistake with the logic that I am using. I think the problem is when c==4. Can you please help me figure this out.
@montycs You just need to realise that what information you need to simulate the whole game is current position and we are holding an item or not… (pos and hold variable in my code…
If c==1 pos-- if pos > 0 (move left if its not already of leftmost position…)
If c==2 pos++ if pos < N-1 (i have used 0-based indexing)
If c==3 pick up box if box[pos] >0 AND !hold
box[pos]-- (reduced no of items in current box by one)
hold =true
Similarly if c==4 drop if box[pos]<H AND hold = true
Box[pos]++ (increased count of items by one)
hold = false
Your code is almost correct except a silly mistake-
int j=0;
while(c!=0)
{
bool isPicked = false;
Every iteration you are setting isPicked=false. I think that statement should be out of while loop, because lets say you picked up a box and set isPicked=true. Then in the immediate next iteration you are setting it false, and this leads to error when drop operations are to be done.