- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 Questions & Answers
- Program to find minimum steps to reach target position by a chess knight in Python
- Program to check robot can reach target position or not in Python
- Program to find minimum cost to reach final index with at most k steps in python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- C++ code to find minimum jump to reach home by frog
- C++ code to count years to reach certain rank in an army
- Program to check robot can reach target by keep moving on visited spots in Python
- C++ code to find reduced direction string of robot movement
- How to find the minimum number of steps needed by knight to reach the destination using C#?
- Position of robot after given movements in C++
- C++ program to count number of operations needed to reach n by paying coins
- Program to check we can reach at position n by jumping or not in Python
- Program to find number of minimum steps to reach last index in Python
- C++ code to check grasshopper can reach target or not
- Find minimum steps required to reach the end of a matrix in C++

Advertisements