Write a code in C++ that does the following task. A robot is at the top left of
ID: 3757361 • Letter: W
Question
Write a code in C++ that does the following task.
A robot is at the top left of an N × N grid, and needs to get to the bottom right in order
to paint his message. To reach his destination as soon as possible, he’ll only move along the
shortest way, by moving right and down only.
The robot can encounter obstacles marked as negative numbers,
where the magnitude is the amount of damage they deal.
The positive numbers are tiles that heal the robot that amount of HP, P are tiles that prevent
the robot’s next damage, and D are tiles that double the robot’s next healing effect.
If the robot reaches 0 HP or less, he collapses and the mission fails, so prevent that!
No matter how many P tiles and D tiles the robot encounters, their effects don’t stack.
For example, if the robot walks on a D tile when he’s already in the state of “doubling next healing
effect”, nothing happens. When the robot next encounters a healing tile, the healing will be doubled.
Afterwards, the robot returns to a “normal” state. The same applies for P tiles.
What is the minimum amount of life the robot needs in order to complete his task?
Explanation / Answer
// C++ implementation to find final position of
// robot after the complete movement
#include <bits/stdc++.h>
using namespace std;
// function to to find final position of
// robot after the complete movement
void finalPosition(string move)
{
int l = move.size();
int countUp = 0, countDown = 0;
int countLeft = 0, countRight = 0;
// traverse the instruction string 'move'
for (int i = 0; i < l; i++) {
// for each movement increment its
// respective counter
if (move[i] == 'U')
countUp++;
else if (move[i] == 'D')
countDown++;
else if (move[i] == 'L')
countLeft++;
else if (move[i] == 'R')
countRight++;
}
// required final position of robot
cout << "Final Position: ("
<< (countRight - countLeft)
<< ", " << (countUp - countDown)
<< ")" << endl;
}
// Driver program to test above
int main()
{
string move = "UDDLLRUUUDUURUDDUULLDRRRR";
finalPosition(move);
return 0;
}
Output:
Final Position: (2, 3)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.