Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Problem
You have taken your kids to a amusement center in VR Mall.
One of the games in the amusement center is Bop the Munchkin.

The game is arranged in 6x6 square with 36 evenly spaced cells.
Cell (0,0) is the bottom left, cell (1, 0) is the one above and cell (0, 1) is the one to the right of the starting cell.

There are muchkins that pop out of the hole in each cell.
Munchkins pop out once every 2 seconds and stay on for the entire 2 second duration.
A mace can be used to bop a munchkin that is popping out of the hole
If a mace can move over the cell before 2 seconds are up, then it can hit the munckin.
This action is instantaneous and does not consume time.

You have three different maces to pick from. Each mace has a weight and speed.
* Mace M1 has a weight 100gms and moves at 1 cell per game second.
* Mace M2 has a weight of 75 gms and can move at a speed of 2 cells per game second.
* Mace M3 has a weight of 50 gms and can move at a speed of 3 cells per game second.

There are different types of munchkins. Munchkins of type P1 can only be destroyed by a hit with mace M1. Type P2 can only be destroyed by M2 and P3 by M3.

The maces can move horizontally or vertically. They will always move towards the munchkin that has popped up, in the direction where there is greatest distance to cover.
In the event that the distance is the same in both horizontal and vertical directions, the horizontal distance is covered first
Move the required number of cells to reach the aligned row/column and then move perpendicular to reach the required cell.
For example if you are in cell 0,0 (start of the game) and are using mace M1 and the first munchkin appears in cell (3, 2) then the mace move two cells up to (2,0) by the end of two second period and this becomes the starting point for the next 2 second cycle

Given a set of munchkins, the sequence and cells where they appear, and the choice of mace, your job is to compute how many munchkins will be bopped on the head.

Input
The first line has a single number N which is the number of munchkins that will pop out
It will be followed by N lines in the form "MunchkinType Cell"
The final line will have the type of mace to use

Eg:

6 [ N - number of munchkins that appear ]
P2 4 4 [ munchkin of type P2 will appear at cell (4, 4) at second 0 ]
P2 2 5 [ munchkin of type P2 will appear at cell (2, 5) at second 2 ]
P2 5 4
P1 5 3 [ munchkin of type P1 will appear at cell (5, 3) at second 6 ]
P3 1 0
P2 3 3
M2 [ mace of type M2 has to be used ]

Output
2 [ number of munchkins that were bopped ]

Explanation:
You start at (0, 0)
Munchkin is at (4, 4). In 2 seconds you can move to (4, 0)
Munchkin is at (2, 5). Mace moves to (4, 4) to cover the greater vertical distance first
Munchkin is at (5, 4). Mace moves to (5, 4). 1 munchkin bopped
Munchkin is at (5, 3). Mace moves to (5, 3). Cannot bop munchkin of type P1
Munchkin is at (1, 0). Mace moves to (1, 3).
Munchkin is at (3, 3). Mace moves to (3, 3). 2 munchkins bopped


What I have tried:

I COULDN'T EVEN UNDERSTAND THE PROBLEM.

[on behalf of the OP]
C++
#include <bits/stdc++.h>
using namespace std;
int sd,nowx,nowy;
int cnt;
int dx,dy,x,y;
int up_bd;
void jao(int indx[][3],int in,int chk)
{
    
    if((abs(dx-x))<abs(dy-y))
    {
        int temp = min(abs(dy-y),up_bd);
        if(dy>y)y+=temp;
        else y-=temp;
        up_bd-=temp;
        temp = min(abs(dx-x),up_bd);
        if(dx>x)x+=temp;
        else x-=temp;
        up_bd-=temp;
        
    }
    else 
    {
        int temp = min(abs(dx-x),up_bd);
        if(dx>x)x+=temp;
        else x-=temp;
        up_bd-=temp;
        temp = abs(dy-y);
        temp = min(temp,up_bd);
        if(dy>y)y+=temp;
        else y-=temp;
        up_bd-=temp;
    }
    if(x==indx[in][0]&&y==indx[in][1] && chk==indx[in][2])cnt++;
    nowx=x,nowy=y;
    
}
int main()
{
  int n;
  cin>>n;
  string s;
  int indx[n][3];
  for(int i=0;i<n;i++)
  {
      
      cin>>s;
      cin>>indx[i][0]>>indx[i][1];
      if(s[1]=='1')indx[i][2]=1;
      else if (s[1]=='2')indx[i][2]=2;
      else indx[i][2]=3;
  }
  cin>>s;
  if(s[1]=='1')sd=1;
  else if(s[1]=='2')sd=2;
  else sd=3;
  
  for(int i=0;i<n;i++)
  {
    up_bd = sd*2;
    x=nowx;
    y=nowy;
    dx = indx[i][0];
    dy=indx[i][1];
    jao(indx,i,sd);
  }
  cout<<cnt<<endl;
  return 0;
}

[/on behalf of the OP]
Posted
Updated 12-Apr-21 7:19am
v2
Comments
KarstenK 12-Apr-21 3:00am    
when you dont understand the task and dont want to engage in the solution you should consider change something in your life ;-)
keshav kaushik 2021 12-Apr-21 4:37am    
:)
Rick York 12-Apr-21 3:19am    
I recommend that you read your input from a text file so you don't have to type it every time. To do that, open the file with a istream object and use that instead of cin. The rest of your code won't have to change.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
Start by thinking about how you would do it on paper, and work from there. If you can't understand the question at all after that, you need to talk to you teacher, not us.
 
Share this answer
 
Quote:
I COULDN'T EVEN UNDERSTAND THE PROBLEM.
The requirements are pretty clear. I assume you find difficult to code a solution.
You should focus on game timing (that is game iterations: iteration 0 is starting time, iteration 1 is 'first second elapsed' and so on) and implement a Mace class obeying the give rules.
If you still have doubts, please ask here specific questions.
 
Share this answer
 
The solution is not optimized but works fine.
C++
#include <iostream>
#include <stdio.h>
#include <math.h>

using namespace std;

struct struc{
	int type, h = 0 , v = 0;
};
	
int main()
{
	//input
	
	int n, total = 0, max, i = 0, hl, vl;
	string t;
	
    cin>>n;
    struc munch[n], mace;
    
    for( ; i<n; ++i){
    	cin>>t>>munch[i].h>>munch[i].v;
    	munch[i].type = t[1] - '0';
	}
	cin>>t;
	mace.type = t[1] - '0';
	//logic	
	for(i = 0; i<n; ++i){
		if(mace.type == 1)
			max = 2;
		else if(mace.type == 2)
			max = 4;
		else
			max = 6;
		hl = munch[i].h - mace.h;
		vl = munch[i].v - mace.v;
		if(abs(hl) >= abs(vl)){
			if(abs(hl)<= max){
				mace.h = munch[i].h;
				max -= abs(hl);}
			else{
			mace.h += max;
			max = 0;}
		}
		if(max != 0){
			if(abs(vl) < max){
				mace.v = munch[i].v;
				max -= abs(vl);}
			else{
			mace.v += max;
			max = 0;}
			}
		if ((mace.h == munch[i].h && mace.v == munch[i].v) && mace.type == munch[i].type)
			total++;
	}	
	//output
	
	cout<<endl<<total;

    return 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900