The question that I found here says:

A robot has to move in a grid which is in the form of a matrix. It can go to

1.) A(i,j)--> A(i+j,j) (Down)

2.) A(i,j)--> A(i,i+j) (Right)Given it starts at (1,1) and it has to go to A(m,n), find the minimum number of STEPS it has to take to get to (m,n) and write

public static int minSteps(int m,int n)For instance to go from (1,1) to m=3 and n=2 it has to take (1, 1) -> (1, 2) -> (3, 2) i.e. 2 steps

The shortest steps comes when moving diagonally meaning right-down-right-down... or down-right-down-right... . However, depending on m and n best answer may need to start from right or down. the easiest way is to do both and find the minimum. Here us my Java method:

public static int minSteps(int m, int n){ int x,y; int stepsDownFirst=0; boolean downDirection=true; for(x=1, y=1;x<m || y<n;stepsDownFirst++){ if(downDirection) y+=x; else x+=y; downDirection=!downDirection; } int stepsRightFirst=0; downDirection=false; for(x=1, y=1;x<m || y<n;stepsRightFirst++){ if(downDirection) y+=x; else x+=y; downDirection=!downDirection; } if(stepsDownFirst<stepsRightFirst) return stepsDownFirst; else return stepsRightFirst; }

After some thoughts I realized I can make it simpler so I wrote this short C++ code:

int minSteps(int m, int n){ int x,y; bool downDirection=true; int steps=0; for(x=y=1;(x<m||y<n)&&(x<n||y<m);steps++){ if (downDirection) y+=x; else x+=y; downDirection=!downDirection; } return steps; }