52.0k views
5 votes
Lucy loves to play the Hop. Skip and Jump game. Given an

N∗M matrix and starting from cell (1,1), her challenge is to hop in an anti-clockwise direction and skip alternate cells. The goal is to find out the last cell she would hop onto. Write an algorithm to find the last cell Lucy would hop onto after moving anti-clockwise and skipping alternate cells.
Input The first line of input consists of two integersmatrix_rowand matrix col, representing the number of rows (N) and the number of columns (M) in the matrix, respectively. The next M lines consist of N space-separated integers representing the elements in each cell of the matrix. Output Print an integer representing the last cell Lucy would hop onto after following the given instructions. Example Input: 3 3 29 8 37
15 41 3 1 10 14 Output: 41 Explanation: Lucy starts with 29, skips 15, hops onto 1, skip 10, hops onto 14, skips 3, hops onto 37 , skips 8 and finally hops onto 41. So, the output is 41 .

User Jrieke
by
6.6k points

1 Answer

1 vote

Final answer:

To find the last cell Lucy would hop onto after moving anti-clockwise and skipping alternate cells in a given N*M matrix, we can use a simple algorithm.

Step-by-step explanation:

To find the last cell Lucy would hop onto after moving anti-clockwise and skipping alternate cells in a given N*M matrix, we can use a simple algorithm.

  1. Initialize two variables, row and col, to 0.
  2. Start from cell (1,1) and move in the anti-clockwise direction.
  3. While row is less than N and col is less than M, perform the following steps:
    1. At cell (row, col), skip the current cell by incrementing col by 1.
    2. If col is less than M and row is less than N, hop onto the next cell by incrementing row by 1.
    3. If row is less than N and col is less than M, hop onto the next cell by incrementing col by 1.

The last cell Lucy would hop onto is the cell at (row, col) when the loop ends.

User Osie
by
7.5k points