
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to count steps to reach final position by robot
Suppose we have two coordinates (x1, y1) and (x2, y2). A robot is at the point (x1, y1) and wants to go to the point (x2, y2). In a single step, the robot can move towards one cell to its 8 adjacent coordinates. We have to find minimal number of steps needed to reach the final position.
So, if the input is like x1 = 3; y1 = 4; x2 = 6; y2 = 1;, then the output will be 3, because
Steps
To solve this, we will follow these steps −
return maximum of |x2 - x1| and |y2 - y1|
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(int x1, int y1, int x2, int y2){ return max(abs(x2 - x1), abs(y2 - y1)); } int main(){ int x1 = 3; int y1 = 4; int x2 = 6; int y2 = 1; cout << solve(x1, y1, x2, y2) << endl; }
Input
3, 4, 6, 1
Output
3
- Related Articles
- Program to check robot can reach target position or not in Python
- Program to find minimum steps to reach target position by a chess knight in Python
- C++ code to count years to reach certain rank in an army
- C++ code to find minimum jump to reach home by frog
- C++ code to find reduced direction string of robot movement
- Program to find minimum cost to reach final index with at most k steps in python
- Program to check robot can reach target by keep moving on visited spots in Python
- Position of robot after given movements in C++
- C++ program to count number of operations needed to reach n by paying coins
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- C++ code to check grasshopper can reach target or not
- C++ Program to check k rupees are enough to reach final cell or not
- C++ code to find final number after min max removal game
- Count number of ways to jump to reach end in C++

Advertisements