VOOZH about

URL: https://www.geeksforgeeks.org/dsa/knights-tour-for-maximum-points/

⇱ Knight's tour for maximum points - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Knight's tour for maximum points

Last Updated : 23 Jul, 2025

Given a starting position in a 2D matrix representing a grid of points, the task is to find the minimum number of steps required for a knight to collect the maximum points, subject to the following conditions:

  • The knight can move only as per the rules of a standard chess knight (i.e. two squares in one direction and one square in a perpendicular direction).
  • The knight can collect all the points from all the cells that can be visited in exactly i steps without revisiting any cell.
  • If the knight can collect y coins in the xth step, he can fetch all the coins that he will collect in the (x + y)th step, and if the knight can collect z coins in the yth step, he can fetch all the coins that he will collect in the (y + z)th step and so on, without increasing the step count.
  • The knight can start and end at any cell on the grid.
  • The grid can have any number of rows and columns.

Note: The knight moves exactly the same as the knight on a chess board. Please follow 0-based indexing.

Examples:

Input: n = 9, m = 10, start_x = 4, start_y = 5
arr =
0 0 0 2 0 2 0 2 0 0
0 0 2 0 2 0 2 0 2 0
0 2 0 0 1 2 0 0 0 2
0 0 2 0 2 0 2 0 2 0
0 2 0 2 0 0 0 2 0 2
0 0 2 0 2 0 2 0 2 0
0 2 0 0 0 2 0 0 0 2
0 0 2 0 2 0 2 0 2 0
0 0 0 2 0 2 0 2 0 0
Output: 1
Explanation: minimum knight have to take 1 steps to gain maximum points.

  • Initially, the knight has 0 coins, he will take 1 step to collect 1 point (sum of cells denoted in red color).
  • Now in the second step, he can collect points from all the cells colored green i.e. 64 points.
  • But with his magical power, at the 1st step, he can fetch points from the (1 + 1)th step. Therefore he can collect 1 + 64 coins at step 1 only. Hence answer is 1.

Input: n = 3, m = 3, start_x = 2, start_y = 1
arr =
7 6 8
9 1 4
6 2 8
Output: 0
Explanation: Initially, the knight has 2 points, or more formally we can say that at the 0th step knight has 2 points.

  • In the first step, he can collect points from cells (0, 0) and (0, 2) i.e. 15 points.
  • In the second step, he can collect points from cells (1, 0) and (1, 2) i.e. 13 coins.
  • In the third step, he can collect points from cells (2, 0) and (2, 2) i.e. 14 points.
  • In the fourth step, he can collect points from the cell (0, 1) i.e. 6 points.
  • So in each step, he can collect coins like -You can see in the below image  Knight can collect 15 coins in the 0th step only

Approach: To solve the problem follow the below idea:

The idea is to to explore all the valid neighboring cells from the current cell using BFS traversal, and keep track of the visited cells. Now, calculate the total points collected by the knight, and add the points to a list at each level. After the BFS completes, find the maximum sum of points that can be collected and return the level at which the maximum sum is collected.

Below are the steps for the above approach:

  • Create a 2d visited array of size n*m to keep track of visited cells.
  • Mark starting position as true.
    • visited[start_x][start_y] = true.
  • Create a queue to perform BFS traversal and push the starting position in the queue.
  • Create a list to store the sum of points collected at each level of the BFS traversal
  • While the queue is not empty run a loop.
    • Initialize a variable say size and store the size of the current level by getting the size of the queue.
    • Initialize a variable_points = 0 for the current level.
    • Run a loop to process all the cells at the current level.
    • Dequeue the front element of the queue and store its x and y coordinates.
    • Add the points at the current cell to the total points collected, points += arr[x][y].
  • Visit all the neighboring cells that are valid and have not been visited before.
  • Store the new x and y coordinates of the neighboring cell using the dx and dy arrays and check if it is valid and has not been visited before, mark it as visited, and add it to the queue.
  • Add the sum of points collected at the current level to the list.
  • Run a loop from the size of the back side of the point array, list[i] = x, which means we can get x points in the ith step. And do list[i] += list[i + list[i]].
  • Find the max of the list[i] and return the answer.

Below is the code for the above approach:


Output
1

Time Complexity: O(N*M)
Auxiliary Space: O(N*M)

Related Articles:

Comment
Article Tags:
Article Tags: